Profiling Clusters
Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 11 of 51.
This chapter turns cluster numbers into groups people can use. By its end, every cluster has a name:
A number like 2 says nothing. Its profile does: the mean of each feature inside the cluster. Add the labels as a column, then group:
park["cluster"] = km.labels_
print(park.groupby("cluster").mean().round(1))This prints one row per cluster:
hours rides age
cluster
0 6.0 6.2 38.4
1 2.4 3.0 45.8
2 6.8 13.9 20.7Now read the rows as words. Cluster 2 is young, stays 7 hours and takes 14 rides: thrill seekers. Cluster 1 leaves early: quick visitors.
Challenge
MediumA karaoke bar wants to know its guests. The starter splits them into 3 clusters; names holds a name for each, from fewest visits a month to most.
- Add the labels to
guestsas aclustercolumn. - Profile the clusters: their means and their number of guests, sorted by
visits_month. - Print one line per cluster with its name, means to 1 decimal.
For another bar:
Once in a while: 30 guests, 1.4 visits a month, 2.5 songs, 3.2 hours
Regulars: 45 guests, 3.0 visits a month, 4.6 songs, 2.0 hours
Superfans: 15 guests, 7.4 visits a month, 9.2 songs, 2.8 hoursTry it yourself
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
# 110 guests of a karaoke bar: visits a month, songs a visit, hours a visit
guests = pd.read_csv("karaoke.csv")
scaled = StandardScaler().fit_transform(guests)
km = KMeans(n_clusters=3, n_init=10, random_state=0)
km.fit(scaled)
# Names for the groups, from fewest visits a month to most
names = ["Once in a while", "Regulars", "Superfans"]
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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 Ones3Reading Clusters
Profiling ClustersCenters in Real UnitsClusters vs Known LabelsWhen K-Means FailsRecap: Tea TastingPractice on your own: Online Python compiler