Lecture 10 · Interactive lab

Classifier Lab

Vote with k nearest neighbours, see decision boundaries, and follow a decision tree's questions

Introduction to Data Science and Machine Learning
Md Mahbub E Noor · University of Barishal
All labs

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.

Clicking the plane

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.

training accuracy
test accuracy
setosaversicolorvirginicafilled = training flower, hollow = test flower

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)

k-NN (k = 5), test accuracy
tree (depth 3), test accuracy

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.

Try this

Works offline: no internet needed. The data and the trained models are built into this page. Every number was calculated with scikit-learn and checked, so it matches what you will see in Jupyter or Colab.