Implementation:Online ml River Compose Renamer
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Feature_Engineering, Data_Transformation, Naming |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Feature naming transformers that rename, prefix, or suffix feature names in streaming data.
Description
This module provides three simple transformers for modifying feature names: Renamer, Prefixer, and Suffixer. These are pure transformers that operate only on feature names without modifying feature values.
Renamer applies a mapping dictionary to rename specific features. Keys in the mapping that don't exist in the input are silently ignored, making it safe to use with varying feature sets. The transformation modifies the dictionary keys in-place using pop and reassignment.
Prefixer and Suffixer add a fixed string to the beginning or end of all feature names respectively. These are useful for namespacing features from different sources or stages in a pipeline, helping to avoid name collisions when combining features from multiple transformers.
Usage
Use Renamer when you need to change specific feature names to match expected inputs of downstream components. Use Prefixer/Suffixer when combining features from multiple sources and you want to keep them clearly distinguished, or when you need to maintain feature provenance through a complex pipeline.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/compose/renamer.py
Signature
class Renamer(base.Transformer):
def __init__(self, mapping: dict[str, str]):
...
class Prefixer(base.Transformer):
def __init__(self, prefix: str):
...
class Suffixer(base.Transformer):
def __init__(self, suffix: str):
...
Import
from river import compose
I/O Contract
| Parameter | Type | Description |
|---|---|---|
| mapping | dict[str, str] | Dictionary mapping old names to new names |
| x | dict | Feature dictionary to transform |
| Parameter | Type | Description |
|---|---|---|
| prefix | str | String to prepend to all feature names |
| x | dict | Feature dictionary to transform |
| Parameter | Type | Description |
|---|---|---|
| suffix | str | String to append to all feature names |
| x | dict | Feature dictionary to transform |
| Method | Return Type | Description |
|---|---|---|
| transform_one(x) | dict | Dictionary with renamed features |
| Method | Parameters | Description |
|---|---|---|
| transform_one(x) | x: dict | Transforms feature names according to strategy |
Usage Examples
from river import compose
# Renamer: Substitute specific feature names
mapping = {'a': 'alpha', 'c': 'gamma'}
x = {'a': 42, 'b': 12, 'c': 7}
renamer = compose.Renamer(mapping)
print(renamer.transform_one(x))
# {'alpha': 42, 'b': 12, 'gamma': 7}
# Keys not in mapping are unchanged
x2 = {'a': 42, 'b': 12}
print(renamer.transform_one(x2))
# {'alpha': 42, 'b': 12}
# Prefixer: Add prefix to all features
x = {'a': 42, 'b': 12}
prefixer = compose.Prefixer('feature_')
print(prefixer.transform_one(x))
# {'feature_a': 42, 'feature_b': 12}
# Suffixer: Add suffix to all features
suffixer = compose.Suffixer('_v1')
print(suffixer.transform_one(x))
# {'a_v1': 42, 'b_v1': 12}
# Use in pipeline to namespace features
from river import preprocessing
from river import feature_extraction
from river import stats
# Example: Combine features from different sources with prefixes
raw_scaler = compose.Prefixer('raw_') | preprocessing.StandardScaler()
agg_features = feature_extraction.Agg(
on='value',
by='category',
how=stats.Mean()
)
agg_with_prefix = agg_features | compose.Prefixer('agg_')
# Combine both with union
combined = raw_scaler + agg_with_prefix
# Example: Rename features to match model expectations
feature_names = {
'user_age': 'age',
'user_gender': 'gender',
'item_price': 'price'
}
pipeline = (
compose.Renamer(feature_names) |
preprocessing.StandardScaler()
)
# Example: Use suffix to track transformation stages
from river import linear_model
model = (
preprocessing.StandardScaler() |
compose.Suffixer('_scaled') |
feature_extraction.PolynomialExtender(degree=2) |
compose.Suffixer('_poly') |
linear_model.LinearRegression()
)
# This creates feature names like:
# 'a_scaled', 'b_scaled', 'a_scaled*b_scaled_poly', etc.
# Example: Multiple renamers in sequence
rename_stage1 = compose.Renamer({'a': 'x', 'b': 'y'})
rename_stage2 = compose.Renamer({'x': 'feature1', 'y': 'feature2'})
pipeline = rename_stage1 | rename_stage2
x = {'a': 1, 'b': 2, 'c': 3}
print(pipeline.transform_one(x))
# {'feature1': 1, 'feature2': 2, 'c': 3}
# Example: Combine prefix and suffix
transformer = (
compose.Prefixer('v2_') |
compose.Suffixer('_normalized')
)
x = {'temperature': 20.5, 'humidity': 65}
print(transformer.transform_one(x))
# {'v2_temperature_normalized': 20.5, 'v2_humidity_normalized': 65}