KNN algorithm
machine learning
training phase
supervised learning
data science

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.

Practice ML system design

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:

  1. store the feature vectors
  2. store the labels or target values
  3. optionally normalize or scale features
  4. choose hyperparameters such as k and 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.

python
1from sklearn.neighbors import KNeighborsClassifier
2
3X_train = [
4    [1.0, 1.0],
5    [1.2, 0.9],
6    [4.0, 4.1],
7    [4.2, 3.9],
8]
9y_train = [0, 0, 1, 1]
10
11model = KNeighborsClassifier(n_neighbors=3)
12model.fit(X_train, y_train)
13
14print(model.predict([[1.1, 1.0]]))

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:

  1. compute the distance from the new point to training examples
  2. find the k nearest stored samples
  3. 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.

python
1import math
2from collections import Counter
3
4
5def distance(a, b):
6    return math.sqrt(sum((x - y) ** 2 for x, y in zip(a, b)))
7
8
9def knn_predict(train_X, train_y, query, k):
10    pairs = []
11    for features, label in zip(train_X, train_y):
12        pairs.append((distance(features, query), label))
13
14    pairs.sort(key=lambda item: item[0])
15    nearest_labels = [label for _, label in pairs[:k]]
16    return Counter(nearest_labels).most_common(1)[0][0]
17
18
19print(knn_predict(X_train, y_train, [1.1, 1.0], 3))

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 k can overfit noise
  • large k can 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice ML system design

All Rights Reserved.