Introduction to Machine Learning

by Albert Major


Introduction to K-Nearest Neighbors (KNN)

Screenshot 2025-12-10 at 19.46.55.png

The Hyperparameter 'k'

The choice of $k$ (the number of neighbors) is a critical hyperparameter that controls the model's behavior.

This diagram illustrates a 1-Nearest Neighbor (1-NN) classifier, where new data points (squares) are assigned the class label of their single closest neighbor. The arrows indicate the specific nearest training point (red triangle or blue circle) that determines the classification for each test instance.

This diagram illustrates a 1-Nearest Neighbor (1-NN) classifier, where new data points (squares) are assigned the class label of their single closest neighbor. The arrows indicate the specific nearest training point (red triangle or blue circle) that determines the classification for each test instance.

These plots demonstrate that as k increases, the decision boundary becomes smoother and less complex. This visualizes the transition from high variance (overfitting) at k=1 to high bias (underfitting) at higher k values.

These plots demonstrate that as k increases, the decision boundary becomes smoother and less complex. This visualizes the transition from high variance (overfitting) at k=1 to high bias (underfitting) at higher k values.

Distance Metrics

KNN relies entirely on calculating the "distance" between points to determine similarity.