Implementation:Online ml River FeatureExtraction RBFSampler
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Feature_Engineering, Kernel_Methods |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Approximates RBF kernel feature maps using random Fourier features for linear models.
Description
RBFSampler extracts random features that approximate the Radial Basis Function (RBF) kernel through explicit feature mapping. It implements the random Fourier features technique, which transforms input features using random weights drawn from a Gaussian distribution and trigonometric functions. The transformation projects features into a higher-dimensional space where linear models can capture non-linear patterns. The projection matrix is lazily constructed as new features are encountered, making it suitable for streaming data.
Usage
Use RBFSampler to give non-linear capacity to linear classifiers and regressors without the computational cost of kernel methods. Particularly effective for large datasets where kernel SVMs become intractable. The gamma parameter controls the RBF kernel width, and n_components determines the dimensionality of the approximation. Common use cases include making linear models competitive with kernel methods on non-linearly separable problems like XOR.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/feature_extraction/kernel_approx.py
Signature
class RBFSampler(base.Transformer):
def __init__(self, gamma=1.0, n_components=100, seed: int | None = None)
Import
from river import feature_extraction
I/O Contract
| Input | Output |
|---|---|
| Dict[str, float] - Original features | Dict[Tuple[str, int], float] - RBF features |
Usage Examples
from river import feature_extraction as fx
from river import linear_model as lm
from river import optim
from river import stream
# XOR function - non-linearly separable
X = [[0, 0], [1, 1], [1, 0], [0, 1]]
Y = [0, 0, 1, 1]
# Linear model fails without RBF features
model = lm.LogisticRegression(optimizer=optim.SGD(.1))
for x, y in stream.iter_array(X, Y):
model.learn_one(x, y)
y_pred = model.predict_one(x)
print(y, int(y_pred))
# 0 0
# 0 0
# 1 0
# 1 1
# With RBF features, linear model learns XOR
model = (
fx.RBFSampler(seed=3) |
lm.LogisticRegression(optimizer=optim.SGD(.1))
)
for x, y in stream.iter_array(X, Y):
model.learn_one(x, y)
y_pred = model.predict_one(x)
print(y, int(y_pred))
# 0 0
# 0 0
# 1 1
# 1 1