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 — is identical; only the aggregation changes.
Algorithm
Given a query point, rank the training cases by distance and take the closest. The simplest prediction is the plain average of their targets:
A refinement weights each neighbor by inverse distance, so closer cases speak louder:
k-NN regression uses the same distance functions as the classifier. For continuous variables, the Euclidean and Manhattan distances are the and cases of the Minkowski family:
For categorical variables, use the Hamming distance — the number of positions at which two equal-length attribute vectors differ:
Choosing is the classic bias–variance dial: small tracks local noise, large smooths away real structure. Cross-validation on held-out data is the reliable way to pick it; in practice, 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:
| Age | Loan ($) | HPI | Distance to query |
|---|---|---|---|
| 25 | 40,000 | 135 | 102,000 |
| 35 | 60,000 | 256 | 82,000 |
| 45 | 80,000 | 231 | 62,000 |
| 20 | 20,000 | 267 | 122,000 |
| 35 | 120,000 | 139 | 22,000 |
| 52 | 18,000 | 150 | 124,000 |
| 23 | 95,000 | 127 | 47,000 |
| 40 | 62,000 | 216 | 80,000 |
| 60 | 100,000 | 139 | 42,000 |
| 48 | 220,000 | 250 | 78,000 |
| 33 | 150,000 | 264 | 8,000 |
With , the nearest case is the last row:
With , 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:
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, , Age spans 20–60 and Loan 18,000–220,000, so the query becomes :
| Age (std) | Loan (std) | HPI | Distance |
|---|---|---|---|
| 0.13 | 0.11 | 135 | 0.765 |
| 0.38 | 0.21 | 256 | 0.494 |
| 0.63 | 0.31 | 231 | 0.316 |
| 0.00 | 0.01 | 267 | 0.924 |
| 0.38 | 0.50 | 139 | 0.336 |
| 0.80 | 0.00 | 150 | 0.616 |
| 0.08 | 0.38 | 127 | 0.672 |
| 0.50 | 0.22 | 216 | 0.426 |
| 1.00 | 0.41 | 139 | 0.364 |
| 0.70 | 1.00 | 250 | 0.386 |
| 0.33 | 0.66 | 264 | 0.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 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 most similar training cases, using the same distance functions as k-NN classification. On the house-price data, predicts HPI 264 while smooths to 180.7 — and standardizing the features changes the nearest neighbor entirely, a reminder that in k-NN, geometry is the model.