# `KNNRegressor`

### *class* capymoa.regressor.KNNRegressor[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/regressor/_knn.py#L6)

Bases: [`MOARegressor`](capymoa.base.MOARegressor.md#capymoa.base.MOARegressor)

K Nearest Neighbor for data stream regression with sliding window

The default number of neighbors (k) is set to 3 instead of 10 (as in MOA)

There is no specific publication for online KNN, please refer to:

[Bifet, Albert, Ricard Gavalda, Geoffrey Holmes, and Bernhard Pfahringer.
Machine learning for data streams: with practical examples in MOA. MIT press, 2023.](https://moa.cms.waikato.ac.nz/book-html/)

Example usage:

```pycon
>>> from capymoa.datasets import Fried
    >>> from capymoa.regressor import KNNRegressor
    >>> from capymoa.evaluation import prequential_evaluation
>>> stream = Fried()
>>> schema = stream.get_schema()
>>> learner = KNNRegressor(schema)
>>> results = prequential_evaluation(stream, learner, max_instances=1000)
>>> results["cumulative"].rmse()
2.9811398077838542
```

#### \_\_init_\_(schema=None, CLI=None, random_seed=1, k=3, median=False, window_size=1000)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/regressor/_knn.py#L30)

Constructing KNN Regressor.

* **Parameters:**
  * **k** – the number of the neighbours.
  * **median** – choose to use mean or median as the aggregation for the final prediction.
  * **window_size** – the size of the sliding window to store the instances.

#### cli_help()[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_regressor.py#L53)

#### *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)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_regressor.py#L59)

#### train(instance)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_regressor.py#L56)
