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.
from sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import train_test_split import pandas as pd f=r"aug_train.csv" df=pd.read_csv(f) X=df[:90000][["training_hours", "city_development_index"]].values y=df[:90000]["target"].values X_train, X_test, y_train, y_test=train_test_split(X,y) knn=KNeighborsClassifier(n_neighbors=2) knn.fit(X_train, y_train) yhat=knn.predict(X_test) print(yhat)
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)