Predicting a Numerical Value with K-Nearest Neighbors (KNN)

Last updated

Problem Setup

Scenario:Estimate the resale price of a device from its age and battery health using KNN regression.

Given:A labeled dataset of 8 devices with Age_Months and Battery_Health_Percent as input features and Resale_Price_GBP as the numerical target. The target device has Age_Months = 20 and Battery_Health_Percent = 78, with K = 3.

Task:Calculate the Euclidean distance from the target device to each historical device, identify the 3 nearest neighbors, and average their resale prices to obtain the predicted value.

Need a quick refresher? Review K-Nearest Neighbors theory

Historical Devices & Resale Prices

Each row represents a previously sold device with its age, battery condition, and observed resale price. These examples provide the reference points for finding devices most similar to the target and estimating its resale value.

Data Point
Age_Months✎
Battery_Health_Percent✎
Resale_Price_GBP
P1
6
95
850
P2
12
88
720
P3
18
80
600
P4
24
72
500
P5
30
65
420
P6
8
92
800
P7
15
85
650
P8
36
55
320
Target
20
78
?

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:Target = 850
P1 = (20−6)2+(78−95)2\sqrt{(20 - 6)^2 + (78 - 95)^2}
P1 = 196+289\sqrt{196 + 289}
P1 = 485\sqrt{485}
P1 = 22.02322.023
For row P2:Target = 720
P2 = (20−12)2+(78−88)2\sqrt{(20 - 12)^2 + (78 - 88)^2}
P2 = 64+100\sqrt{64 + 100}
P2 = 164\sqrt{164}
P2 = 12.80612.806
For row P3:Target = 600
P3 = (20−18)2+(78−80)2\sqrt{(20 - 18)^2 + (78 - 80)^2}
P3 = 4+4\sqrt{4 + 4}
P3 = 8\sqrt{8}
P3 = 2.8282.828
For row P4:Target = 500
P4 = (20−24)2+(78−72)2\sqrt{(20 - 24)^2 + (78 - 72)^2}
P4 = 16+36\sqrt{16 + 36}
P4 = 52\sqrt{52}
P4 = 7.2117.211
For row P5:Target = 420
P5 = (20−30)2+(78−65)2\sqrt{(20 - 30)^2 + (78 - 65)^2}
P5 = 100+169\sqrt{100 + 169}
P5 = 269\sqrt{269}
P5 = 16.40116.401
For row P6:Target = 800
P6 = (20−8)2+(78−92)2\sqrt{(20 - 8)^2 + (78 - 92)^2}
P6 = 144+196\sqrt{144 + 196}
P6 = 340\sqrt{340}
P6 = 18.43918.439
For row P7:Target = 650
P7 = (20−15)2+(78−85)2\sqrt{(20 - 15)^2 + (78 - 85)^2}
P7 = 25+49\sqrt{25 + 49}
P7 = 74\sqrt{74}
P7 = 8.6028.602
For row P8:Target = 320
P8 = (20−36)2+(78−55)2\sqrt{(20 - 36)^2 + (78 - 55)^2}
P8 = 256+529\sqrt{256 + 529}
P8 = 785\sqrt{785}
P8 = 28.01828.018

Step 2: Rank Training Points by Distance

RankPointDistanceTarget Value
#1P32.828600
#2P47.211500
#3P78.602650
#4P212.806720
#5P516.401420
#6P618.439800
#7P122.023850
#8P828.018320

Step 3: Average the 3 Nearest Target Values

Formula: Prediction=v1+v2+⋯+vkK\text{Prediction} = \dfrac{v_1 + v_2 + \dots + v_k}{K}
Calculate the Mean

Prediction = (600 + 500 + 650) / 3

Prediction = 1750 / 3

Prediction = 583.333

Final Answer

Nearest neighbors:P3 (£600), P4 (£500), P7 (£650)

Predicted resale price:£583.33

The estimate reflects the average resale price of similar historical devices. It is a prediction based on the selected neighbors, not a guaranteed selling price.

Exam Takeaway

  • Keep input features separate from target values: Calculate distances using the input features only. The target values are used after selecting the nearest neighbors to calculate the regression prediction.
  • Check feature scales before calculating distances: Euclidean distance is sensitive to numerical ranges, so features with larger scales can dominate neighbor selection. If scaling is required, apply the same transformation to both training points and the query point.
  • Understand how K affects the prediction: A smaller K makes predictions more sensitive to individual nearby observations, while a larger K averages more target values and generally produces a smoother estimate. Neither choice guarantees the most accurate prediction.