Skip to content

KNeighborsClassifier

Module: strata.neighbors • Kind: struct • Traits: Classifier, Copyable, Movable
Source: strata/neighbors/classification.mojo

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

Classifier implementing the k-nearest neighbors vote.

Supports uniform voting and inverse-distance weighted voting:

  • Uniform weights:
P(y=c∣x)=1K∑i∈NK(x)I(yi=c)P(y = c \mid x) = \frac{1}{K} \sum_{i \in N_K(x)} \mathbb{I}(y_i = c)
  • Distance weights:
wi=1d(x,xi),P(y=c∣x)=∑i∈NK(x)wiI(yi=c)∑i∈NK(x)wiw_i = \frac{1}{d(x, x_i)}, \quad P(y = c \mid x) = \frac{\sum_{i \in N_K(x)} w_i \mathbb{I}(y_i = c)}{\sum_{i \in N_K(x)} w_i}
Parameter Description
compute_dtype Precision used for internal distance calculations. 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 KNeighborsClassifier.

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
classes_ Distinct class labels matrix of shape (n_classes,).
n_classes_ Number of distinct classes seen during fit.
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
KNeighborsClassifier.fit() Fit the k-nearest neighbors classifier from the training dataset.
KNeighborsClassifier.predict_proba() Return probability estimates for the test data X.
KNeighborsClassifier.predict() Predict the class labels 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 classifier 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 class labels 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_proba[feat_dtype: DType](
self,
X: Matrix[feat_dtype],
) -> Matrix[feat_dtype]
def predict_proba[feat_dtype: DType, target_dtype: DType](
self,
dataset: Dataset[feat_dtype, target_dtype],
) -> Matrix[ feat_dtype ]

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

Return probability estimates for the test data X.

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: Matrix[feat_dtype] — Matrix[feat_dtype]: Probabilities of shape (n_queries, n_classes).


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

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

Predict the class labels 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[Int] — List[Int]: Predicted class label for each sample.

Section titled “Returns: List[Int] — List[Int]: Predicted class label for each sample.”
from strata.neighbors import KNeighborsClassifier
from strata.core import Matrix
var clf = KNeighborsClassifier[DType.float64](n_neighbors=3, weights="distance")
clf.fit(X_train, y_train)
var y_pred = clf.predict(X_test)
var proba = clf.predict_proba(X_test)