# `BatchRegressor`

### *class* capymoa.base.BatchRegressor[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_batch_regressor.py#L20)

Bases: [`Regressor`](capymoa.base.Regressor.md#capymoa.base.Regressor), [`Batch`](capymoa.base.Batch.md#capymoa.base.Batch), [`ABC`](https://docs.python.org/3/library/abc.html#abc.ABC)

Base class for regressor that support mini-batches.

Supported by:

- [`capymoa.evaluation.prequential_evaluation()`](capymoa.evaluation.md#capymoa.evaluation.prequential_evaluation)

Evaluators that support batch classifiers will call the [`batch_train()`](#capymoa.base.BatchRegressor.batch_train)
and [`batch_predict()`](#capymoa.base.BatchRegressor.batch_predict) methods instead of [`train()`](#capymoa.base.BatchRegressor.train) and
[`predict()`](#capymoa.base.BatchRegressor.predict):

```pycon
>>> from capymoa.base import BatchRegressor
>>> from capymoa.datasets import FriedTiny
>>> from capymoa.evaluation import prequential_evaluation
>>>
>>> batch_size = 500
>>> class MyBatchRegressor(BatchRegressor):
...     def batch_train(self, x, y):
...         print(f"batch_train x: {x.shape} {x.dtype}")
...         print(f"batch_train y: {y.shape} {y.dtype}")
...
...     def batch_predict(self, x):
...         print(f"batch_predict x: {x.shape} {x.dtype}")
...         return np.zeros((x.shape[0],))
...
>>> stream = FriedTiny()
>>> learner = MyBatchRegressor(stream.get_schema())
>>> _ = prequential_evaluation(
...     stream,
...     learner,
...     batch_size=batch_size,
...     max_instances=721
... )
batch_predict x: torch.Size([500, 10]) torch.float32
batch_train x: torch.Size([500, 10]) torch.float32
batch_train y: torch.Size([500]) torch.float32
batch_predict x: torch.Size([221, 10]) torch.float32
batch_train x: torch.Size([221, 10]) torch.float32
batch_train y: torch.Size([221]) torch.float32
```

You can manually use `itertools.batched` (python 3.12) function and
`np.stack` to collect batches of instances as a matrix:

```pycon
>>> from itertools import islice
>>> from capymoa._utils import batched # Not available in python < 3.12
>>> for i, batch in enumerate(batched(stream, 100)):
...     x = np.stack([instance.x for instance in batch])
...     y = np.stack([instance.y_value for instance in batch])
...     x = torch.from_numpy(x).to(dtype=learner.x_dtype, device=learner.device)
...     y = torch.from_numpy(y).to(dtype=learner.y_dtype, device=learner.device)
...     learner.batch_train(x, y)
...     break
batch_train x: torch.Size([100, 10]) torch.float32
batch_train y: torch.Size([100]) torch.float32
```

The default implementation of [`train()`](#capymoa.base.BatchRegressor.train) and [`predict()`](#capymoa.base.BatchRegressor.predict) calls the
batch variants with a batch of size 1. This is useful for parts of CapyMOA
that expect a classifier to be able to train and predict on single
instances.

```pycon
>>> instance = next(stream)
>>> learner.train(instance)
batch_train x: torch.Size([1, 10]) torch.float32
batch_train y: torch.Size([]) torch.float32
>>> learner.predict(instance)
batch_predict x: torch.Size([1, 10]) torch.float64
np.float64(0.0)
```

#### \_\_init_\_(schema=None, random_seed=1)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_regressor.py#L11)

#### *abstract* batch_predict(x: [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)) → [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_batch_regressor.py#L101)

Return probability estimates for each label in a batch.

* **Parameters:**
  **x** – Batch of [`x_dtype`](#capymoa.base.BatchRegressor.x_dtype) valued feature vectors
  `(batch_size, num_features)`
* **Returns:**
  Predicted batch of [`y_dtype`](#capymoa.base.BatchRegressor.y_dtype) valued targets
  `(batch_size,)`.

#### *abstract* batch_train(x: [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor), y: [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)) → [None](https://docs.python.org/3/builtins/constants.html#None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_batch_regressor.py#L92)

Train the classifier with a batch of instances.

* **Parameters:**
  * **x** – Batch of [`x_dtype`](#capymoa.base.BatchRegressor.x_dtype) valued feature vectors
    `(batch_size, num_features)`
  * **y** – Batch of [`y_dtype`](#capymoa.base.BatchRegressor.y_dtype) valued targets `(batch_size,)`.

#### *classmethod* from_params(schema: [Any](https://docs.python.org/3/library/typing.html#typing.Any) = None, params: [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [Any](https://docs.python.org/3/library/typing.html#typing.Any)] | [None](https://docs.python.org/3/builtins/constants.html#None) = None, random_seed: [int](https://docs.python.org/3/builtins/functions.html#int) = 1) → [Any](https://docs.python.org/3/library/typing.html#typing.Any)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_learner_params.py#L170)

Construct an instance from parameters produced by `get_params`.

#### get_params() → [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [Any](https://docs.python.org/3/library/typing.html#typing.Any)][[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_learner_params.py#L163)

Return the hyper-parameters captured from the constructor.

#### predict(instance: [RegressionInstance](capymoa.core.RegressionInstance.md#capymoa.core.RegressionInstance)) → float64[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_batch_regressor.py#L120)

Calls [`batch_predict()`](#capymoa.base.BatchRegressor.batch_predict) with a batch of size 1.

#### train(instance: [RegressionInstance](capymoa.core.RegressionInstance.md#capymoa.core.RegressionInstance)) → [None](https://docs.python.org/3/builtins/constants.html#None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_batch_regressor.py#L111)

Calls [`batch_train()`](#capymoa.base.BatchRegressor.batch_train) with a batch of size 1.

#### device *: [device](https://docs.pytorch.org/docs/stable/tensor_attributes.html#torch.device)* *= device(type='cpu')*

Device on which the batch will be processed.

#### x_dtype *: [dtype](https://docs.pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)* *= torch.float32*

Data type for the input features.

#### y_dtype *: [dtype](https://docs.pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)* *= torch.float32*

Data type for the target value/labels.
