MACHINE LEARNING • LESSON 10

KNN Classification

KNN classification predicts the category of a new data point by looking at its K nearest neighbors and using their classes to make a decision.

THE SIMPLEST IDEA

KNN asks: “Which nearby examples are most similar to this one?”

KNN finds the nearest data points, looks at their labels, and usually chooses the class that receives the most votes.

01

What Is KNN Classification?

KNN stands for K-Nearest Neighbors. It is a machine learning algorithm that can be used to classify a new data point.

Classification means predicting a category such as:

EMAIL Spam
STUDENT Pass
CUSTOMER Buy
IMAGE Cat

KNN makes the prediction by comparing the new example with examples it has already seen.

02

The KNN Classification Process

KNN classification can be understood in four simple steps.

01 New Data

Give KNN a new data point.

02 Calculate Distance

Find how far it is from known points.

03 Find K Neighbors

Select the closest points.

04 Vote

The majority class becomes the prediction.

03

Simple Example — Pass or Fail

Let's classify a new student as either Pass or Fail.

We use two features: Study Hours and Attendance.

NEW STUDENT [5 study hours, 88% attendance]

We don't know the result yet.

KNN compares this student with students whose results are already known.

04

Step 1 — Calculate the Distances

Suppose the new student has these distances from several known students:

STUDENT DISTANCE CLASS
Student A 1.2 Pass
Student B 2.0 Pass
Student C 2.8 Fail
Student D 5.4 Fail
Student E 7.1 Pass

Remember: smaller distance means closer.

05

Step 2 — Choose K

Suppose we choose:

K 3

We will use the three nearest students.

Looking at the distance table, the three closest students are:

NEIGHBOR 1 Student A

Distance = 1.2

NEIGHBOR 2 Student B

Distance = 2.0

NEIGHBOR 3 Student C

Distance = 2.8

K = 3 means KNN ignores the farther students and uses the 3 closest students for this prediction.
06

Step 3 — Let the Neighbors Vote

Now look at the classes of the three nearest neighbors.

STUDENT A Pass
STUDENT B Pass
STUDENT C Fail
PASS 2 votes
FAIL 1 vote

Pass has the majority, so KNN predicts:

PREDICTION Pass
07

Why Does the Majority Vote Work?

The basic idea is simple: similar data points often have similar labels.

If most of the nearby examples are classified as Pass, KNN assumes the new example is also likely to belong to the Pass group.

NEARBY EXAMPLES Pass • Pass • Fail
MAJORITY Pass
NEW PREDICTION Pass
08

What Happens When K Changes?

The prediction can change if we change K because a different number of neighbors will participate in the vote.

K = 1 Pass

Only the closest student votes.

K = 3 Pass

2 Pass vs 1 Fail.

K = 5 Depends on all 5

More students participate in the vote.

This is why choosing K is important. You learned about this on the previous page.

09

A Second Example — Fruit Classification

Let's use another example to make the idea clearer.

Suppose KNN needs to classify a new fruit as either an Apple or an Orange.

NEW FRUIT

Unknown class

NEIGHBOR 1 Apple

Very close

NEIGHBOR 2 Apple

Very close

NEIGHBOR 3 Orange

Very close

K = 3 Apple

Apple receives 2 votes and Orange receives 1 vote.

10

KNN Classification in One Picture

The entire process can be remembered like this:

NEW DATA
MEASURE Distance
SELECT K Neighbors
COUNT Class Votes
PREDICT Majority Class
11

What KNN Is Actually Learning

There is an important idea here. KNN does not create a simple equation like linear regression.

Instead, KNN keeps the training examples and uses them when a new prediction is requested.

TRAINING DATA Known examples + labels

KNN uses these examples as its reference.

NEW DATA Find nearby examples

Calculate distances and choose K neighbors.

PREDICTION Majority class

The most common class wins.

12

One Important Limitation

KNN depends heavily on the quality of the distance calculation.

If the features are badly prepared, the nearest neighbors may not actually be the most useful neighbors.

Good features + appropriate scaling + suitable K = much more useful KNN predictions.

This is why the data preparation topics from earlier lessons are important even when using a simple algorithm like KNN.

REMEMBER THIS

KNN classification is basically “find nearby examples and let them vote.”

KNN calculates the distance from the new point to known points, chooses the K closest points, checks their classes, and predicts the class with the majority vote.

New Point Distance K Neighbors Vote Prediction
QUICK CHECK

Check Your Understanding

What does K mean? The number of nearest neighbors used for the prediction.
What does KNN calculate first? The distance between the new data point and known data points.
Which points become neighbors? The points with the smallest distances.
How does KNN choose the class? In basic classification, the class with the majority of votes wins.
If K = 3 and the neighbors are Pass, Pass, Fail, what is the prediction? Pass, because Pass receives 2 out of 3 votes.
Why is distance important? Distance determines which examples KNN considers to be the nearest neighbors.
NEXT TOPIC

Build KNN With Python

Now that you understand how KNN classification works, we'll build the same process in Python using scikit-learn.