Machine Learning
Preparing data
Prepare data for clustering.
Project: customer segmentation
In this project we'll split a shop's 400 customers into groups by monthly spending, number of visits and average basket size. There are no labels; the goal is to give the marketing team meaningful segments.
harcama = ... # monthly TL
ziyaret = ... # monthly visits
sepet = harcama / ziyaret
X = np.column_stack([harcama, ziyaret, sepet])
Basket size is a derived feature: it separates the "big basket" shopper who spends the same amount in few visits from the frequent "small basket" shopper.
Preparation
First the feature ranges are checked, then each is scaled to mean 0 and standard deviation 1. Since clustering is unlabelled there is no train/test split; all customers are clustered.
print(X.min(axis=0).round(0), X.max(axis=0).round(0))
Xs = StandardScaler().fit_transform(X)
print(Xs.std(axis=0).round(2)) # [1. 1. 1.]
When new customers arrive the same scaler and km.predict will be used, so the scaler object must be kept.
Exercise Inspect the customer data
Print the shape of X, then its column means with np.round(..., 0). Scale with StandardScaler and print the column standard deviations of the scaled data with np.round(..., 2).
Show example solution
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
print(X.shape)
print(np.round(X.mean(axis=0), 0))
Xs = StandardScaler().fit_transform(X)
print(np.round(Xs.std(axis=0), 2))
Test yourself
-
Why is scaling a must on this data?
- Because there are no labels
- It isn't needed
- Spending is hundreds of TL, visits single digits; spending dominates the distance
- Clustering speeds up with scaling
Show answer
Spending is hundreds of TL, visits single digits; spending dominates the distance — Unscaled KMeans in practice clusters only by spending.
-
Scale the features.
Xs = StandardScaler().____(X)
Show answer
Xs = StandardScaler().fit_transform(X)
-
Match the features.
- harcama
- Monthly TL
- ziyaret
- Monthly visit count
- sepet
- Derived: TL / visit
- Xs
- Scaled matrix
Next lesson: Training KMeans