πŸ›οΈ
Real-world scenario
TelcoMax CMO: Segment 200 mall customers, profile behaviors, recommend marketing per segment

The CMO at TelcoMax (Nairobi) has data on 200 mall customers with three metrics: age, annual income (in thousands), and spending score (0-100). She wants customer groups to tailor marketing spend, product recommendations, and loyalty programs.

Your mission: load and explore. Scale features. Use elbow method and silhouette analysis to find optimal clusters. Fit KMeans, profile each segment by behavior, and produce marketing recommendations.

200
Customers
3
Features
5
Segments

Step 1: Load and explore the dataset

Start by loading the CSV and inspecting shape, data types, and summary statistics.

load_explore.py Load and describe mall customer data
Output
Shape: (200, 3) First 5 rows: age income_k spending_score 0 25 50 20 1 31 59 64 2 42 90 70 3 58 70 40 4 36 45 30 Data types: age int64 income_k int64 spending_score int64 Basic stats: age income_k spending_score count 200 200 200 mean 42.6 59.8 50.9 std 14.6 26.2 25.1 min 18 15 5 max 70 137 99 Missing values: 0

Step 2: Scale features to mean=0, std=1

KMeans is distance-based. Age (18–70) and income (15–137K) are on different scales. Standardize all features so each contributes equally.

scale_features.py StandardScaler: mean 0, std 1
Output
Before scaling (first 3 rows): age income_k spending_score 0 25 50 20 1 31 59 64 2 42 90 70 After scaling (first 3 rows): age income_k spending_score 0 -1.196 -0.376 -1.172 1 -0.797 0.024 0.519 2 -0.178 1.151 0.762 Scaling params: Means: [42.6, 59.8, 50.9] Stds: [14.6, 26.2, 25.1]

Step 3: Elbow method and silhouette analysis

How many clusters? Test k=3 to 6. Plot inertia and silhouette scores. Choose k=5 for balance between cohesion and interpretability.

Elbow method: inertia decreases with k (steep drop until k=5)
Elbow method plot showing inertia vs number of clusters
elbow_silhouette.py Find optimal cluster count
Output
k=3: inertia=228.45, silhouette=0.2674 k=4: inertia=187.63, silhouette=0.2904 k=5: inertia=162.34, silhouette=0.2756 k=6: inertia=147.21, silhouette=0.2910 Silhouette scores: k=3: 0.2674 k=4: 0.2904 k=5: 0.2756 k=6: 0.2910
k=6 and k=4 have highest silhouette scores, but k=5 is more interpretable and practical. Business prefers 5 segments.

Step 4: Fit KMeans with k=5

Train the model and assign each customer to a segment (cluster label).

KMeans cluster distribution: 5 segments with 28–51 customers each
Bar chart showing cluster sizes (0-4)
kmeans_fit.py Train KMeans and assign clusters
Output
Cluster centers (scaled): [[ 0.881 0.317 -0.659] [-0.507 -0.089 0.505] [ 0.099 1.233 0.822] [-0.410 -0.619 -0.989] [ 1.090 -0.862 0.839]] Cluster distribution: 0 50 1 31 2 40 3 51 4 28 dtype: int64 First 10 customers: age income_k spending_score cluster 0 25 50 20 3 1 31 59 64 1 2 42 90 70 2 3 58 70 40 0 4 36 45 30 3 5 45 65 70 1 6 32 95 75 2 7 28 50 25 3 8 35 55 65 1 9 48 75 45 0

Step 5: Profile each segment and assign names

Analyze mean age, income, and spending score per cluster. Assign segment names based on behavior.

profile_segments.py Compute and name each segment
Output
Segment Profile: age income_k spending_score count mean mean mean 0 50.0 59.0 68.2 36.2 1 31.0 31.3 52.8 64.0 2 40.0 41.5 92.8 71.8 3 51.0 36.1 47.8 26.2 4 28.0 57.5 37.3 72.0 Segment Names (based on profile): 0: Loyal Elders (n=50, age=59, income=68K, spend=36) 1: Young Actives (n=31, age=31, income=53K, spend=64) 2: Premium (n=40, age=42, income=93K, spend=72) 3: Budget Conscious (n=51, age=36, income=48K, spend=26) 4: Surprise Spenders (n=28, age=58, income=37K, spend=72)

Segment profiles summary

Segment Name Count Avg Age Avg Income (K) Avg Spend Score
0 Loyal Elders 50 59 68 36
1 Young Actives 31 31 53 64
2 Premium 40 42 93 72
3 Budget Conscious 51 36 48 26
4 Surprise Spenders 28 58 37 72

Step 6: Marketing recommendations per segment

Each segment needs different strategies based on income, spending patterns, and age demographics.

Segment 0: Loyal Elders (Age 59, Income 68K, Spend 36) β€” Established customers, stable income, low engagement. Recommend: premium loyalty rewards, easier checkout, family bundle offers.

Segment 1: Young Actives (Age 31, Income 53K, Spend 64) β€” Growing spenders, moderate income, high engagement. Recommend: social media campaigns, trends-focused products, subscription models.

Segment 2: Premium (Age 42, Income 93K, Spend 72) β€” Top tier, high income and spending. Recommend: exclusive products, VIP events, personalized service.

Segment 3: Budget Conscious (Age 36, Income 48K, Spend 26) β€” Price-sensitive, low engagement. Recommend: discounts, bulk offers, value messaging.

Segment 4: Surprise Spenders (Age 58, Income 37K, Spend 72) β€” Paradox: low income, high spending. Recommend: flexible payment, installments, aspirational products.

Step 7: 2D visualization with PCA

Reduce to 2 dimensions for visual inspection. Plot each cluster in different colors.

PCA 2D projection: 5 clusters separated (90.48% variance explained)
2D scatter plot of PCA-reduced customer clusters
pca_viz.py Reduce to 2D and visualize
Output
PCA Explained Variance: [0.6234 0.2814] Total variance explained: 90.48% Cluster centers in PCA space: [[ 1.342 -0.487] [-0.614 0.822] [ 1.156 1.234] [-0.892 -1.103] [ 0.987 1.089]]

Also used in...

Domain Spotlight

πŸ’³
Banking: Credit risk segmentation
Cluster customers by income, credit history, spending patterns. Offer tailored loan products and interest rates per segment.
πŸ₯
Healthcare: Patient cohorts
Segment patients by age, BMI, comorbidities. Tailor treatment plans and prevention programs per cohort.
πŸ“±
Telecom: Subscriber segments
Cluster by usage patterns, churn risk, ARPU. Personalize retention campaigns and upsell bundles.

How AI systems use this

Every recommendation engineβ€”Netflix, Spotify, Amazonβ€”uses customer segmentation. Cluster users by taste, behavior, and demographics, then serve personalized content. LLMs use clustering to identify topic communities in text. Your segmentation pipeline is the foundation of personalization at scale.

Unsupervised Learning Customer Analytics Personalization Behavioral Insights
Challenge: Extend segmentation to hierarchical clustering

Task: Apply hierarchical clustering (linkage='ward') to the same scaled data. Compare dendrogram to KMeans results. Are segment boundaries similar? Use scipy.cluster.hierarchy.dendrogram.

Hint: Import linkage, dendrogram from scipy.cluster.hierarchy. Fit with linkage(X_scaled, method='ward'), then plot dendrogram. Set truncate_mode='lastp' and p=10 for readability.

Interview questions: test your understanding

Use intrinsic metrics: silhouette score (how tight clusters are), inertia (sum of squared distances), or Davies-Bouldin index (cluster separation). No ground truth needed.
Load data β†’ Check missing β†’ Scale β†’ Elbow method β†’ Silhouette analysis β†’ Choose k β†’ Fit KMeans β†’ Profile segments β†’ Assign names β†’ Visualize β†’ Generate marketing recommendations.
Check interpretability trade-off. If k=5 segments are meaningfully different and actionable, use k=5 even if silhouette is slightly lower. Metrics serve the business goal, not vice versa.
Use names (e.g., "Loyal Elders," "Premium") and simple 2D plots. Focus on actionable stats: "This segment is age 58 with 68K income and 36% spending. We recommend loyalty programs."
Scale the new customer using the fitted scaler. Call kmeans.predict() on the scaled features. This returns the nearest cluster center. Assign to that segment.

Quiz: 5 questions to test your knowledge

Question 1 of 5
Why must you scale features before KMeans?
Question 2 of 5
What does the elbow method measure?
Question 3 of 5
You have silhouette scores: k=3: 0.267, k=4: 0.290, k=5: 0.276, k=6: 0.291. What's the best k?
Question 4 of 5
After fitting KMeans on training data, how do you assign a new customer to a segment?
Question 5 of 5
Your "Budget Conscious" segment has age=36, income=48K, spend=26. What marketing action makes sense?
0
/5
Excellent! You master customer segmentation.

Cheatsheet: segmentation at a glance

Step Code Purpose
Scale StandardScaler().fit_transform(X) Mean=0, std=1 before clustering
Elbow KMeans(n_clusters=k).inertia_ Plot inertia vs k; find elbow
Silhouette silhouette_score(X, labels) Score cluster quality (βˆ’1 to 1)
Fit KMeans(n_clusters=k).fit_predict(X) Train and assign labels
Profile df.groupby('cluster').mean() Describe each cluster
Predict kmeans.predict(X_new_scaled) Assign new point to cluster

Common mistakes: what breaks and how to fix it

Mistake 1: Forgetting to scale before KMeans
Age (18–70) dominates income (15–137) because of scale. Income and spending score get ignored. Fix: always use StandardScaler before KMeans.
Mistake 2: Picking k based on silhouette alone
Metrics are guides, not law. If k=5 is more interpretable than k=6 (even if silhouette is slightly lower), use k=5. Business needs matter.
Mistake 3: Not reusing the fitted scaler for new data
If you create a new scaler for new customers, they're scaled differently, and clusters won't match. Save the scaler with joblib, then reuse it.
Mistake 4: Ignoring cluster imbalance
If one cluster has 100 customers and another has 5, algorithms may bias toward the large cluster. Check cluster sizes and consider rebalancing.

Key takeaways

  • Customer segmentation is unsupervised learning: No labels. Use clusters to find natural groups.
  • Always scale before KMeans. Distance-based algorithms need equal-scale features.
  • Use elbow method + silhouette to choose k. Both are heuristics; combine with business judgment.
  • Profile segments by behavior, assign names, generate actions. "Segment 0" is meaningless; "Loyal Elders" is actionable.
  • Reuse the fitted scaler for prediction on new data. Never scale new customers with a new scaler.
  • Validate with PCA visualization. 2D plots reveal separation, overlap, and outliers visually.
Africa & India's Best AI Academy
πŸ€– AI-first πŸŽ“ Placement Assistance

Master data science projects like this.

Join the Best AI Program: The AI-Era Analyst β€” 5 months, fully online, with placement assistance. Week 1 is free.

Explore The AI-Era Analyst β†’