Last updated
Classification with K-Nearest Neighbours
Definition: Classification predicts a category (not a number). K-Nearest Neighbours (KNN) is a simple, intuitive classifier: to label a new point, it looks at the K closest known points and takes a majority vote.
Example — apple or orange?
Features are [weight, texture] where texture is 0 = smooth, 1 = bumpy:
from sklearn.neighbors import KNeighborsClassifier
# features: [weight, texture] labels: the fruit
X = [[150,0],[170,0],[140,0],[130,1],[120,1],[110,1]]
y = ["apple","apple","apple","orange","orange","orange"]
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X, y)
print("A 160g smooth fruit is:", model.predict([[160, 0]])[0])
print("A 115g bumpy fruit is: ", model.predict([[115, 1]])[0])
How KNN decides
For a new fruit, KNN finds the 3 most similar known fruits and picks the most common label among them. No complex maths — just "what does it most resemble?"
💡 Try it: change the test fruit values and re-run to see the prediction flip between apple and orange.
Try it Yourself
Output
Ad · responsive