03 · CLASSIFICATION · EVALUATION

Classification Lab: Train and Evaluate kNN

Change the number of neighbours, inspect mistakes and investigate why a single accuracy number is never the whole story.

1. How k-nearest neighbours works

This small fictional sensor dataset includes soil moisture and temperature. The first 12 labelled records train a k-nearest neighbours (kNN) classifier; the final eight are held out for testing. A new record is classified by the majority label of its k closest training examples.

Fair comparison:

We divide temperature by 20 and soil-moisture percentage by 100 before calculating Euclidean distance. This reduces the impact of different numerical scales. The two scales are fixed teaching choices, not values learned from the test set.

Experiment with the number of neighbours

Test IDTemp (°C)Moisture (%)ActualPredictedOutcome

2. Understand the trade-offs

False positive

The classifier says a plant needs watering when the example's labelled outcome says it does not. That might waste water.

False negative

The classifier misses a labelled watering need. In some contexts, missing a genuine need might matter more than an unnecessary alert.

The labels are fictional examples, not horticultural advice. With only 12 training records and eight test records, metrics are unstable and cannot justify a deployed system.

3. Challenge and career connection

Compare all three values of k. Which errors change? Suggest how you would collect better data, decide who could be harmed by wrong predictions, and check whether the test set represents the environment where the model would be used.