Beauty Segmentation
ML Pipeline

CUSTOMER INTELLIGENCE SYSTEM · ANOMALY DETECTION · PREDICTIVE ENGINE

01 Preprocess
02 Anomaly
03 PCA
04 Cluster
05 Metrics
06 K-Fold
07 Segments
08 Predict
Data Preprocessing
// impute → encode → normalize → ready
Total Records
1,000
Customers
Features
9
After encoding
Missing Filled
150
Median imputed
Segments
4
Classes
Feature Pipeline
AgeNumeric · No missing
IncomeNumeric · Imputed
Spending_ScoreNumeric · Imputed
Purchase_FrequencyNumeric · No missing
Avg_Order_ValueNumeric · No missing
> Shriguru:
Service_UsageNumeric · Imputed
Last_Purchase_DaysNumeric · No missing
GenderLabel Encoded
Product_CategoryLabel Encoded
Preprocessing Steps
1
Load CSV
beauty_customers.csv — 1000 rows, 11 cols
2
Median Imputation
Income, Spending_Score, Service_Usage (5% each)
3
Label Encoding
Gender → {0,1,2} · Product_Category → {0–5}
4
StandardScaler
Zero mean, unit variance — critical for clustering & PCA
5
Train / Test Split
80 / 20 stratified · Train=800, Test=200
Anomaly Detection
// IsolationForest · contamination=0.05 · flags unbiased-activity customers
Anomalies Found
50
Suspicious behavior
Clean Customers
950
Normal patterns
Detection Rate
5%
Contamination threshold
How It Works
Isolation Forest isolates anomalies by randomly partitioning the feature space. Anomalous customers (those buying via unbiased/unusual patterns) are easier to isolate — requiring fewer splits.

Flagged as anomaly when:
· Extreme spending vs income mismatch
· Purchase frequency outliers
· Unusual service usage spikes
· Recency patterns inconsistent with segment
Anomalous Customer IDs (first 20)
PCA — Feature Reduction
// 9 features → 7 principal components · 95% variance retained
Original Features
9
PCA Components
7
Retained at 95% threshold
Variance Kept
95%
Explained Variance per Component
PC1 dominates with 47.1% variance — primarily encodes spending power and income signals. PC2–PC4 each carry ~11% capturing purchase frequency, recency, and service usage patterns.
K-Means Clustering
// silhouette method to find optimal K · best K=2 (data-driven)
Optimal K
2
By silhouette score
Silhouette Score
0.3315
Best cluster separation
Algorithm
K-Means
n_init=10 · random_state=42
Silhouette Score by K
Silhouette score ranges from -1 to 1. Higher = better defined clusters. K=2 achieves 0.3315 (solid separation for real-world customer data). K=4 is second-best at 0.3039 — aligns with our 4 true business segments.
Model Performance Metrics
// Random Forest Classifier · 200 trees · trained on PCA-reduced features
Accuracy
98.0%
Precision
98.1%
Recall
98.0%
F1 Score
98.0%
Per-Class Performance
SegmentPrecisionRecallF1 ScoreSupport
Budget100.0%98.3%99.2%60
Frequent94.3%100.0%97.1%50
Premium100.0%95.0%97.4%40
Trend98.0%98.0%98.0%50
PC Component Importance (Random Forest)
K-Fold Cross Validation
// StratifiedKFold · 5 folds · shuffle=True · scoring=accuracy
CV Mean Accuracy
98.2%
Std Deviation
±0.68%
Very stable model
Folds
5
Stratified splits
Per-Fold Accuracy
Low standard deviation (0.68%) confirms the model is not overfitting. Stratified splits ensure each fold has proportional class distribution, giving reliable generalization estimates.
Customer Segments
// business interpretation of each cluster
💎
Premium / Luxury
High income earners with maximum spending scores. Order premium skincare, fragrance. Loyal to brands, low recency days means frequent visits.
₹80k–₹1.5LIncome
70–100Spend Score
₹3k–₹8kAvg Order
3–8Salon/mo
🔄
Frequent Visitors
Mid-income customers who visit most often. Regular salon users. Moderate order values but high purchase frequency makes them high lifetime value.
₹40k–₹75kIncome
55–80Spend Score
12–20Freq/mo
4–10Salon/mo
💰
Budget Regulars
Price-conscious, lower income but consistent buyers. Respond best to discounts, bundles, and loyalty rewards. High count — biggest segment.
₹15k–₹35kIncome
20–50Spend Score
3–8Freq/mo
0–2Salon/mo
✨
Trend Drivers
Young, social-media-influenced buyers aged 18–30. High spending score despite moderate income. Cosmetics & skincare trend chasers. Influencer-driven.
₹25k–₹60kIncome
60–90Spend Score
Age 18–30
2–5Salon/mo
Predict Unknown Customer
// enter customer features → model predicts segment + anomaly status
PREDICTED SEGMENT
CONFIDENCE PER SEGMENT