KNN Regression: A Complete Solved Numerical Example

Scenario: Used Laptop Price Estimation

The Objective: Predict the resale price (£) of a used laptop based on its age in months and battery health percentage.

Step 1: The Historical Data & Target Point

To predict a continuous value, KNN Regression looks at the most similar historical data points. We are predicting the Resale_Price_GBP for a new target point with features: [20, 78] using K = 3.

Data PointAge_MonthsBattery_Health_PercentResale_Price_GBP
P1695850
P21288720
P31880600
P42472500
P53065420
P6892800
P71585650
P83655320
Target2078?

Step 2: Calculate Euclidean Distances

First, we measure exactly how "far" our target point is from every single historical row using the Euclidean distance formula.

Formula: d=(x2x1)2+(y2y1)2+d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2 + \dots}
Distance to Row P1Target Val = 850

d = √((20 - 6)² + (78 - 95)²)

d = √(196 + 289)

d = √485

d = 22.0227

Distance to Row P2Target Val = 720

d = √((20 - 12)² + (78 - 88)²)

d = √(64 + 100)

d = √164

d = 12.8062

Distance to Row P3Target Val = 600

d = √((20 - 18)² + (78 - 80)²)

d = √(4 + 4)

d = √8

d = 2.8284

Distance to Row P4Target Val = 500

d = √((20 - 24)² + (78 - 72)²)

d = √(16 + 36)

d = √52

d = 7.2111

Distance to Row P5Target Val = 420

d = √((20 - 30)² + (78 - 65)²)

d = √(100 + 169)

d = √269

d = 16.4012

Distance to Row P6Target Val = 800

d = √((20 - 8)² + (78 - 92)²)

d = √(144 + 196)

d = √340

d = 18.4391

Distance to Row P7Target Val = 650

d = √((20 - 15)² + (78 - 85)²)

d = √(25 + 49)

d = √74

d = 8.6023

Distance to Row P8Target Val = 320

d = √((20 - 36)² + (78 - 55)²)

d = √(256 + 529)

d = √785

d = 28.0179

Step 3: Select the Top K Neighbors

We rearrange the calculated distances in ascending order (smallest to largest) and select the top K = 3 closest neighbors.

RankPointDistanceTarget Value
#1P32.8284600
#2P47.2111500
#3P78.6023650
#4P212.8062720
#5P516.4012420
#6P618.4391800
#7P122.0227850
#8P828.0179320

Step 4: Final Regression Prediction

In KNN Regression, the final prediction is simply the average (mean) of the target values from our selected K neighbors.

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.33

Final Takeaway

Because the 3 laptops closest in age and battery health sold for an average of 583.33, the model predicts that is the most accurate estimated resale price for the new laptop.