Skip to content

KNeighborsRegressor

Module: strata.neighbors • Kind: struct • Traits: Copyable, Movable, Regressor
Source: strata/neighbors/regression.mojo

struct KNeighborsRegressor[compute_dtype: DType = DType.float64](Copyable, Movable, Regressor)
from strata.neighbors import KNeighborsRegressor

Regression based on k-nearest neighbors.

Predicts the target value for query points by local interpolation:

  • Uniform weights:
y^(x)=1K∑i∈NK(x)yi\hat{y}(x) = \frac{1}{K} \sum_{i \in N_K(x)} y_i
  • Distance weights:
wi=1d(x,xi),y^(x)=∑i∈NK(x)wiyi∑i∈NK(x)wiw_i = \frac{1}{d(x, x_i)}, \quad \hat{y}(x) = \frac{\sum_{i \in N_K(x)} w_i y_i}{\sum_{i \in N_K(x)} w_i}
Parameter Description
compute_dtype Computational precision for distance arithmetic. Default DType.float64.

def __init__(
out self,
n_neighbors: Int = 5,
weights: String = "uniform",
algorithm: String = "auto",
metric: String = "euclidean",
p: Float64 = 2.0,
)

Initialize KNeighborsRegressor.

Argument Type Description
n_neighbors Int Number of nearest neighbors (>= 1). Default 5.
weights String Voting weight policy (‘uniform’, ‘distance’). Default ‘uniform’.
algorithm String Neighbor search algorithm (‘auto’, ‘brute’). Default ‘auto’.
metric String Distance metric identifier. Default ‘euclidean’.
p Float64 Minkowski metric exponent (>= 1.0). Default 2.0.

Attribute Description
n_samples_fit_ Number of samples in the fitted data.
n_features_in_ Number of features seen during fit.
is_fitted Boolean flag indicating if estimator has been fitted.

Method Description
KNeighborsRegressor.fit() Fit the k-nearest neighbors regressor from the training dataset.
KNeighborsRegressor.predict() Predict the continuous target values for the provided data.

def fit[feat_dtype: DType, target_dtype: DType](
mut self,
X: Matrix[feat_dtype],
y: List[Scalar[target_dtype]],
)
def fit[feat_dtype: DType, target_dtype: DType](
mut self,
dataset: Dataset[feat_dtype, target_dtype],
)

Overload Note: This method accepts either standard (X, y) inputs or a unified dataset: Dataset container.

Fit the k-nearest neighbors regressor from the training dataset.

Parameter Type Description
X Matrix[feat_dtype] Feature matrix of shape (n_samples, n_features).
y List[Scalar[target_dtype]] Target continuous values vector of length n_samples.
dataset Dataset[feat_dtype, target_dtype] Dataset container holding feature matrix and targets. (Can be provided alternatively in place of (X, y))

def predict[feat_dtype: DType](
self,
X: Matrix[feat_dtype],
) -> List[Scalar[feat_dtype]]
def predict[feat_dtype: DType, target_dtype: DType](
self,
dataset: Dataset[feat_dtype, target_dtype],
) -> List[ Scalar[feat_dtype] ]

Overload Note: This method accepts either standard (X, y) inputs or a unified dataset: Dataset container.

Predict the continuous target values for the provided data.

Parameter Type Description
X Matrix[feat_dtype] Query feature matrix of shape (n_queries, n_features).
dataset Dataset[feat_dtype, target_dtype] Dataset container holding feature matrix and targets. (Can be provided alternatively in place of (X, y))

Returns: List[Scalar[feat_dtype]] — List[Scalar[feat_dtype]]: Predicted regression values.

Section titled “Returns: List[Scalar[feat_dtype]] — List[Scalar[feat_dtype]]: Predicted regression values.”
from strata.neighbors import KNeighborsRegressor
from strata.core import Matrix
var reg = KNeighborsRegressor[DType.float64](n_neighbors=3, weights="distance")
reg.fit(X_train, y_train)
var y_pred = reg.predict(X_test)