What does the KNN algorithm do in the training phase?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The short answer is that k-nearest neighbors does almost no model training in the usual sense. KNN is a lazy learner: during the "training phase" it mainly stores the training examples, and most of the real computation happens later when you ask it to make a prediction.
Why KNN Is Different
Many supervised algorithms use training time to learn parameters:
- linear regression learns coefficients
- logistic regression learns weights
- decision trees learn split rules
- neural networks learn layer parameters
KNN does not build a compact parametric model like that. Instead, it keeps the labeled data and defers the decision until inference time.
That is why people often say KNN has a cheap training phase and an expensive prediction phase.
What Actually Happens During Training
The essential training work in KNN is usually:
- store the feature vectors
- store the labels or target values
- optionally normalize or scale features
- choose hyperparameters such as
kand the distance metric
If you are using a library such as scikit-learn, calling fit() still exists, but for KNN the method is mostly validating and storing the data rather than learning deep structure from it.
A Simple Example
Here is a small scikit-learn example.
When fit() runs here, the classifier is not learning coefficients like a linear model would. It is remembering the training points so that later predictions can compare new inputs against them.
What The Prediction Phase Does Instead
At prediction time, KNN performs the expensive part:
- compute the distance from the new point to training examples
- find the
knearest stored samples - vote on the label for classification, or average for regression
That means prediction cost usually scales with the size of the training set, while training cost is relatively light.
A simple manual implementation shows the idea.
This code makes the lazy-learning structure obvious. Nothing complex happened earlier in training.
The Real "Training" Decisions
Even though KNN does not learn parameters in the usual way, useful preparation still matters.
Feature Scaling
Distance-based models are sensitive to scale. If one feature ranges from 0 to 1 and another ranges from 0 to 100000, the large-scale feature dominates the distance calculation.
Choosing k
The value of k is a hyperparameter chosen outside the core algorithm, often by cross-validation.
- small
kcan overfit noise - large
kcan oversmooth boundaries
Choosing A Distance Metric
Euclidean distance is common, but Manhattan, cosine, or domain-specific metrics may work better depending on the data.
These choices are often more important than the nominal training step itself.
Are There Any Accelerations
Some KNN implementations build data structures such as KD-trees or ball trees to speed up neighbor lookup. That preparation happens during fit(), but it is still not the same as learning a predictive model from the data.
It is better to think of this as indexing the stored data rather than training weights.
Common Pitfalls
A common mistake is assuming that because fit() is called, KNN must be learning a complex internal model. Usually it is just storing data and perhaps building a search structure.
Another issue is ignoring scaling. Unscaled features can make KNN behave badly even though the training step appears to succeed.
Developers also sometimes choose k arbitrarily. Since KNN relies heavily on local neighborhood structure, the wrong k can make results unstable or overly smooth.
Finally, KNN can become expensive at inference time on large datasets. Cheap training does not mean cheap deployment.
Summary
- KNN is a lazy learner, so the training phase mostly stores the training data.
- Most of the real work happens during prediction when distances are computed.
- Training still involves important choices such as scaling,
k, and distance metric. - Some implementations build search indexes during
fit(), but that is not the same as learning model weights. - KNN has light training cost and potentially heavy prediction cost.
Related reading
- What Does The MAE Actually Telling me?
- What does the print of DMA and tensorflow mean? And is it possible to set them?
- What does the property losses of the Bayesian layers of TensorFlow Probability represent?
- what does the question mark in tensorflow shape mean?
- What does the .numpy function do?
- What does the verbosity parameter of a random forest mean? sklearn
- What events should go through the RAFT log
- What exactly differs fuzzy search from Full Text Search?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.