K-nearest neighbors
K-nearest neighbours predicts from nearby training examples. Its behaviour depends on the distance metric, feature scaling, and the number of neighbours.
Understand the idea
The classifier stores training observations and finds neighbours at prediction time. A smaller k can be sensitive to noise; a larger k can blur local structure. Fit scaling inside a training pipeline when magnitudes need normalization. Evaluate the complete pipeline with a suitable split rather than tuning k on the final test set.
Where to try it
Run locally with scikit-learn and NumPy installed in your virtual environment (py -m pip install scikit-learn numpy). These examples are executed during course review, but the online runner does not currently preload scikit-learn. The small data is for learning the API, not evidence of production model quality.
A worked example
from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier(n_neighbors=3)
model.fit([[1], [2], [3], [8], [9], [10]], [0, 0, 0, 1, 1, 1])
print(model.predict([[2], [9]]).tolist())
Read the result
The predictions are [0, 1] for points close to the two training clusters.
Try one change
Try a point near the boundary and compare several k values using training-only validation.
For package options, see the scikit-learn documentation.