Skip to Content
OchreML
Clustering, Neighbors & NB

KNeighborsRegressor

Non-parametric k-nearest neighbors regressor supporting 1D and multi-output 2D targets with uniform or distance weighting.

Overview & Backend Support

KNeighborsRegressor predicts continuous target values by averaging (uniformly or weighted by inverse distance) the targets of the n_neighbors closest training samples. Both single-output 1D targets (n_samples,) and multi-output 2D targets (n_samples, n_outputs) are supported natively.

  • Backend primitives: N3 (pairwise_l2_sq) and N4 (topk_argmin) across CPU (Rayon), CUDA, wgpu, and Google TPU for Euclidean / Minkowski (p=2) queries; "manhattan"/"l1", "chebyshev", "cosine", and general Minkowski p are also supported.
  • Incremental learning (partial_fit): Not supported.
  • Persistence: Supports binary .dit serialization via save(path), KNeighborsRegressor.load(path, device=...), ochreml.save(), and ochreml.load().

Import & Signature

from ochreml.neighbors import KNeighborsRegressor
# or lazily from top-level:
from ochreml import KNeighborsRegressor

KNeighborsRegressor(
    n_neighbors: int = 5,
    *,
    weights: str = "uniform",
    metric: str = "minkowski",
    p: float = 2.0,
    device: str | None = None,
)

Parameters

ParameterTypeDefaultDescription
n_neighborsint5Number of nearest neighbors used by default for regression and kneighbors queries.
weights{"uniform", "distance"}"uniform"Weight function used in averaging neighbor targets.
metric{"minkowski", "euclidean", "l2", "manhattan", "l1", "chebyshev", "cosine"}"minkowski"Distance metric used for neighbor search.
pfloat2.0Power parameter for the Minkowski metric.
devicestr | NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeTypeDescription
n_features_in_int | NoneNumber of features seen during fit.
n_samples_fit_int | NoneNumber of indexed training samples.
device_str | NoneResolved hardware device string used by the underlying Rust model.

Methods

MethodSignatureDescription
fitfit(X, y) -> KNeighborsRegressorIndex training features X and targets y ((n_samples,) or (n_samples, n_outputs)).
predictpredict(X) -> ndarrayPredict target values of shape (n_queries,) (1D) or (n_queries, n_outputs) (multi-output).
scorescore(X, y) -> floatReturn the coefficient of determination (R^2) on (X, y).
kneighborskneighbors(X=None, n_neighbors=None, return_distance=True)Return (distances, indices) or indices of shape (n_queries, k). If X is None, queries the training set excluding each point itself.
savesave(path: str) -> NoneSave the fitted regressor to a binary .dit file.
loadKNeighborsRegressor.load(path: str, device: str | None = None) -> KNeighborsRegressorLoad a fitted regressor from a binary .dit file.
toto(device: str) -> KNeighborsRegressorMove estimator compute target to another hardware device.

Example

import numpy as np
from ochreml.neighbors import KNeighborsRegressor

X = np.array([[0.0], [1.0], [2.0], [3.0]], dtype=np.float64)
y = np.array([0.0, 1.0, 4.0, 9.0], dtype=np.float64)

reg = KNeighborsRegressor(n_neighbors=2, weights="distance", metric="euclidean")
reg.fit(X, y)

X_test = np.array([[0.5], [2.5]], dtype=np.float64)
print("Predictions:", reg.predict(X_test))
print("R^2 on train:", reg.score(X, y))

On this page