Implementation:Online ml River Stream Iter Sklearn
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Data_Streaming, Scikit_Learn |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Converts scikit-learn dataset objects (Bunch) into River-compatible data streams.
Description
The iter_sklearn_dataset function enables iteration over scikit-learn datasets, including those loaded via fetch_openml which provides access to thousands of datasets from OpenML. It automatically handles both array and DataFrame formats, extracting feature names when available. This bridges scikit-learn's batch learning interface with River's online learning paradigm.
Usage
Use this when working with scikit-learn's built-in datasets or datasets fetched from OpenML. It's particularly useful for comparing batch and online learning approaches on the same data, or for converting standard benchmark datasets to streaming format.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/stream/iter_sklearn.py
Signature
def iter_sklearn_dataset(
dataset: sklearn.utils.Bunch,
**kwargs
) -> base.typing.Stream:
...
Import
from river import stream
I/O Contract
| Parameter | Type | Description |
|---|---|---|
| dataset | sklearn.utils.Bunch | A scikit-learn dataset object |
| **kwargs | dict | Additional arguments passed to iter_array or iter_pandas |
Returns:
| Type | Description |
|---|---|
| Iterator[(dict, Any)] | Stream of (features dict, target) tuples |
Usage Examples
import pprint
from sklearn import datasets
from river import stream
# Load a scikit-learn dataset
dataset = datasets.load_diabetes()
# Iterate over the dataset
for xi, yi in stream.iter_sklearn_dataset(dataset):
pprint.pprint(xi)
print(f"Target: {yi}")
break
# Output:
# {'age': 0.038075906433423026,
# 'bmi': 0.061696206518683294,
# 'bp': 0.0218723855140367,
# 's1': -0.04422349842444599,
# 's2': -0.03482076283769895,
# 's3': -0.04340084565202491,
# 's4': -0.002592261998183278,
# 's5': 0.019907486170462722,
# 's6': -0.01764612515980379,
# 'sex': 0.05068011873981862}
# Target: 151.0
# Using with other sklearn datasets
iris = datasets.load_iris()
print("\nIris dataset:")
for x, y in stream.iter_sklearn_dataset(iris):
print(f"Features: {list(x.keys())}")
print(f"Target: {y}")
break
# With OpenML datasets (requires internet)
# from sklearn.datasets import fetch_openml
# credit_g = fetch_openml('credit-g', version=1, parser='auto')
# for x, y in stream.iter_sklearn_dataset(credit_g):
# print(x, y)
# break
# You can pass additional kwargs like shuffle
wine = datasets.load_wine()
shuffled = stream.iter_sklearn_dataset(wine, shuffle=True, seed=42)
print("\nFirst 3 shuffled samples:")
for i, (x, y) in enumerate(shuffled):
if i >= 3:
break
print(f"Sample {i}: target={y}, features={list(x.keys())}")