QuerEd.com · Machine Learning

K-Nearest Neighbours (KNN)

Understand the complete KNN process using a simple cat-and-dog image example — from preparing the dataset to making a prediction.

1. What is KNN?

K-Nearest Neighbours (KNN) is a supervised machine learning algorithm used for classification and regression.

It makes predictions by finding the most similar examples in an existing labelled dataset.

Simple idea: Find the examples most similar to the new example, look at their answers, and use them to make a prediction.

What we will do

  1. Prepare a labelled dataset.
  2. Receive a new image.
  3. Calculate distances.
  4. Find the K nearest neighbours.
  5. Use majority voting to predict its label.

2. The KNN process, step by step

Imagine we have images of cats and dogs and want to classify a new image.

01

Prepare the dataset

First, we need a dataset containing labelled images. We identify characteristics (features) of each image and represent them as numbers.

This process is called Feature Engineering when we manually define and prepare the features.

ImageEar shapeEye sizeLabel
Image 134Cat
Image 223Cat
Image 352Dog
Image 441Dog
Image 533Cat

Important terminology:

  • Features: Numerical characteristics describing the image.
  • Labels: The correct categories, such as Cat or Dog.
  • Dataset: The collection of labelled examples.

The developer defines the features and measurement rules. There is no universal standard for manually chosen feature values.

02

Receive a new image

Now imagine we receive another image that we want to classify.

We apply exactly the same feature extraction process used for the existing dataset.

New imageEar shapeEye sizeLabel
Image 633.5Unknown

Notice that we have not assigned a label because we want KNN to predict it.

03

Calculate Euclidean distance

KNN compares the new image against every record in the existing dataset. It calculates the distance between their numerical features.

For example: New image: (3, 3.5). Existing image: (3, 4).

KNN uses a mathematical formula called Euclidean Distance.

d = √[(x₁ − x₂)² + (y₁ − y₂)²]
Euclidean distance for two features

Applying our values:

d = √[(3 − 3)² + (3.5 − 4)²]
d = √[0 + 0.25]
d = 0.5

The distance between these two records is 0.5.

04

Repeat the distance calculation

Suppose the existing dataset contains 100 images.

KNN calculates the Euclidean distance between the new image and all 100 existing images.

We now have 100 calculated distances.

05

Select the K value

K is a hyperparameter that determines how many nearest records we consider.

K = 4

This means we want the four nearest records.

The computer:

  1. Sorts all calculated distances from smallest to largest.
  2. Selects the four smallest distances.
  3. Retrieves the labels of those four records.
06

Perform majority voting

Suppose the four nearest records are:

ImageDistanceLabel
Image 10.5Cat
Image 20.7Cat
Image 30.9Dog
Image 41.2Cat

Count the labels:

  • Cat = 3
  • Dog = 1
Final prediction

CAT

The new image is classified as a Cat because three of its four nearest neighbours are labelled Cat.

3. Complete KNN workflow

1. Prepare labelled datasetImages + numerical features + labels
↓
2. Receive new imageExtract the same features
↓
3. Calculate Euclidean distancesCompare against every dataset record
↓
4. Find K nearest neighboursSort distances and select the smallest K
↓
5. Majority votingCount the labels of the nearest neighbours
↓
6. Predict the new image's labelCat or Dog

4. How do we select the best K value?

We don't necessarily know which K value will give the best predictions.

We can test different values:

We evaluate each value using separate validation data and compare their performance.

This process is called Hyperparameter Tuning.

In practice, Python can automate this process.

5. The five important terms to remember

Feature Engineering
Defining and preparing useful features
Dataset
Collection of labelled examples
Euclidean Distance
Mathematical measurement of distance
K
Number of nearest neighbours
Majority Voting
Choosing the most common label
This explanation covers KNN classification using the image example we discussed. KNN can also be used for numerical predictions (regression).