CodeBlanks Logo Icon CodeBlanks

Label analysis

Analyze cluster labels.

Segment profiles

Clusters are only valuable once interpreted. The means of each segment in original units are computed and the segments are named:

for k in range(3):
    uye = X[km.labels_ == k]
    print(k, len(uye), uye.mean(axis=0).round(0))
# e.g. low spending + few visits  -> "occasional"
#      high spending + few visits -> "big basket"
#      medium spending + many visits -> "loyal frequent"

Means in original units are reported rather than scaled centres; the marketing team talks TL, not z-scores.

Tying segment names to code

Because cluster numbers may change on every run, names aren't hard-coded to numbers; they are derived from the centres' properties. For example, the cluster with the highest mean visits is picked as "loyal":

ort = np.array([X[km.labels_ == k].mean(axis=0) for k in range(3)])
sadik = ort[:, 1].argmax()           # highest visits
buyuk = ort[:, 2].argmax()           # biggest basket
print(sadik, buyuk)

This approach prevents names from drifting to the wrong cluster when the model is retrained.

Practice Extract the profiles

Collect each segment's means in original units in the array ort and print it with np.round(ort, 0). Then print the numbers of the segments with the most visits and the biggest basket on one line.

Starter code

model.py
import numpy as np

rng = np.random.default_rng(5)
n = 400
grup = rng.integers(0, 3, n)
harcama = np.array([300, 1500, 800])[grup] + rng.normal(0, 120, n)
ziyaret = np.array([2, 4, 12])[grup] + rng.normal(0, 1.2, n)
sepet = harcama / np.maximum(ziyaret, 1)
X = np.column_stack([harcama, ziyaret, sepet])
from sklearn.preprocessing import StandardScaler
Xs = StandardScaler().fit_transform(X)
from sklearn.cluster import KMeans
km = KMeans(n_clusters=3, n_init=10, random_state=0).fit(Xs)

# buraya yaz

Sample solution

model.py
import numpy as np

rng = np.random.default_rng(5)
n = 400
grup = rng.integers(0, 3, n)
harcama = np.array([300, 1500, 800])[grup] + rng.normal(0, 120, n)
ziyaret = np.array([2, 4, 12])[grup] + rng.normal(0, 1.2, n)
sepet = harcama / np.maximum(ziyaret, 1)
X = np.column_stack([harcama, ziyaret, sepet])
from sklearn.preprocessing import StandardScaler
Xs = StandardScaler().fit_transform(X)
from sklearn.cluster import KMeans
km = KMeans(n_clusters=3, n_init=10, random_state=0).fit(Xs)

ort = np.array([X[km.labels_ == k].mean(axis=0) for k in range(3)])
print(np.round(ort, 0))
print(ort[:, 1].argmax(), ort[:, 2].argmax())

Expected output

[[ 303.    2.  181.]
 [1505.    4.  422.]
 [ 787.   12.   65.]]
2 1

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the problem with reporting the centres in scaled data?

  • The values can't be read in TL or visits Correct answer
  • They are wrong
  • They can't be computed
  • There is no problem

Explanation: z-scores describe relative position; business language needs the original units.

2 Find the segment with the highest mean visits.

Question
sadik = ort[:, 1].____()
Answer
sadik = ort[:, 1].argmax()

Explanation: argmax gives the cluster number, not the value.

3 Match the profiles with the names.

Term Meaning
Low spend, few visits Occasional
High spend, few visits Big basket
Medium spend, many visits Loyal frequent
ort[:, 1].argmax() Tying the name

All lessons in Intermediate