Recap: Delivery Zones
Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 10 of 51.
Challenge
EasyRemember 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.
- Scale both columns.
- 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.
- 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: 3and its map:
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
1Finding Groups
Learning Without LabelsMeasuring SimilarityK-Means by HandK-Means in scikit-learnRecap: Gym Members7PCA in Practice
Seeing 64 DimensionsComponents as PatternsCompress and RebuildPCA Before a ModelRecap: Face Scanner10Project: Digit Explorer
Look at the DigitsShrink with PCACluster the DigitsName the ClustersFind the Odd OnesPractice on your own: Online Python compiler