k-NN by hand: let the nearest neighbours vote
Click anywhere on the plane to move the new point ?. The circle grows until it holds k neighbours; they vote, and the majority wins.
What to notice
- Distance = √((x₁ − x₂)² + (y₁ − y₂)²): straight-line distance on the plane.
- Same data, different k, different answer: k = 1 says B, k = 3 says A.
- With an even k the vote can tie. scikit-learn then picks the class that comes first (A).
Try this
Decision boundaries: where the model's answer changes
Every pixel is coloured by the class the model would predict there. Models are trained on the lecture's training split of the iris data (70%), using only two measurements so we can draw them.
What to notice
- k = 1 draws jagged islands around single flowers: almost 100% on training data, worse on test data. (Not quite 100% with sepals: some flowers have identical measurements but different species.)
- Large k smooths the regions; very large k ignores real detail.
- A tree cuts the plane with straight horizontal and vertical lines, one question at a time.
Try this
Read the tree: follow the questions for one flower
A tree trained on all four measurements (the lecture's split). Move the sliders to describe a flower and watch its path light up.
What to notice
- Each box asks one yes/no question. Yes (≤) goes left, no goes right.
- The coloured bar shows the training flowers that reached the box; leaves answer with the majority.
- Nobody typed these rules: the tree chose the questions that made each side as pure as possible.
Try this
Compare the models: complexity, and the scale of the features
All four measurements, the lecture's split. Training accuracy always rewards complexity; test accuracy tells the truth.
k-NN: accuracy as k grows
Decision tree: accuracy as depth grows
Change the units of one measurement (cm → mm is × 10)
What to notice
- k = 1 and deep trees score 100% on training data: they memorise.
- k-NN measures distance, so a feature with bigger numbers dominates the vote. Trees only compare one feature with a threshold, so units do not matter.