MACHINE LEARNING • LESSON 10

What Is KNN?

K-Nearest Neighbors (KNN) is a Machine Learning algorithm that makes a prediction by looking at the closest known examples.

THE SIMPLEST IDEA

Look at the closest examples, then use them to make a prediction.

Instead of learning a complicated set of rules, KNN asks a simple question: "Which known examples are closest to this new example?"

01

What Does KNN Mean?

KNN stands for K-Nearest Neighbors.

K Number of neighbors

How many nearby examples should we look at?

NEAREST Closest

We look for examples that are closest to the new example.

NEIGHBORS Existing examples

The known data points around the new example.

KNN = Find the K closest examples and use them to make a prediction.
02

A Simple Real-Life Example

Imagine you move to a new neighborhood and want to know whether a nearby restaurant is likely to be expensive or affordable.

You look at restaurants near it and notice:

NEARBY RESTAURANT PRICE
Restaurant A Affordable
Restaurant B Affordable
Restaurant C Expensive

If most of the nearby restaurants are affordable, you might reasonably guess that the new restaurant is also affordable.

THE IDEA Similar nearby examples can help us understand a new example.
03

KNN Does Something Similar

KNN applies this same idea to data.

Suppose we already know the classes of several data points.

KNOWN DATA ● ● ●

Class A

KNOWN DATA ■ ■ ■

Class B

NEW DATA

Unknown class

KNN looks at the data points closest to the new point.

04

What Is a Neighbor?

In KNN, a neighbor is simply a known data point that is close to the new data point.

NEW DATA

We want to classify this point.

NEARBY DATA ● ● ●

These are its nearest neighbors.

"Near" means that the data points have similar feature values according to the distance measure being used.

05

What Does K Control?

The letter K tells KNN how many neighbors it should consider.

K = 1 Look at 1 neighbor

The closest example has the strongest influence.

K = 3 Look at 3 neighbors

The three closest examples can vote on the prediction.

K = 5 Look at 5 neighbors

The five closest examples can vote on the prediction.

K is simply the number of nearest neighbors KNN looks at.
06

A Simple KNN Prediction

Suppose we choose:

K 3

The three closest known examples to our new example are:

NEIGHBOR 1 Pass
NEIGHBOR 2 Pass
NEIGHBOR 3 Fail

Now count the votes:

PASS 2 votes
FAIL 1 vote
PREDICTION PASS

Because Pass received the majority of votes, KNN predicts Pass.

07

KNN Classification

KNN can be used for classification problems where the prediction is a category or class.

KNOWN EXAMPLES Pass / Fail

The model already knows the classes of the training examples.

NEW EXAMPLE Unknown

KNN finds its closest neighbors.

FINAL RESULT Pass or Fail

The neighbors vote on the class.

08

Student Example

Let's use the same type of student example we used with Decision Trees.

STUDY HOURS ATTENDANCE RESULT
2 60% Fail
3 70% Fail
5 85% Pass
6 90% Pass

Now imagine a new student:

STUDY HOURS 5
ATTENDANCE 88%

This new student is close to the students who studied around 5–6 hours and had high attendance.

KNN IDEA Nearby students are mostly Pass → predict Pass
09

KNN Does Not Build a Decision Tree

KNN works differently from the Decision Tree we just learned.

DECISION TREE Learns decision rules

Example: split the data using feature values.

KNN Looks at nearby examples

It uses the closest training examples when making a prediction.

This difference is important. KNN does not need to create a tree of decisions like the previous algorithm.

10

The Basic KNN Process

01 New Data

Give KNN an example it needs to classify.

02 Find Neighbors

Find the closest known examples.

03 Look at Labels

Check the classes of those neighbors.

04 Predict

Use the majority class for the prediction.

REMEMBER THIS

KNN predicts by looking at nearby examples.

The algorithm finds the K closest known data points to a new data point and uses their labels to make the prediction.

New Data Find K Neighbors Look at Labels Vote Prediction
QUICK CHECK

Check Your Understanding

What does KNN stand for? K-Nearest Neighbors.
What does K mean? The number of nearest neighbors considered for the prediction.
What is a neighbor? A known data point that is close to the new data point.
How does KNN classification work? It looks at the classes of the nearest neighbors and uses the majority class.
Does KNN create a Decision Tree? No. KNN uses nearby training examples to make predictions.
What is the main idea of KNN? Similar nearby examples can help predict the class of a new example.
NEXT TOPIC

How KNN Works

Next, we will go step by step through how KNN finds the nearest neighbors and uses them to make a prediction.