K-nearest neighbors classifier — a vote of the closest rows
KNN stores the training rows and, for a new row, finds the k closest ones and takes a vote. There is no fitted equation. The model is the table, which is why prediction gets slower as the table grows and why a duplicate row counts as an extra vote.
Distance is meaningless across columns in different units, so scale first or the column with the largest numbers decides the neighbors. k=1 reproduces the training labels and copies their noise. k equal to the whole table predicts the majority class for everyone. The useful range is small and odd, so votes do not tie.
Weights change the vote from a headcount to a distance-weighted sum. Inverse-distance weights let the nearest neighbor outvote a crowd of far ones, which helps when the classes form tight pockets.
Settings that change this fit
- k
- Neighbors in the vote. Small k follows local pockets and noise. Large k smooths toward the global majority.
- metric
- Euclidean after scaling is the default. Manhattan is steadier when a few coordinates are occasionally huge.