What happens when we apply .fit() method to a kNN model in Scikit-learn if kNN has no training phase?


Since kNN handles both training and prediction at the RAM level and requires no explicit training process, what exactly happens when a knn model is being fitted? I thought this step was related to training the model. Thank you.

Here is the error I will get if I skip fitting step.

NotFittedError: This KNeighborsClassifier instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.

Sample Code:

from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import pandas as pd


X=df[:90000][["training_hours", "city_development_index"]].values

X_train, X_test, y_train, y_test=train_test_split(X,y)

knn.fit(X_train, y_train)


Unlike other Machine Learning Algorithms KNN doesn’t optimize a cost function instead it remembers the training data. When a prediction is made the KNN compares the input with the training data it has stored. The class label of the data point which has maximum similarity with the queried input is given as prediction. Hence when we fit a KNN model it learns or stores the dataset in memory.

Answered By – Vikas Kumar Ojha

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.