Which Delivery Zone?

Machine Learning in CodeEasy+10 XP+10

Which Delivery Zone?

A courier company splits a town into a "north" and a "south" zone. You have the map positions of some known addresses (points, as [x, y] in kilometres) and their zones (zones). Both columns are in kilometres, so no scaling is needed.

Write zone_for(points, zones, new_points, k).

  • Fit a KNeighborsClassifier(n_neighbors=k) on the known addresses.
  • Predict the zone for every point in new_points, in order.

Return a list of zone names (strings).

Examples

points = [[1.2, 8.5], [2.1, 9.3], [0.8, 7.6], [1.9, 8.1], [7.4, 2.2], [8.3, 1.5], [6.9, 3.1], [7.8, 0.9], [4.6, 6.2], [5.3, 4.4]]
zones = ['north', 'north', 'north', 'north', 'south', 'south', 'south', 'south', 'north', 'south']
new_points = [[2.0, 7.0], [6.5, 2.5]]
k = 3
returns ['north', 'south']
points = [[1.2, 8.5], [2.1, 9.3], [0.8, 7.6], [1.9, 8.1], [7.4, 2.2], [8.3, 1.5], [6.9, 3.1], [7.8, 0.9], [4.6, 6.2], [5.3, 4.4]]
zones = ['north', 'north', 'north', 'north', 'south', 'south', 'south', 'south', 'north', 'south']
new_points = [[4.9, 5.6]]
k = 1
returns ['north']

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