MACHINE LEARNING • LESSON 10

Distance Between Data Points

KNN needs a way to decide which data points are close and which are far away. It does this by measuring the distance between data points.

THE SIMPLEST IDEA

Smaller distance means the data points are closer.

KNN compares a new data point with known data points. The points with the smallest distances become its nearest neighbors.

01

Why Does KNN Need Distance?

Remember what KNN does:

NEW DATA

A new example needs a prediction.

FIND NEIGHBORS ● ● ●

Find the closest known examples.

PREDICTION Pass

Use the neighbors to make a prediction.

But KNN cannot simply say "this point looks close." It needs a mathematical way to measure closeness.

Distance gives KNN a measurable way to decide which data points are nearest.
02

Think of a Map

Imagine two people standing on a map. If they are standing close together, the distance between them is small.

If they are far apart, the distance is large.

POINT A

Location A

DISTANCE ─────────
POINT B

Location B

KNN uses the same basic idea with data. Instead of physical locations, it compares the values of the features.

03

Data Points Have Features

Suppose we want to predict whether a student will Pass or Fail.

We use two features:

FEATURE 1 Study Hours

How many hours the student studies.

FEATURE 2 Attendance

The student's attendance percentage.

A student can therefore be represented by two numbers.

DATA POINT [5, 88]

5 study hours and 88% attendance.

04

A Simple Distance Example

Suppose our new student is:

STUDY HOURS 5
ATTENDANCE 88%

Now compare this student with two known students.

STUDENT STUDY ATTENDANCE RESULT
New Student 5 88% ?
Student A 5 90% Pass
Student B 2 60% Fail

Student A is clearly more similar to the new student than Student B.

NEW STUDENT → STUDENT A Small distance

Their feature values are very similar.

NEW STUDENT → STUDENT B Larger distance

Their feature values are much more different.

05

Distance in Two Dimensions

With two features, we can think of each data point as a position on a simple graph.

Attendance
Study Hours
New
A
B

The new point is closer to point A than point B. Therefore, point A is a better candidate for being one of the nearest neighbors.

06

Euclidean Distance

One common way to measure distance is called Euclidean distance.

It is basically the straight-line distance between two points.

EUCLIDEAN DISTANCE d = √((x₂ − x₁)² + (y₂ − y₁)²)

Don't worry about memorizing the formula yet. The important idea is what each part means:

x First feature

For example, study hours.

y Second feature

For example, attendance.

d Distance

How far apart the two points are.

07

Calculate a Simple Distance

Let's use a very simple example so the calculation is easy to follow.

POINT A (2, 3)
POINT B (5, 7)

Apply the Euclidean distance formula:

Step 1 (5 − 2)²

= 3² = 9

Step 2 (7 − 3)²

= 4² = 16

Step 3 9 + 16

= 25

Step 4 √25

= 5

FINAL DISTANCE 5

So the Euclidean distance between (2, 3) and (5, 7) is 5.

08

Smaller Distance vs Larger Distance

Suppose our new point has three possible neighbors.

DATA POINT DISTANCE MEANING
Point A 2.1 Very close
Point B 4.7 Farther away
Point C 8.5 Much farther
SMALL DISTANCE Closer
LARGE DISTANCE Farther away

Therefore, Point A is the nearest of these three points.

09

How Distance Helps KNN

Now connect distance back to the KNN process.

01 New Point

KNN receives a new data point.

02 Calculate Distance

Compare it with known points.

03 Sort by Distance

Smallest distances come first.

04 Choose K

Select the K closest points.

10

Complete Example

Let's put everything together using a new student.

NEW STUDENT [5, 88]

5 study hours, 88% attendance.

CALCULATE DISTANCES Compare with known students

Find how close each student is.

SORT DISTANCES Small → Large

The smallest distances are the nearest points.

CHOOSE K K = 3

Select the three closest students.

After finding the three nearest students, KNN can look at their labels and perform the majority vote.

11

Why Feature Scaling Matters

There is an important problem when features use very different scales.

FEATURE 1 Age

Usually around 18–80.

FEATURE 2 Income

Could be 20,000–200,000.

Income has much larger numerical values than age. If we calculate distance directly, the larger-scale feature can dominate the calculation.

This is why KNN often benefits from feature scaling before calculating distances.

You already learned about Feature Scaling in Lesson 6 — Data Preparation. Here we are seeing why it matters specifically for KNN.

12

The Big Picture

Distance is not the final prediction. Distance is used to find the neighbors.

DISTANCE Find what is close
NEIGHBORS Choose K closest
LABELS Check their classes
PREDICTION Majority vote
REMEMBER THIS

KNN uses distance to find its nearest neighbors.

A smaller distance means two data points are closer. KNN calculates distances, finds the closest points, selects K of them, and then uses their labels to make the prediction.

Data Points Calculate Distance Find Closest Choose K Predict
QUICK CHECK

Check Your Understanding

Why does KNN need distance? To determine which known data points are closest to a new data point.
What does a small distance mean? The two data points are close or more similar according to the selected features and distance measure.
What does a large distance mean? The two data points are farther apart.
What is Euclidean distance? A common way of measuring the straight-line distance between data points.
After calculating distances, what does KNN do? It identifies the points with the smallest distances and selects the K nearest neighbors.
Why can feature scaling matter for KNN? Features with much larger numerical scales can dominate distance calculations.
NEXT TOPIC

KNN Classification

Now that we know how KNN finds the nearest points, we can put everything together and see exactly how those neighbors are used to classify a new data point.