626 — kNN

Step 1 of 8

Introduction — What is kNN?

kNN (k-Nearest Neighbours) is a simple classification algorithm. It predicts the class of a new data point by looking at the k closest labelled points and taking a majority vote.

Check understanding

What does kNN use to classify new data?

  1. Random selection
  2. Nearest labelled neighbours
  3. Mathematical formula
Step 2 of 8

The dataset — two clusters

Our dataset has two clear clusters of points. Each point is labelled as either Class A or Class B, forming distinct groups in the space.

Check understanding

What pattern do you see in the dataset?

  1. Random scatter
  2. Two distinct clusters
  3. Single group
Step 3 of 8

Classifying with k = 1

With k = 1, the algorithm looks at only the single nearest neighbour. The new point gets classified as whatever class that nearest neighbour belongs to.

Check understanding

How many neighbours does k = 1 consider?

  1. All neighbours
  2. Just one
  3. Three neighbours
Step 4 of 8

Classifying with k = 3

With k = 3, the algorithm examines the three nearest neighbours and uses majority voting. If two are Class A and one is Class B, the prediction is Class A.

Check understanding

How does k = 3 make its prediction?

  1. Uses the closest point only
  2. Majority vote of 3 neighbours
  3. Averages all points
Step 5 of 8

Classifying with k = 7

With k = 7, we consider seven nearest neighbours. This larger k value makes the classification more stable and less sensitive to individual outlier points.

Check understanding

What advantage does a larger k provide?

  1. Faster computation
  2. More stable predictions
  3. Worse accuracy
Step 6 of 8

Decision boundary with k = 1

The decision boundary shows which regions would be classified as A or B. With k = 1, the boundary is very jagged because it reacts to every single nearby point.

Check understanding

Why is the k = 1 boundary jagged?

  1. It uses all points
  2. It reacts to individual points
  3. It ignores close points
Step 7 of 8

Decision boundary with k = 7

With k = 7, the boundary becomes much smoother. Instead of following every local variation, it captures the general separation between the two clusters.

Check understanding

What does a smooth boundary indicate?

  1. Over-fitting to noise
  2. General pattern recognition
  3. Random classification
Step 8 of 8

Comparing k = 1 vs k = 7

Side-by-side comparison shows the key trade-off: small k is sensitive to local details (can overfit), while large k focuses on broader patterns (more generalizable). The transition zone between clusters shows this most clearly.

Check understanding

In the ambiguous middle region, which k is more stable?

  1. k = 1 (more detail)
  2. k = 7 (averages neighbors)
  3. Both are equal