Menu
CoddyTech

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:

120 theme park visitors as dots, hours in the park across and rides taken up, in three groups named in the legend: Thrill seekers high on the right, Families in the middle, Quick visitors low on the left

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.7

Now 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 icon

Challenge

Medium

A 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.

  1. Add the labels to guests as a cluster column.
  2. Profile the clusters: their means and their number of guests, sorted by visits_month.
  3. 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 hours

Try 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Unsupervised Learning

Practice on your own: Online Python compiler