Start learning free

LESSON 52 OF 165 · MACHINE LEARNING: MODELS

Groups Without a Label: k-means

Practice

Every model so far learned from a label: the bought_again column held the answer. Sometimes there is no answer column, and you just want to find groups of customers who behave alike. That is clustering, and k-means is the simplest way to do it. It keeps one middle point per group, its center. It puts each customer in the group of the closest center, moves each center to the average of its group, and repeats until no customer changes group.

KMeans(n_clusters=3, n_init=10, random_state=0), from sklearn.cluster, makes a model that looks for 3 groups. n_init=10 runs the search 10 times from different random starts and keeps the tightest groups; random_state=0 makes those starts the same on every run. model.fit(X) gets only the features, no y. After it, model.labels_ gives back one group number per row, 0, 1 or 2, in the same order as the rows. The numbers are just names: group 0 is not the best or the first.

import numpy as np
from sklearn.cluster import KMeans

x = np.array([[1], [2], [3], [20], [21], [22]])
km = KMeans(n_clusters=2, n_init=10, random_state=0)
km.fit(x)
print(km.labels_)   # 1, 2, 3 share one number; 20, 21, 22 share the other

Distance adds up the differences in every column, so the columns must be on one scale first; otherwise spent, in hundreds of dollars, would decide alone. The starting code reads customers.csv and scales visits, orders and spent into scaled. To read the groups, value_counts() gives back how many rows hold each value, biggest count first, and df.groupby("group")[columns].mean() gives back one row per group with the average of each column. Your program prints this (the last two lines come from the course's check):

group
1    131
2    123
0     46
       visits  orders  spent  bought_again
group                                     
0        11.9     5.2  462.3           0.9
1         3.9     1.7   95.5           0.3
2         9.0     3.4  188.6           0.8
Groups of the first 5 customers: [0 2 2 0 1]
Customers it grouped: 300

Your task: make a KMeans with 3 groups, 10 starts and random_state=0 in model, fit it on scaled, and store model.labels_ in a new column df["group"]. Then print each group's size, and each group's average visits, orders, spent and bought_again rounded to 1 digit, both with .to_string().


Assignment
Follow the Coding Tutorial to split the shop's customers into 3 groups with k-means and print what each group's average customer looks like.


Hint
There is no y here: fit on scaled alone. bought_again is never shown to the model, yet its average is very different from group to group.


STEP 1 OF 5
    Ctrl + Enter to check