Implementation:Online ml River FeatureExtraction PolynomialExtender
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Feature_Engineering, Polynomial_Features |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Generates polynomial combinations of features to capture interaction effects for linear models.
Description
PolynomialExtender creates all polynomial combinations of input features up to a specified degree. It generates products of features to capture interaction effects and non-linear relationships that linear models cannot learn directly. The interaction_only parameter restricts combinations to include each feature at most once. An optional bias term can be added as a constant feature. Feature names are automatically constructed by joining constituent feature names with asterisks.
Usage
Use this to add polynomial and interaction features to linear models, enabling them to capture non-linear patterns. Degree 2 is most common, capturing pairwise interactions. Higher degrees can cause overfitting and exponential feature growth. The interaction_only parameter is useful when you only want cross-feature interactions without powers. Combine with StandardScaler to normalize the polynomial features before training.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/feature_extraction/poly.py
Signature
class PolynomialExtender(base.Transformer):
def __init__(self, degree=2, interaction_only=False, include_bias=False, bias_name="bias")
Import
from river import feature_extraction
I/O Contract
| Input | Output |
|---|---|
| Dict[str, float] - Original features | Dict[str, float] - Original + polynomial features |
Usage Examples
from river import feature_extraction as fx
X = [
{'x': 0, 'y': 1},
{'x': 2, 'y': 3},
{'x': 4, 'y': 5}
]
poly = fx.PolynomialExtender(degree=2, include_bias=True)
for x in X:
print(poly.transform_one(x))
# {'x': 0, 'y': 1, 'x*x': 0, 'x*y': 0, 'y*y': 1, 'bias': 1}
# {'x': 2, 'y': 3, 'x*x': 4, 'x*y': 6, 'y*y': 9, 'bias': 1}
# {'x': 4, 'y': 5, 'x*x': 16, 'x*y': 20, 'y*y': 25, 'bias': 1}
# Interaction-only mode
X = [
{'x': 0, 'y': 1, 'z': 2},
{'x': 2, 'y': 3, 'z': 2},
{'x': 4, 'y': 5, 'z': 2}
]
poly = fx.PolynomialExtender(degree=3, interaction_only=True)
for x in X:
print(poly.transform_one(x))
# {'x': 0, 'y': 1, 'z': 2, 'x*y': 0, 'x*z': 0, 'y*z': 2, 'x*y*z': 0}
# {'x': 2, 'y': 3, 'z': 2, 'x*y': 6, 'x*z': 4, 'y*z': 6, 'x*y*z': 12}
# {'x': 4, 'y': 5, 'z': 2, 'x*y': 20, 'x*z': 8, 'y*z': 10, 'x*y*z': 40}
# In a pipeline
from river import datasets
from river import evaluate
from river import linear_model as lm
from river import metrics
from river import preprocessing as pp
dataset = datasets.Phishing()
model = (
fx.PolynomialExtender() |
pp.StandardScaler() |
lm.LogisticRegression()
)
metric = metrics.Accuracy()
evaluate.progressive_val_score(dataset, model, metric)
# Accuracy: 88.88%