15.4.3. Homework#
Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.
Sign in from the account menu before you start working on the assignment.
Write and run your code directly in the coding cells.
Multiple submissions are allowed. The highest score is kept.
1. Labels
Like a classifier, k-means needs training examples whose group labels are already known.
2. Scaling
Customers are described by annual spend (in dollars) and number of orders. Converting both features to standard units before k-means keeps spend from dominating the distances.
3. Smallest Inertia
Inertia always decreases as k increases, so the value of k with the smallest inertia is the best choice.
4. Silhouette Score
A silhouette score close to 1 means that points are much closer to their own cluster than to the nearest other cluster.
5. Cluster Numbers
In a k-means result, cluster 0 is always the most important segment, and cluster 0 in one run is the same group as cluster 0 in another run.
### 6. Assigning Spend to Two Centers
# One round of the assignment step on one feature.
# 1. Assign each monthly spend value to the nearer center (0 or 1).
# 2. Print the assignments and the size of each cluster.
# Print exactly:
# Assignments: [0, 0, 1, 0, 1, 1, 1]
# Sizes: [3, 4]
import numpy as np
import pandas as pd
spend = pd.Series([15, 22, 61, 30, 75, 58, 90])
centers = [20, 70]
### Your code starts here.
### Your code ends here.
Assignments: [0, 0, 1, 0, 1, 1, 1]
Sizes: [3, 4]
### 7. Inertia by Hand
# Inertia is the sum of squared distances from each point to its own cluster's center.
# 1. Compute the center of each cluster (the mean of its points).
# 2. Compute the inertia.
# Print exactly:
# Centers: [[1.5, 1.5], [9.0, 8.0]]
# Inertia: 5.0
import numpy as np
import pandas as pd
points = pd.DataFrame({
"x": [1, 2, 8, 10, 9],
"y": [2, 1, 9, 7, 8],
"cluster": [0, 0, 1, 1, 1],
})
### Your code starts here.
### Your code ends here.
Centers: [[1.5, 1.5], [9.0, 8.0]]
Inertia: 5.0
### 8. Clustering Stores
# Twelve stores are described by foot traffic and basket size, in standard units.
# 1. Fit KMeans(n_clusters=3, n_init=10, random_state=0).
# 2. Print the cluster sizes, sorted from smallest to largest.
# Print exactly:
# Cluster sizes: [4, 4, 4]
import numpy as np
import pandas as pd
from sklearn.cluster import KMeans
stores = pd.DataFrame({
"traffic_su": [-1.2, -1.0, -1.3, -0.9, 0.1, 0.3, 0.0, 0.2, 1.3, 1.1, 1.4, 1.2],
"basket_su": [1.1, 0.9, 1.2, 1.0, -1.0, -1.2, -0.9, -1.1, 0.2, 0.0, 0.3, 0.1],
})
### Your code starts here.
### Your code ends here.
Cluster sizes: [4, 4, 4]
### 9. How Many Store Groups?
# 1. For k = 2, 3, and 4, fit KMeans(n_clusters=k, n_init=10, random_state=0) to
# stores and compute the silhouette score, rounded to 3 decimals.
# 2. Print each score, then the k with the highest score.
# Print exactly:
# k=2: 0.682
# k=3: 0.864
# k=4: 0.773
# Best k: 3
import numpy as np
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
stores = pd.DataFrame({
"traffic_su": [-1.2, -1.0, -1.3, -0.9, 0.1, 0.3, 0.0, 0.2, 1.3, 1.1, 1.4, 1.2],
"basket_su": [1.1, 0.9, 1.2, 1.0, -1.0, -1.2, -0.9, -1.1, 0.2, 0.0, 0.3, 0.1],
})
### Your code starts here.
### Your code ends here.
k=2: 0.682
k=3: 0.864
k=4: 0.773
Best k: 3
### 10. Profiling Segments
# K-means has assigned nine customers to clusters.
# 1. Use groupby to compute each cluster's mean visits and mean spend.
# 2. Find the cluster with the highest mean visits and print its mean spend
# and its number of customers.
# Print exactly:
# Most frequent visitors: cluster 1
# Their mean spend: 45.0
# Customers: 3
import numpy as np
import pandas as pd
segments = pd.DataFrame({
"visits": [2, 3, 12, 14, 1, 13, 6, 5, 7],
"spend": [120, 140, 40, 50, 100, 45, 80, 70, 90],
"cluster": [0, 0, 1, 1, 0, 1, 2, 2, 2],
})
### Your code starts here.
### Your code ends here.
Most frequent visitors: cluster 1
Their mean spend: 45.0
Customers: 3