Codzy

AI: classifying with k nearest neighbours

The k nearest neighbours algorithm (k-NN) classifies a new item by looking at the examples that resemble it most. We measure the distance between points (here with math.dist), keep the k closest ones and let their labels vote.

No complicated training: everything lives in the data. The choice of k matters: too small and one odd example is enough to get it wrong; too large and different groups get mixed. collections.Counter counts the votes easily.

import math

fruits = [((150, 7), "apple"), ((170, 8), "apple"), ((120, 4), "lemon"), ((110, 3), "lemon")]
new = (160, 7)

def distance(p, q):
    return math.dist(p, q)

closest = min(fruits, key=lambda item: distance(item[0], new))
print("Nearest neighbour:", closest[1])

Start the lesson