Implementation:Online ml River Compat RiverToSklearn
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Scikit_Learn, Compatibility, Machine_Learning |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Compatibility wrappers that convert River online learning models to scikit-learn compatible estimators with batch processing interfaces.
Description
The river-to-sklearn compatibility layer provides wrapper classes that adapt River's online learning estimators to work with scikit-learn's batch learning API. The module includes four main wrapper classes: River2SKLRegressor, River2SKLClassifier, River2SKLTransformer, and River2SKLClusterer.
Each wrapper maintains a deep copy of the River estimator and implements scikit-learn's standard interface methods (fit, partial_fit, predict, transform). The wrappers handle data validation, feature consistency checks, and proper streaming of batch data through the River model's learn_one/predict_one interface.
For classifiers, the module handles both binary and multiclass cases, applying label encoding when necessary for binary classifiers. For transformers, it distinguishes between supervised and unsupervised transformers. The convert_river_to_sklearn function automatically detects the estimator type and applies the appropriate wrapper.
Usage
Use these wrappers when you need to integrate River's online learning models into scikit-learn pipelines or workflows that expect the standard scikit-learn API. This is particularly useful for comparing online learning models with batch models, or for using River models in existing scikit-learn-based systems.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/compat/river_to_sklearn.py
Signature
def convert_river_to_sklearn(estimator: base.Estimator):
...
class River2SKLRegressor(River2SKLBase, sklearn_base.RegressorMixin):
def __init__(self, river_estimator: base.Regressor):
...
class River2SKLClassifier(River2SKLBase, sklearn_base.ClassifierMixin):
def __init__(self, river_estimator: base.Classifier):
...
class River2SKLTransformer(River2SKLBase, sklearn_base.TransformerMixin):
def __init__(self, river_estimator: base.Transformer):
...
class River2SKLClusterer(River2SKLBase, sklearn_base.ClusterMixin):
def __init__(self, river_estimator: base.Clusterer):
...
Import
from river import compat
I/O Contract
| Parameter | Type | Description |
|---|---|---|
| estimator | base.Estimator | River estimator to wrap |
| Method | Return Type | Description |
|---|---|---|
| convert_river_to_sklearn(estimator) | sklearn wrapper | Returns appropriate scikit-learn compatible wrapper |
| Method | Parameters | Return Type | Description |
|---|---|---|---|
| fit(X, y) | X: array-like, y: array-like | self | Fits model on entire dataset |
| partial_fit(X, y) | X: array-like, y: array-like | self | Incrementally fits model on data portion |
| predict(X) | X: array-like | ndarray | Makes predictions on dataset |
| Method | Parameters | Return Type | Description |
|---|---|---|---|
| predict_proba(X) | X: array-like | ndarray | Returns class probabilities |
| Method | Parameters | Return Type | Description |
|---|---|---|---|
| transform(X) | X: array-like | ndarray | Transforms dataset |
Usage Examples
from river import compat
from river import linear_model
from river import preprocessing
from sklearn.model_selection import cross_val_score
import numpy as np
# Convert River regressor to sklearn
river_reg = preprocessing.StandardScaler() | linear_model.LinearRegression()
sklearn_reg = compat.convert_river_to_sklearn(river_reg)
# Now use it like any sklearn estimator
X = np.random.randn(100, 5)
y = np.random.randn(100)
# Batch training
sklearn_reg.fit(X, y)
predictions = sklearn_reg.predict(X)
# Incremental training
X_batch = np.random.randn(20, 5)
y_batch = np.random.randn(20)
sklearn_reg.partial_fit(X_batch, y_batch)
# Example with classifier
from river import naive_bayes
river_clf = naive_bayes.GaussianNB()
sklearn_clf = compat.convert_river_to_sklearn(river_clf)
X_clf = np.random.randn(100, 5)
y_clf = np.random.randint(0, 2, 100)
sklearn_clf.fit(X_clf, y_clf)
probas = sklearn_clf.predict_proba(X_clf)
predictions = sklearn_clf.predict(X_clf)
# Works with sklearn cross-validation
scores = cross_val_score(sklearn_clf, X_clf, y_clf, cv=5)
print(f"Cross-validation scores: {scores}")