Skip to content

iter_array

Iterates over the rows from an array of features and an array of targets.

This method is intended to work with numpy arrays, but should also work with Python lists.

Parameters

  • X

    Typenp.ndarray

    A 2D array of features. This can also be a 1D array of strings, which can be the case if you're working with text.

  • y

    Typenp.ndarray | None

    DefaultNone

    An optional array of targets.

  • feature_names

    Typelist[base.typing.FeatureName] | None

    DefaultNone

    An optional list of feature names. The features will be labeled with integers if no names are provided.

  • target_names

    Typelist[base.typing.FeatureName] | None

    DefaultNone

    An optional list of output names. The outputs will be labeled with integers if no names are provided. Only applies if there are multiple outputs, i.e. if y is a 2D array.

  • shuffle

    Typebool

    DefaultFalse

    Indicates whether or not to shuffle the input arrays before iterating over them.

  • seed

    Typeint | None

    DefaultNone

    Random seed used for shuffling the data.

Examples

from river import stream
import numpy as np

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, y in dataset:
    print(x, y)
{'x1': 1, 'x2': 2, 'x3': 3} True
{'x1': 11, 'x2': 12, 'x3': 13} False

This also works with a array of texts:

X = ["foo", "bar"]
dataset = stream.iter_array(
    X, Y,
    feature_names=['x1', 'x2', 'x3']
)
for x, y in dataset:
    print(x, y)
foo True
bar False