KNNRegressor¶
K-Nearest Neighbors regressor.
This non-parametric regression method keeps track of the last window_size
training samples. Predictions are obtained by aggregating the values of the closest n_neighbors stored-samples with respect to a query sample.
Parameters¶
-
n_neighbors (int) – defaults to
5
The number of nearest neighbors to search for.
-
window_size (int) – defaults to
1000
The maximum size of the window storing the last observed samples.
-
aggregation_method (str) – defaults to
mean
The method to aggregate the target values of neighbors. | 'mean' | 'median' | 'weighted_mean'
-
min_distance_keep (float) – defaults to
0.0
The minimum distance (similarity) to consider adding a point to the window. E.g., a value of 0.0 will add even exact duplicates. Default is 0.05 to add similar but not exactly the same points.
-
distance_func (Callable[[Any, Any], float]) – defaults to
None
An optional distance function that should accept an a=, b=, and any custom set of kwargs (defined in distance_func_kwargs). If not defined, the default Minkowski distance is used.
Examples¶
>>> from river import datasets, neighbors
>>> from river import evaluate, metrics
>>> dataset = datasets.TrumpApproval()
>>> model = neighbors.KNNRegressor(window_size=50)
>>> for x, y in dataset.take(100):
... model = model.learn_one(x, y)
>>> for x, y in dataset.take(1):
... model.predict_one(x)
41.839342
Methods¶
clone
Return a fresh estimator with the same parameters.
The clone has the same parameters but has not been updated with any data. This works by looking at the parameters from the class signature. Each parameter is either - recursively cloned if it's a River classes. - deep-copied via copy.deepcopy
if not. If the calling object is stochastic (i.e. it accepts a seed parameter) and has not been seeded, then the clone will not be idempotent. Indeed, this method's purpose if simply to return a new instance with the same input parameters.
learn_one
Learn a set of features x
and optional class y
. Parameters: x: A dictionary of features. y: A class (optional if known). extra: an optional list or tuple of features to store Returns: self
Parameters
- x
- y – defaults to
None
- extra ([typing.Tuple,
] ) – defaults toNone
predict_one
Predict the target value of a set of features x
.
Search the window for the n_neighbors
nearest neighbors. Return a default prediction if the size of the window is 0 (no neighbors yet)
Parameters
- x
Returns
The prediction.
Notes¶
See the NearestNeighbors documentation for details about the base model, along with KNNBase for an example of providing your own distance function.