Labs ICT
โญ Pro Login

K-Nearest Neighbors

Classifying based on what's closest to you.

Simple Yet Powerful

K-Nearest Neighbors (KNN) is one of the simplest ML algorithms. It doesn't learn an explicit model โ€” instead, it stores the entire training dataset and makes predictions by looking at the K closest data points. It's instance-based learning: the algorithm remembers everything and classifies new points by majority vote of their neighbors.

How KNN Works


  K=3: Look at 3 nearest neighbors

        ? โ† New point to classify

  โ—‹ โ—‹ โ— โ—† โ— โ—‹ โ—‹
  โ—‹ โ— โ— โ—† โ— โ— โ—‹
  โ— โ— โ— โ—† โ— โ— โ—

  Nearest 3 neighbors of โ—†:
  โ— โ— โ—

  Majority: โ— โ†’ New point classified as โ—

  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

  K=5: Look at 5 nearest neighbors

  Nearest 5 neighbors of โ—†:
  โ— โ— โ— โ—‹ โ—‹

  Majority: โ— โ†’ Still classified as โ—

Choosing K

  • Small K (e.g., K=1) โ€” Very sensitive to noise. A single outlier can change the prediction. Low bias, high variance.
  • Large K (e.g., K=20) โ€” Smoother decision boundaries, less sensitive to noise. But may miss local patterns. High bias, low variance.
  • Rule of thumb โ€” Start with K = โˆšn (square root of the number of training samples) and tune from there.

Distance Metrics

KNN relies on a distance metric to find "nearest" neighbors:

  • Euclidean Distance โ€” Straight-line distance (most common)
  • Manhattan Distance โ€” Sum of absolute differences (city blocks)
  • Minkowski Distance โ€” Generalization of both

Important: Features must be normalized/scaled before using KNN, or features with larger ranges will dominate the distance calculation.

Pros and Cons

Pros: Simple to understand and implement, no training phase (lazy learning), naturally handles multi-class problems, adapts to new data easily.

Cons: Slow at prediction time (must compute distances to all training points), memory-intensive (stores entire dataset), struggles with high-dimensional data, sensitive to irrelevant features and unscaled data.

When to Use KNN

KNN works well for small to medium-sized datasets with clear local structure. It's commonly used for recommendation systems, pattern recognition, and as a baseline for more complex models. For large datasets, consider approximate nearest neighbor algorithms (like KD-Trees or Ball Trees) to speed things up.

๐Ÿงช Quick Quiz

How does KNN classify a new data point?