Skip to main content
Module 5 · Lesson 2 of 622/56 in the course~20 min
Module lessons (2/6)

k-nearest neighbours

Lesson preview5 hands-on exercises readyMachine Learning: from first principles to practice · Module 5 — Classification

Distance-based voting, scaling and choosing k.

What you'll learn in this lesson

  • Explain how k-nearest neighbours classifies a point.
  • Show why feature scaling matters for distance-based models.
  • Choose k with cross-validation.

Overview

To classify a new point, look at the k closest training points and let them vote. No formula is learned: the model simply remembers the training data. It is the most intuitive classifier there is.

Start on the Two moons dataset with k-nearest neighbours and sweep k from 1 up to 15. Small k wraps around isolated noisy outliers; moderate k tracks the two interleaving crescents smoothly.

Sections covered in this lesson

  1. 1The hook: ask your neighbours
  2. 2Try it: change k and the model
  3. 3Comparing choices of k and distance weighting
  4. 4Distances need comparable scales
  5. 5Progressive examples: distance lookup and scaling
  6. 6Practice
Pro Lesson · Module 5 — Classification

Unlock "k-nearest neighbours" and the full Machine Learning: from first principles to practice curriculum

Distance-based voting, scaling and choosing k.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Machine Learning: from first principles to practice plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.