MACHINE LEARNING • LESSON 10

How KNN Works

KNN makes a prediction by finding the data points closest to a new example and using their labels to decide the answer.

THE SIMPLEST IDEA

KNN finds the closest examples, checks their labels, and lets them vote.

The process is simple: give KNN a new data point, find its nearest neighbors, look at their classes, and use the majority class as the prediction.

01

The KNN Process

When KNN receives a new data point, it follows a sequence of steps to make its prediction.

01 New Data

Give KNN a data point that needs a prediction.

02 Find Neighbors

Find the known points closest to the new point.

03 Check Labels

Look at the classes of those neighbors.

04 Vote

Count how many neighbors belong to each class.

05 Predict

The majority class becomes the prediction.

02

Step 1 — Start With a New Data Point

First, we have a new data point whose class is unknown.

Let's use a student example with two features:

STUDY HOURS 5
ATTENDANCE 88%
NEW STUDENT [5, 88]

We don't know yet whether this student will Pass or Fail.

03

Step 2 — Look at the Known Data

KNN already has examples where the correct class is known.

STUDY HOURS ATTENDANCE RESULT
2 60% Fail
3 70% Fail
4 75% Fail
5 85% Pass
6 90% Pass
7 95% Pass

KNN compares the new student with these known examples to find which ones are closest.

04

Step 3 — Find the Nearest Neighbors

Now KNN determines which known students are closest to the new student.

NEW STUDENT 5 hours / 88%
CLOSE EXAMPLES 5 / 85% 6 / 90% 7 / 95%

These examples are closer to the new student than the students with much lower study hours and attendance.

KNN does not simply choose random examples. It uses a distance measure to determine which examples are closest.
05

Step 4 — Choose K Neighbors

K tells KNN how many nearby examples it should use.

K = 1 1 nearest neighbor
K = 3 3 nearest neighbors
K = 5 5 nearest neighbors

For this example, let's choose:

K 3

That means KNN will use the 3 closest students to make the prediction.

06

Step 5 — Check Their Labels

Now KNN looks at the known labels of the three nearest neighbors.

NEIGHBOR 1 Pass

5 hours / 85%

NEIGHBOR 2 Pass

6 hours / 90%

NEIGHBOR 3 Pass

7 hours / 95%

All three nearest neighbors are labeled Pass.

07

Step 6 — Let the Neighbors Vote

For classification, the nearest neighbors can vote on the class of the new data point.

PASS 3 votes
FAIL 0 votes
WINNER PASS

Pass has the majority of votes, so KNN predicts:

PREDICTION Pass
08

The Same Process With Mixed Neighbors

The neighbors do not always have the same label. That is where majority voting becomes important.

Suppose K is still 3, but the closest neighbors are:

NEIGHBOR 1 Pass
NEIGHBOR 2 Fail
NEIGHBOR 3 Pass

Count the labels:

PASS 2 votes
FAIL 1 vote
RESULT Pass

Pass wins because it has more votes than Fail.

09

Why Does "Nearest" Matter?

Imagine asking ten random students whether a new student will pass. Their answers may not tell us much.

But if we look at students who are similar to the new student, their outcomes can be much more useful.

NEARBY EXAMPLES More relevant

Their feature values are similar to the new example.

FAR EXAMPLES Less relevant

Their feature values are less similar to the new example.

KNN gives the most attention to the examples that are closest to the new data point.
10

KNN in One Example

Let's put everything together.

NEW DATA Student: 5 hours, 88%
CHOOSE K K = 3
FIND NEAREST 3 closest students
CHECK LABELS Pass, Pass, Fail
MAJORITY VOTE Pass
11

KNN Does Not Use All Data Equally

This is an important idea.

KNN has access to many training examples, but for a particular prediction it focuses on the nearest examples rather than treating every point as equally relevant.

CLOSE Strong influence
FARTHER AWAY Usually less relevant
12

One Important Detail: Distance

KNN needs a way to decide which points are close and which points are far away.

It does this using a distance measure.

FOR NOW, REMEMBER Smaller distance → closer point Larger distance → farther point

We will look at exactly how this distance is calculated in the next part of the lesson: Distance Between Data Points.

REMEMBER THIS

KNN = Find → Select → Check → Vote → Predict

KNN finds the closest known examples, selects the requested number of neighbors, checks their labels, and uses the majority class to make the prediction.

New Data Find Closest Select K Check Labels Vote Prediction
QUICK CHECK

Check Your Understanding

What is the first thing KNN receives? A new data point that needs a prediction.
How does KNN find neighbors? It uses a distance measure to determine which known points are closest.
What does K control? How many nearest neighbors are used for the prediction.
Why do neighbors have labels? Their known labels are used to determine the prediction for the new point.
What happens when K = 3? KNN considers the three closest known data points.
How is the final class chosen? In basic KNN classification, the majority class among the selected neighbors wins.
NEXT TOPIC

Choosing K

Now that you understand how KNN works, the next question is important: how do we decide how many neighbors KNN should use?