Overview
TheKNeighborsClassifier implements the k-nearest neighbors vote for classification. It predicts the class of a sample based on the majority class among its k nearest neighbors in the training set.
Constructor
Parameters
Number of neighbors to use for classification. Must be a positive integer.
Methods
fit()
Fit the k-nearest neighbors classifier from the training dataset.Training data matrix where each row is a sample and each column is a feature.
Target values (class labels) for the training data.
Sample weights (currently not implemented but reserved for future use).
this - The fitted classifier instance.
Throws:
- Error if
nNeighborsexceeds the training set size - Error if input validation fails
predict()
Predict the class labels for the provided data.Test samples to predict.
Vector - Predicted class labels.
predictProba()
Return probability estimates for the test data.Test samples to predict.
Matrix - Probability of each class for each sample. Each row represents a sample, and each column represents a class probability.
score()
Return the mean accuracy on the given test data and labels.Test samples.
True labels for the test samples.
number - Mean accuracy score.
Attributes
Unique class labels identified during training.
Examples
Basic Classification
Multi-class Classification with Probabilities
Model Evaluation
Notes
- Uses squared Euclidean distance for efficiency (avoids square root computation)
- Stores the entire training dataset (instance-based learning)
- Prediction time increases with training set size
- Works best when features are on similar scales
- The algorithm uses uniform weighting (all neighbors contribute equally)