Learn by Looking Nearby
Similar examples suggest similar outcomes
K-nearest neighbors (KNN) stores training examples rather than fitting a compact equation. For a new row, it calculates distance to training rows, selects the k closest, and combines their outcomes. Classification uses a vote; regression often averages neighbor targets.
new point x
nearest 5 labels: fail, fail, safe, fail, safe
prediction: fail (3 of 5)
Small k creates flexible, jagged decisions sensitive to noise. Large k smooths decisions but may wash out local patterns and favor the majority class. Choose k with cross-validation and consider distance-weighted voting, where closer neighbors count more.
from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier(n_neighbors=7, weights='distance')
model.fit(X_train, y_train)
Analogy: To estimate a home's price, inspect nearby comparable homes rather than one nationwide average. The method works only if nearby captures meaningful similarity.
Tip: For an even k, classification votes can tie. Prefer an odd value for binary tasks or define an explicit distance-weighted tie policy.