Implementation:Online ml River Stream Iter Array
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Data_Streaming, Array_Processing |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Converts NumPy arrays or Python lists into a River-compatible data stream for online learning.
Description
The iter_array function transforms 2D arrays of features and 1D arrays of targets into an iterable stream of (features_dict, target) tuples. It supports feature naming, shuffling, multi-output targets, and can handle both NumPy arrays and Python lists. Features can be automatically labeled with integers or custom names can be provided.
Usage
Use this when you have data in array format (NumPy or lists) and want to convert it to River's dict-based format for online learning. It's essential for bridging batch learning datasets with River's streaming API.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/stream/iter_array.py
Signature
def iter_array(
X: np.ndarray,
y: np.ndarray | None = None,
feature_names: list[base.typing.FeatureName] | None = None,
target_names: list[base.typing.FeatureName] | None = None,
shuffle: bool = False,
seed: int | None = None,
) -> base.typing.Stream:
...
Import
from river import stream
I/O Contract
| Parameter | Type | Description |
|---|---|---|
| X | np.ndarray | 2D array of features or 1D array of strings |
| y | np.ndarray or None | Array of targets (1D or 2D for multi-output) |
| feature_names | list or None | Names for features (uses integers if None) |
| target_names | list or None | Names for multi-output targets |
| shuffle | bool | Whether to shuffle data before streaming |
| seed | int or None | Random seed for shuffling |
Returns:
| Type | Description |
|---|---|
| Iterator[(dict, Any)] | Stream of (features dict, target) tuples |
Usage Examples
import numpy as np
from river import stream
# Example 1: Basic usage with feature names
X = np.array([[1, 2, 3], [11, 12, 13]])
y = np.array([True, False])
dataset = stream.iter_array(
X, y,
feature_names=['x1', 'x2', 'x3']
)
for x, target in dataset:
print(f"Features: {x}, Target: {target}")
# Output:
# Features: {'x1': 1, 'x2': 2, 'x3': 3}, Target: True
# Features: {'x1': 11, 'x2': 12, 'x3': 13}, Target: False
# Example 2: With shuffling
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
y = np.array([10, 20, 30, 40])
shuffled_stream = stream.iter_array(
X, y,
feature_names=['a', 'b'],
shuffle=True,
seed=42
)
print("Shuffled order:")
for x, target in shuffled_stream:
print(f"{x} -> {target}")
# Example 3: Text data
X_text = np.array(["hello world", "machine learning"])
y_text = np.array([1, 0])
for text, label in stream.iter_array(X_text, y_text):
print(f"Text: {text}, Label: {label}")
# Example 4: Multi-output targets
X_multi = np.array([[1, 2], [3, 4]])
y_multi = np.array([[0.5, 0.3], [0.7, 0.2]])
multi_stream = stream.iter_array(
X_multi, y_multi,
feature_names=['f1', 'f2'],
target_names=['out1', 'out2']
)
for x, y_dict in multi_stream:
print(f"Features: {x}")
print(f"Targets: {y_dict}")