mojolearn.KNeighborsRegressor#

class mojolearn.KNeighborsRegressor(n_neighbors=5, query_tile=0, *, weights='uniform', metric='euclidean', algorithm='brute', p=2)#

Regressor that averages the targets of the k nearest training points.

Each prediction is the mean of the n_neighbors nearest targets, or with weights='distance' the weighted mean sum(w * y) / sum(w) with w = 1 / d. Neighbor search is exact brute force, as in NearestNeighbors. Targets are converted to float32.

Parameters:
  • n_neighbors (int, default 5) – Number of neighbors averaged. Must be between 1 and the number of training samples.

  • query_tile (int, default 0) – As in NearestNeighbors: memory use only, never the answer.

  • weights ({‘uniform’, ‘distance’} or None, default ‘uniform’) – ‘uniform’ (or None) averages equally. ‘distance’ follows scikit-learn, including its rule for neighbors at distance exactly zero (they get weight 1, the rest of that row 0). A callable raises ValueError, as does ‘distance’ with metric='inner_product'.

  • metric (str, default ‘euclidean’) – As in NearestNeighbors with algorithm='brute'.

  • algorithm ({‘brute’, ‘auto’}, default ‘brute’) – Exact brute force. ‘rbc’ raises ValueError here; use NearestNeighbors(algorithm='rbc').kneighbors instead.

  • p (float, default 2) – As in NearestNeighbors.

  • numeric_mode ({‘identical’, ‘fast’}, optional) – As in NearestNeighbors. ‘fast’ is available on Apple GPUs only.

Variables:
  • outputs_2d_ (bool) – Whether y had more than one output column.

  • n_samples_fit_ (int) – Number of training samples.

  • n_features_in_ (int) – Number of features seen at fit.

  • used_query_tile_ (int) – Query batch size the last prediction ran with.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 8), dtype=np.float32)
>>> y = X.sum(axis=1)
>>> reg = mojolearn.KNeighborsRegressor(n_neighbors=5).fit(X, y)
>>> pred = reg.predict(X[:10])

Methods

fit(X, y)

Store the training points and their targets.

get_params([deep])

kneighbors(X[, n_neighbors, return_distance])

Find the nearest fitted points to each query, nearest first.

load(path)

Load a model written by save.

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Predict each query's target from its neighbors.

save(path)

Save the fitted model to an .npz file.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

save(path)#

Save the fitted model to an .npz file.

The file holds the training points, targets and parameters. mojolearn.host_model(path) can predict from it on a CPU with no GPU.

Parameters:

path (str or path-like) – Destination file.

Raises:

RuntimeError – If the model is not fitted.

classmethod load(path)#

Load a model written by save.

Parameters:

path (str or path-like) – File written by save.

Returns:

KNeighborsRegressor – A fitted model, ready for predict.

fit(X, y)#

Store the training points and their targets.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training points; converted to float32.

  • y (array-like, shape (n_samples,) or (n_samples, n_outputs)) – Numeric targets; converted to float32. A 2-D y is averaged column by column.

Returns:

self

predict(X)#

Predict each query’s target from its neighbors.

Parameters:

X (array-like of shape (n_queries, n_features)) – Query points; converted to float32.

Returns:

Array of float32, shape (n_queries,) or (n_queries, n_outputs) – The (weighted) mean target; 2-D when y was 2-D.

Raises:

ValueError – If the model is not fitted, X has the wrong number of features, or n_neighbors exceeds the number of training samples.

kneighbors(X, n_neighbors=None, return_distance=True)#

Find the nearest fitted points to each query, nearest first.

Distances are the chosen metric’s (‘euclidean’ returns true, not squared, distances). Ties in distance are broken by the lower index, matching scikit-learn’s layout and ordering.

On a GPU the fitted points are copied to the device at the first call after fit and reused by later calls. Changing the array passed to fit in place after that is not seen; call fit again instead (as with cuML, whose fit copies the data to the device).

Parameters:
  • X (array-like of shape (n_queries, n_features)) – Query points; converted to float32.

  • n_neighbors (int, optional) – Neighbors per query; defaults to the constructor’s n_neighbors. Must be between 1 and n_samples_fit_.

  • return_distance (bool, default True) – Return the distances as well as the indices.

Returns:

  • distances (Array of shape (n_queries, n_neighbors), float32) – Returned only when return_distance is True.

  • indices (Array of shape (n_queries, n_neighbors), int64) – Row indices into the fitted data.

Raises:

ValueError – If the model is not fitted, X has the wrong number of features, or n_neighbors is out of range.

numeric_mode_used()#

The numeric mode this estimator runs in.

Returns:

str – 'identical' or 'fast', read from the compiled binary the estimator uses, not from the argument it was given.

set_params(**params)#

Set constructor parameters. The estimator is rebuilt through its constructor, so every constructor check runs again and any fitted state is dropped, as a new setting invalidates it.

vendor_used()#

The GPU backend this estimator runs on.

Returns:

str – 'metal', 'cuda' or 'hip', read from the compiled binary the estimator uses.