🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #111

KNN Regression

For regression, KNN predicts a continuous value by averaging the target values of the k nearest neighbors — the same "find similar points" idea as classification, with averaging replacing voting.

Formula

\[ \hat{y} = \frac{1}{k}\sum_{i=1}^{k} y_{(i)} \]

\(y_{(1)}, \dots, y_{(k)}\) are the target values of the \(k\) nearest neighbors. This is a simple, unweighted average by default — every one of the k neighbors contributes equally.

Worked Numerical Example

House sizes (sq ft) and prices (lakh): \((800, 35), (1000, 42), (1200, 48), (1500, 60), (1800, 72), (2000, 80)\). Query: predict the price for a 1300 sq ft house, using \(k=3\).

Size|1300 − size|Price
120010048
150020060
100030042
80050035
180050072
200070080
\[ \hat{y} = \frac{48+60+42}{3} = \frac{150}{3} = 50 \]

The 3 nearest neighbors by size are 1200, 1500 and 1000 sq ft — averaging their prices gives a prediction of ₹50 lakh for the 1300 sq ft house.

from sklearn.neighbors import KNeighborsRegressor
import numpy as np

X_train = np.array([[800],[1000],[1200],[1500],[1800],[2000]])
y_train = np.array([35, 42, 48, 60, 72, 80])

model = KNeighborsRegressor(n_neighbors=3)
model.fit(X_train, y_train)

print(model.predict([[1300]]))   # [50.]  -- matches the hand calculation exactly

Distance-Weighted Regression

model_weighted = KNeighborsRegressor(n_neighbors=3, weights="distance")
model_weighted.fit(X_train, y_train)
print(model_weighted.predict([[1300]]))
# Closer neighbors (1200, distance 100) pull the prediction more than farther ones (1000, distance 300)

With weights="distance", the 1200 sq ft neighbor (distance 100, very close) has more influence than the 1000 sq ft neighbor (distance 300, farther) — usually a more accurate approach than treating all k neighbors identically.

Practical Use Cases

  • Price estimation based on similar past sales (real estate, used cars)
  • Any regression problem where "similar inputs should have similar outputs" is a reasonable, defensible assumption

Advantages and Limitations, Specific to Regression

  • Naturally captures local, non-linear relationships without needing explicit polynomial features
  • Predictions are always bounded within the range of observed target values in the neighborhood — KNN regression can never extrapolate beyond the training data's value range, unlike linear regression

Common Mistakes

  • Expecting KNN regression to extrapolate sensibly beyond the range of training data — since it only averages nearby observed values, it can't predict a value outside what it's already seen nearby.
  • Using unweighted averaging on data where the nearest neighbor is dramatically closer than the k-th nearest — weighted averaging is usually the better default.

Interview Relevance

Q: "Can KNN regression predict a value higher than any value seen in training?" No — since predictions are always an average (weighted or not) of observed neighbor values, the output is mathematically bounded within the range of the training targets, unlike linear regression which can extrapolate arbitrarily far.

Practice Question

Using the house price table above, predict the price for a 1600 sq ft house with k=3, showing your distance calculations.

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

KNN Regression – FAQs

Quick answers about learning KNN Regression in Machine Learning.

This free note from CodingNow 2.0 explains KNN Regression in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including KNN Regression, is 100% free with no signup required.
With focused practice, most students grasp KNN Regression in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now