Ask the Nearest Leaves

Classic MLMedium+20 XP+20

Ask the Nearest Leaves

A garden centre identifies leaves from two measurements, length and width in centimetres, by comparing each new leaf with leaves it has already labelled. That is K-Nearest Neighbors: no formula is learned; every prediction is a fresh look at the stored examples.

To classify one new point, KNN:

  1. measures the straight-line (Euclidean) distance from the new point to every training point, using all of the features;
  2. takes the kkk closest training points, its nearest neighbours;
  3. lets each of those kkk neighbours cast one vote for its own label;
  4. predicts the label with the most votes.

Real data needs two tie rules, and you must follow both exactly:

  • Equal distances. If two training points are exactly the same distance from the new point, the one with the smaller index in X_train counts as nearer. This decides which points make it into the kkk.
  • Equal votes. If two or more labels share the top vote count, the winner is the tied label whose neighbour is closest to the new point: go through the kkk neighbours from nearest to farthest (in the order the first rule fixes) and take the first one whose label is tied for the top.

Task: write knn_classify(X_train, y_train, queries, k).

  • X_train is a list of points, each a list of numbers; y_train holds one label per point. Labels may be strings or integers.
  • queries is a list of new points with the same number of features.
  • k is a whole number from 1 up to the number of training points.

Return a list with one predicted label per query, in the same order as queries.

Inside K-Nearest Neighbors

The dashed point is unknown. Find its 3 nearest neighbors and let them vote.

?

Examples

X_train = [[4.0, 2.0], [4.5, 2.2], [6.0, 4.0], [6.5, 4.4], [5.0, 2.1], [6.2, 3.8]]
y_train = ['ash', 'ash', 'oak', 'oak', 'ash', 'oak']
queries = [[4.6, 2.4], [6.1, 4.1], [5.4, 3.0]]
k = 3
returns ['ash', 'oak', 'oak']
X_train = [[1.0, 1.0], [3.0, 1.0], [1.0, 4.0], [4.0, 4.0], [6.0, 6.0]]
y_train = ['elm', 'ash', 'elm', 'ash', 'oak']
queries = [[1.5, 1.5]]
k = 4
returns ['elm']

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run