Predicting a Class with K-Nearest Neighbors (KNN)

Last updated

Problem Setup

Scenario:Predict whether a website visitor will make a purchase based on how many pages they visited and how long they stayed on the site.

Given:Eight historical visitor records are labeled as Purchased = Yes or No. The target visitor has Pages_Visited = 5 and Time_on_Site_mins = 12, with K = 3.

Task:Use K-Nearest Neighbors to calculate the Euclidean distance from the target visitor to each historical record, identify the 3 closest neighbors, and assign the purchase outcome using their majority class.

Need a quick refresher? Review K-Nearest Neighbors theory

Historical Visitors & Target

The table contains previously observed visitors with known purchase outcomes. Their feature values provide the reference points used to measure how close each visitor is to the target before selecting the nearest neighbors.

Data Point
Pages_Visited✎
Time_on_Site_mins✎
Purchased
P1
2
5
No
P2
3
8
No
P3
5
15
Yes
P4
7
20
Yes
P5
6
18
Yes
P6
1
3
No
P7
4
10
No
P8
8
25
Yes
Target
5
12
?

Step-by-Step Solution

Step 1: Calculate Euclidean Distance for each row

Formula: d=(x2−x1)2+(y2−y1)2+…d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2 + \dots}
For row P1:
d = (5−2)2+(12−5)2\sqrt{(5 - 2)^2 + (12 - 5)^2}
d = 9+49\sqrt{9 + 49}
d = 58\sqrt{58}
d = 7.6167.616
For row P2:
d = (5−3)2+(12−8)2\sqrt{(5 - 3)^2 + (12 - 8)^2}
d = 4+16\sqrt{4 + 16}
d = 20\sqrt{20}
d = 4.4724.472
For row P3:
d = (5−5)2+(12−15)2\sqrt{(5 - 5)^2 + (12 - 15)^2}
d = 0+9\sqrt{0 + 9}
d = 9\sqrt{9}
d = 33
For row P4:
d = (5−7)2+(12−20)2\sqrt{(5 - 7)^2 + (12 - 20)^2}
d = 4+64\sqrt{4 + 64}
d = 68\sqrt{68}
d = 8.2468.246
For row P5:
d = (5−6)2+(12−18)2\sqrt{(5 - 6)^2 + (12 - 18)^2}
d = 1+36\sqrt{1 + 36}
d = 37\sqrt{37}
d = 6.0836.083
For row P6:
d = (5−1)2+(12−3)2\sqrt{(5 - 1)^2 + (12 - 3)^2}
d = 16+81\sqrt{16 + 81}
d = 97\sqrt{97}
d = 9.8499.849
For row P7:
d = (5−4)2+(12−10)2\sqrt{(5 - 4)^2 + (12 - 10)^2}
d = 1+4\sqrt{1 + 4}
d = 5\sqrt{5}
d = 2.2362.236
For row P8:
d = (5−8)2+(12−25)2\sqrt{(5 - 8)^2 + (12 - 25)^2}
d = 9+169\sqrt{9 + 169}
d = 178\sqrt{178}
d = 13.34213.342

Step 2: Rank Training Points by Distance

RankPointDistanceClass
#1P72.236No
#2P33Yes
#3P24.472No
#4P56.083Yes
#5P17.616No
#6P48.246Yes
#7P69.849No
#8P813.342Yes

Step 3: Pick the top K=3 neighbors & determine majority

Formula: Prediction=Mode(c1,c2,…,ck)\text{Prediction} = \text{Mode}(c_1, c_2, \dots, c_k)
Top 3 Neighbors

Selecting the first 3 rows from our sorted list:

  • P7 belongs to class No
  • P3 belongs to class Yes
  • P2 belongs to class No

Final Answer

Prediction:No

Nearest neighbors:P7 → No, P3 → Yes, P2 → No

Class vote:No: 2 | Yes: 1

This classification reflects the behavior of the selected historical neighbors, not a certainty about whether the visitor will actually make a purchase.

Exam Takeaway

  • Check feature scales before calculating distances: Euclidean distance is sensitive to differences in feature ranges. Features with larger numerical ranges can dominate the result, so apply normalization or standardization when appropriate. Use the preprocessing specified in the question.
  • Select neighbors using distance, not class labels: Rank all training points by their distance to the query point and select exactly K neighbors. Class labels are used only after the neighbors have been selected. Follow the specified rule when distances are tied.
  • Distinguish majority voting from prediction confidence: A class receiving the most votes becomes the KNN prediction, but its vote proportion is not automatically a calibrated probability. Changing K, the distance metric, or feature scaling can change the predicted class.