Implementation:Online ml River Preprocessing RandomProjection
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Preprocessing, Dimensionality_Reduction |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Random projection methods for dimensionality reduction using Gaussian or sparse random matrices.
Description
This module provides two random projection techniques for reducing feature dimensionality while approximately preserving distances. GaussianRandomProjector uses components drawn from N(0, 1/n_components) to project features into a lower-dimensional space. SparseRandomProjector uses sparse random matrices with configurable density, trading some accuracy for memory efficiency. Both methods maintain online projection matrices that are lazily instantiated as new features are encountered, making them suitable for streaming high-dimensional data.
Usage
Use random projections to reduce dimensionality before applying linear models, particularly when dealing with very high-dimensional data. Gaussian projection works well for dense features, while sparse projection is preferable for sparse high-dimensional data (like text). Both preserve approximate distances between points (Johnson-Lindenstrauss lemma), making them useful for nearest neighbor methods, clustering, and as regularization techniques.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/preprocessing/random_projection.py
Signature
class GaussianRandomProjector(base.Transformer):
def __init__(self, n_components=10, seed: int | None = None)
class SparseRandomProjector(base.Transformer):
def __init__(self, n_components=10, density=0.1, seed: int | None = None)
Import
from river import preprocessing
I/O Contract
| Input | Output |
|---|---|
| Dict[str, float] - Original features | Dict[int, float] - Projected features |
Usage Examples
from river import datasets
from river import evaluate
from river import linear_model
from river import metrics
from river import preprocessing
# Gaussian Random Projection
dataset = datasets.TrumpApproval()
model = preprocessing.GaussianRandomProjector(
n_components=3,
seed=42
)
for x, y in dataset:
x = model.transform_one(x)
print(x)
break
# {0: -61289.371..., 1: 141312.510..., 2: 279165.993...}
# In a pipeline with regression
model = (
preprocessing.GaussianRandomProjector(
n_components=5,
seed=42
) |
preprocessing.StandardScaler() |
linear_model.LinearRegression()
)
evaluate.progressive_val_score(dataset, model, metrics.MAE())
# MAE: 0.933...
# Sparse Random Projection
dataset = datasets.TrumpApproval()
model = preprocessing.SparseRandomProjector(
n_components=3,
seed=42
)
for x, y in dataset:
x = model.transform_one(x)
print(x)
break
# {0: 92.89572746525327, 1: 1344540.5692342375, 2: 0}