K-nearest neighbors
Ask the most similar stored examples. Nearby deliveries suggest a duration; nearby parcels suggest a category.
Start with an example
If the three closest routes took 20, 24, and 28 minutes, uniform-weight regression predicts their average: 24 minutes.
How it learns
An input feature is a measurement about an example. The target is its known answer: a number for regression, or a category for classification. Training means learning from examples whose answers are already known.
- Store the training examples. For a new input, find the k closest stored inputs; k is the number of neighbors you choose to consult.
- To predict a number, average their known answers. To predict a class, count their class votes. Closeness uses the input measurements, not the unknown answer you are trying to predict.
Try it yourself
The example above gives the idea. The experiment below uses a separate, made-up dataset with unitless measurements so you can focus on how the model works. Read the graph guide first, then change one thing at a time.
1. Understand what you are looking at
Use nearby examples to estimate a class. This graph demonstrates classification; the delivery-time example above demonstrates the related averaging idea for regression.
- Across: the horizontal axis
- The input measurement x, from 0 to 10. Moving right means a larger input.
- Up: the vertical axis
- The estimated probability of class 1, from 0 to 1. A curve height of 0.8 means an estimated 80% chance, not an observed class of 0.8.
Dark dots are observed answers: class 0 at the bottom or class 1 at the top. The green line is the model's estimate, which can lie between 0 and 1.
How to read one result: Start at x on the bottom axis and follow the vertical dashed line to the outlined diamond. Its height is the estimated probability of class 1 at that input; it is not a new observed example.
2. Predict, change, observe
- Set Neighbors (k) to 1. Move Input to inspect (x) across the graph: the estimate follows the class of the closest dot, measured horizontally.
- Before increasing k to 15, predict whether one unusual dot will have more or less influence. Try it and compare how abruptly the line changes.
- Observations
- Model prediction
- Inspected result (diamond)
Training errors: 2 of 16.
Training errors count observed labels that disagree with the decision: estimates of at least 0.5 choose class 1; lower estimates choose class 0.
3. Explain it in your own words
If 2 of the 3 nearest examples belong to class 1, what height should the prediction have?
Compare your reasoning
The height is 2 divided by 3, about 0.67: two of the three neighbors vote for class 1. Distance here uses x, not the dot's class height. If unsure, set k to 1 first and follow how the nearest horizontal position determines the answer.
Ready to continue when you can explain the result and why it changes. If the explanation is still unclear, reset and repeat the two steps above with one control at a time.
What this experiment simplifies
Each prediction is the fraction of class-1 labels among the k nearest examples. A larger k uses a wider neighborhood.
These are small teaching implementations, not scikit-learn runs. Training error on these examples does not estimate performance on new data.
View the plotted examples
| Input feature (x) | Observed class |
|---|---|
| 0.30 | 0 |
| 0.92 | 0 |
| 1.54 | 0 |
| 2.16 | 1 |
| 2.78 | 0 |
| 3.40 | 0 |
| 4.02 | 0 |
| 4.64 | 1 |
| 5.26 | 1 |
| 5.88 | 1 |
| 6.50 | 1 |
| 7.12 | 0 |
| 7.74 | 1 |
| 8.36 | 1 |
| 8.98 | 1 |
| 9.60 | 1 |
The math, one step at a time
Read the symbols after the idea makes sense. You do not need to memorize the formula.
Nₖ(x) is the set of k nearest training inputs to x. This formula assumes uniform weights; k is a setting, not a learned coefficient.
What to watch for
- Feature units matter: kilometres and grams need an appropriate shared scale.
- Very small k can follow noise; very large k can blur useful local patterns. Prediction also requires searching stored examples.
Explore a related model
Further reading
The official documentation goes further when you are ready.