Machine Learning, FoundationsLesson 58 of 92

Module 6 · Trees, ensembles, and the algorithms that win on tables

k-nearest neighbours, and why distance stops meaning anything

In this lesson

  • 8 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 6 of 10 in this chapter

This chapter’s goalChoose between a linear model, a random forest and a gradient boosting library for a given tabular problem and defend the choice, tune the four hyperparameters that actually matter in each, and explain why impurity-based feature importance is biased and what permutation importance and SHAP replace it with.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:59Not available
Full audiobookNot availableNot available
How far the farthest point is, relative to the nearest2 dimensions6010 dimensions4100 dimensions1.61,000 dimensions1.2farthest distance divided by nearest distance1,000 random points in a unit cube. In two dimensions the farthest neighbour is sixty times furtheraway than the nearest. By 1,000 dimensions it is only 20 per cent further, and the word neighbour hasstopped picking anything out. This is geometry, and it affects every method built on distance.
1,000 random points in a unit cube. In two dimensions the farthest neighbour is sixty times further away than the nearest. By 1,000 dimensions it is only 20 per cent further, and the word neighbour has stopped picking anything out. This is geometry, and it affects every method built on distance.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

The model with no training

Store the data. To predict, find the k closest training rows and take their average or their majority vote.

Python
from sklearn.neighbors import KNeighborsClassifier
KNeighborsClassifier(n_neighbors=15, weights="distance")

There is no fitting step: fit just stores the array. All the work happens at prediction time, which inverts the usual cost profile: instant training, slow predictions, and the entire training set has to live in memory forever.

k-NN is worth understanding for three reasons beyond its own use. It is the clearest possible illustration of the curse of dimensionality; it is the mechanism underneath every embedding search in module 7; and it makes the assumption behind most machine learning explicit: that things close together in feature space have similar targets. Every model assumes some version of that. k-NN says it out loud.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the How a Language Model Actually Works exam.

Stuck on something?Ask Funke, your AI tutor

How to cite: Addaly (2026) ‘k-nearest neighbours, and why distance stops meaning anything’, in Machine Learning, Foundations. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/machine-learning-foundations/nearest-neighbours (Accessed: 29 November 2026).