Start learning free

LESSON 53 OF 165 · MACHINE LEARNING: MODELS

How Many Groups? in Python

Practice

k-means makes exactly as many groups as you ask for with n_clusters. It never tells you the right number. To judge a k, measure how tight its groups are. After fit, model.inertia_ gives back one number, the inertia: each customer's distance to its group's center, squared, all added up. Smaller means tighter groups.

Here is the inertia of two groups, using six numbers instead of customers:

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.inertia_)   # 4.0: centers 2 and 21; four numbers are 1 away, two sit on a center

The catch: inertia always falls when you add a group, because every point can only get closer to a center. In one group, the six numbers above have an inertia of 545.5. Two groups bring it down to 4.0, and with one group per number it reaches 0. So the smallest inertia is not the answer. Instead, look for the elbow: the k after which an extra group lowers the inertia much less. To find it, print the drop at each k: the inertia before it minus its own. In a list, inertias[-1] is the last item and inertias[-2] is the one before it.

The starting code scales visits, orders and spent into scaled and makes an empty list inertias. Your program prints this:

k = 1 inertia: 900.0
k = 2 inertia: 477.3
  drop: 422.7
k = 3 inertia: 350.1
  drop: 127.2
k = 4 inertia: 283.6
  drop: 66.5
k = 5 inertia: 239.5
  drop: 44.1
k = 6 inertia: 207.9
  drop: 31.6

Your task: loop over k from 1 to 6 with range(1, 7). Each time, make a KMeans with n_clusters=k, n_init=10 and random_state=0 in model, and fit it on scaled. Add model.inertia_, rounded to 1 digit, to inertias, then print "k =", k, "inertia:" and the rounded inertia. When k is more than 1, also print " drop:" and the last-but-one inertia minus the last one, rounded to 1 digit.


Assignment
Follow the Coding Tutorial to print the inertia for 1 to 6 groups and how much each extra group lowers it.


Hint
Guard the drop line with if k > 1:. At k = 1 the list holds only one inertia, so there is nothing to subtract from.


STEP 1 OF 7
    Ctrl + Enter to check

    Ml Choose K in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free