Skip to content

KNN (K-Nearest Neighbors)

The K-nearest-neighbors method. The model does not look for a general formula: it simply looks at the \(K\) most similar objects from the training set and takes their answer.

How It Works

  1. Every object is a point in the space of features.
  2. Compute the distance to every training object (metric: Euclidean, Manhattan and others).
  3. Take the \(K\) nearest ones.
  4. For classification — majority voting; for regression — averaging.

About \(K\)

\(K\) is a hyperparameter:

  • a small \(K\) → sensitivity to noise, risk of overfitting;
  • a large \(K\) → heavy smoothing, risk of underfitting.

Specifics

  • before running, the features are usually scaled;
  • in high-dimensional spaces it performs worse because of the curse of dimensionality;
  • there is no training as such: the model is 'lazy' — all the computation happens at inference time.

See Also

← All Terms