Menu
CoddyTech

Recap: Delivery Zones

Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 10 of 51.

challenge icon

Challenge

Easy

Remember the zone map that opened this chapter? Now draw yours. A bakery's van serves 150 stops, and stops.csv stores east in km but north in meters.

  1. Scale both columns.
  2. For k from 2 to 7, fit KMeans on the scaled rows; print its inertia (1 decimal) and silhouette (3 decimals). Then print the k with the best silhouette.
  3. Map that k's zones in real units: each zone's stops labelled with their count, like 50 stops, a black star at each zone's mean, a legend.

For another city:

k = 2: inertia 109.5, silhouette 0.535
k = 3: inertia 22.8, silhouette 0.737
k = 4: inertia 18.3, silhouette 0.597
k = 5: inertia 14.5, silhouette 0.443
k = 6: inertia 12.7, silhouette 0.368
k = 7: inertia 11.2, silhouette 0.364
Zones: 3

and its map:

A map titled Delivery Zones, east in km across and north in meters up: three zones of stops, 45 at the lower left, 40 at the upper right, 25 at the lower right, each with a black star at its middle and a legend naming each zone by its number of stops

Then Coddy describes your map.

Try it yourself

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import silhouette_score

# One row per delivery stop: east in km, north in meters (the old system's unit)
stops = pd.read_csv("stops.csv")
X = stops[["east_km", "north_m"]]

fig, ax = plt.subplots()
ax.set_title("Delivery Zones")
ax.set_xlabel("East (km)")
ax.set_ylabel("North (m)")

# Write your code below

All lessons in Unsupervised Learning

Practice on your own: Online Python compiler