← All tutorials

GoCareerGo Tutorials

Machine Learning Tutorial

Supervised learning, classification, neural networks and core ML algorithms.

K-Nearest Neighbors (KNN)

A simple, 'lazy learning' algorithm — classifies a new point by majority vote among its K closest neighbors in the training set.

from sklearn.neighbors import KNeighborsClassifier

model = KNeighborsClassifier(n_neighbors=5)
model.fit(X_train, y_train)
  • No real 'training' phase — all computation happens at prediction time
  • Sensitive to feature scale — always normalize/standardize features first
  • Choosing K is a bias-variance tradeoff: small K → high variance, large K → high bias