Home > Back-end >  How to n-times train K Nearest Neighboar (Knn)?
How to n-times train K Nearest Neighboar (Knn)?

Time:09-12

Is there a way to tell the Knn function in Python how often he should train the model before I predict a possible outcome? From my understanding, if a model is trained more than one time, it is more robust. Or do I get that wrong? From my code it seems that the model is probably one get trained one time:

from sklearn.neighbors import KNeighborsClassifier

neigh = KNeighborsClassifier(n_neighbors=1)
neigh.fit(X_train, y_train)

CodePudding user response:

The k-nearest-neighbor algorithm isn't a traditional machine learning algorithm that learns by iterations (epochs) or additional [decision] trees [...] to slowly converge/adapt, but a model that returns an averaged prediction based on an observation's k nearest (most similar) neighbors. The fit() only stores the input training data, defines the distance metric to determine similarity (nearest) - by default the euclidean distance - and sets n_neighbors, the number of neighbors to be considered. A parameter n_neighbors=1 simply returns a prediction based on the most similar observation already known in the input training data set - otherwise, predictions will be averaged between the k nearest neighbors.

More details regarding the actual implementation of fit() and the need to fit a k-nearest-neighbor model in the first place can be found in this thread.

  • Related