Skip to content
Saed Sayad

k-NN Regression

k-NN regression predicts a numerical target as the average of the k nearest neighbors. Distance functions, weighting, and standardization.

5 min read · Updated August 8, 2026

K nearest neighbors is a lazy, non-parametric algorithm: it stores the entire training set and answers a query by finding the most similar stored cases. In k-NN classification the neighbors vote on a class label; in k-NN regression their numerical targets are averaged. The machinery — distance functions, the choice of kk — is identical; only the aggregation changes.

Algorithm

Given a query point, rank the training cases by distance and take the kk closest. The simplest prediction is the plain average of their targets:

y^=1ki=1kyi\hat{y} = \frac{1}{k} \sum_{i=1}^{k} y_i

A refinement weights each neighbor by inverse distance, so closer cases speak louder:

y^=i=1kwiyii=1kwi,wi=1d(x,xi)\hat{y} = \frac{\sum_{i=1}^{k} w_i\, y_i}{\sum_{i=1}^{k} w_i}, \qquad w_i = \frac{1}{d(x, x_i)}

k-NN regression uses the same distance functions as the classifier. For continuous variables, the Euclidean and Manhattan distances are the p=2p = 2 and p=1p = 1 cases of the Minkowski family:

DEuclid=i=1p(xiyi)2,DManhattan=i=1pxiyi,DMinkowski=(i=1pxiyiq)1/qD_{Euclid} = \sqrt{\sum_{i=1}^{p}(x_i - y_i)^2}, \qquad D_{Manhattan} = \sum_{i=1}^{p} |x_i - y_i|, \qquad D_{Minkowski} = \left( \sum_{i=1}^{p} |x_i - y_i|^q \right)^{1/q}

For categorical variables, use the Hamming distance — the number of positions at which two equal-length attribute vectors differ:

DH=i=1p1[xiyi]D_H = \sum_{i=1}^{p} \mathbf{1}[x_i \neq y_i]

Choosing kk is the classic bias–variance dial: small kk tracks local noise, large kk smooths away real structure. Cross-validation on held-out data is the reliable way to pick it; in practice, kk of 10 or more beats 1-NN on most datasets.

Worked example: house price index

The training set relates a house’s Age and Loan amount to its House Price Index (HPI). We predict HPI for a new case with Age = 48 and Loan = $142,000, using Euclidean distance:

AgeLoan ($)HPIDistance to query
2540,000135102,000
3560,00025682,000
4580,00023162,000
2020,000267122,000
35120,00013922,000
5218,000150124,000
2395,00012747,000
4062,00021680,000
60100,00013942,000
48220,00025078,000
33150,0002648,000

With k=1k = 1, the nearest case is the last row:

D=(4833)2+(142000150000)2=8000.01    HPI^=264D = \sqrt{(48 - 33)^2 + (142000 - 150000)^2} = 8000.01 \;\Rightarrow\; \widehat{HPI} = 264

With k=3k = 3, the neighbors are the rows at distances 8,000 (HPI 264), 22,000 (HPI 139), and 42,000 (HPI 139), and the prediction is their average:

HPI^=264+139+1393=180.7\widehat{HPI} = \frac{264 + 139 + 139}{3} = 180.7

Standardized distance

Notice what dominated that computation: Loan, measured in tens of thousands of dollars, swamped Age, measured in tens of years. The distance was essentially Loan distance alone. When variables live on different scales — or mix numerical and categorical types — standardize first. With min–max scaling, z=vminmaxminz = \frac{v - \min}{\max - \min}, Age spans 20–60 and Loan 18,000–220,000, so the query becomes (0.70,0.61)(0.70, 0.61):

Age (std)Loan (std)HPIDistance
0.130.111350.765
0.380.212560.494
0.630.312310.316
0.000.012670.924
0.380.501390.336
0.800.001500.616
0.080.381270.672
0.500.222160.426
1.000.411390.364
0.701.002500.386
0.330.662640.371

Now the nearest neighbor is the third row (HPI 231), not the last — a completely different answer from the same data. As with the classifier, that sensitivity is not a good sign of robustness; it means scale choices are modeling decisions.

In practice

sklearn.neighbors.KNeighborsRegressor handles both uniform and inverse-distance weighting (weights='distance'), with StandardScaler in a Pipeline to make standardization automatic. k-NN regression is a solid non-parametric baseline and a natural fit for recommendation-style similarity problems, but prediction cost grows with the training set, so large systems use approximate indexes (ball trees, KD-trees, HNSW). The curse of dimensionality bites hard beyond a dozen or so features — distances concentrate and “nearest” stops meaning much.

Common pitfalls

  • Forgetting to scale features, letting the largest-unit variable silently dominate every distance.
  • Choosing kk by gut — too small and you fit noise; too large and you fit the global mean. Cross-validate it.
  • Using plain Euclidean distance on high-dimensional sparse data, where all points become nearly equidistant.
  • Ignoring prediction cost: k-NN does zero work at training time and all of it at query time.
  • Mixing categorical features into Euclidean distance instead of using Hamming distance or encoding properly.

Summary

k-NN regression predicts the (optionally distance-weighted) average target of the kk most similar training cases, using the same distance functions as k-NN classification. On the house-price data, k=1k = 1 predicts HPI 264 while k=3k = 3 smooths to 180.7 — and standardizing the features changes the nearest neighbor entirely, a reminder that in k-NN, geometry is the model.