KNN Regression: A Complete Solved Numerical Example

Scenario: Used Laptop Price Estimation

The Objective: Predict the exact resale price of a used laptop by mathematically finding similar past sales and averaging their values.

Core Mechanics
  • Same Start, Different End: Like Classification, it begins by finding the KK nearest neighbors. The only difference is that instead of a category, you are predicting a continuous number.
  • Average, Don't Vote: Since you aren't choosing a class, you take the mean (average) of your KK neighbors' values. The result is a specific numerical prediction, not a count.
  • Outlier Sensitivity: In Classification, one rogue neighbor rarely changes the outcome. In Regression, a single extreme neighbor can dramatically pull your average away from the truth.
  • The KK Smoothness Tradeoff: A small KK captures local detail but is jittery and noisy. A large KK produces smooth predictions but can drift too far toward the global average, losing all local accuracy.

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

Distance to Row P2Target Val = 720

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

d = √(64 + 100)

d = √164

d = 12.806

Distance to Row P3Target Val = 600

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

d = √(4 + 4)

d = √8

d = 2.828

Distance to Row P4Target Val = 500

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

d = √(16 + 36)

d = √52

d = 7.211

Distance to Row P5Target Val = 420

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

d = √(100 + 169)

d = √269

d = 16.401

Distance to Row P6Target Val = 800

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

d = √(144 + 196)

d = √340

d = 18.439

Distance to Row P7Target Val = 650

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

d = √(25 + 49)

d = √74

d = 8.602

Distance to Row P8Target Val = 320

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

d = √(256 + 529)

d = √785

d = 28.018

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.828600
#2P47.211500
#3P78.602650
#4P212.806720
#5P516.401420
#6P618.439800
#7P122.023850
#8P828.018320

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

Final Takeaway

Notice the critical difference in Step 4 compared to standard KNN Classification! Instead of taking a rigid majority vote, KNN Regression calculates the mathematical average of the 3 closest laptops (600, 500, and 650) to predict a highly specific, continuous price.