Detect unstable customer segments after retraining
In this article (3 sections)
Cluster labels have no inherent identity. A retrained model can call the same profile “cluster 2” instead of “cluster 0.” Counting raw label changes without alignment can report migration that is only renaming.
Align profiles before rows
Our synthetic second period perturbs all 360 customer profiles. We fit separate standardized three-cluster K-means models to the original and new periods. A cost matrix measures squared distance between old and new centroids, and the Hungarian assignment finds a one-to-one mapping that minimizes total distance.
In this seed, the optimal new-to-old mapping happens to be 0→0, 1→1 and 2→2. Raw and aligned match rates are both 98.89%, leaving four customer migrations. The alignment still matters: another initialization could permute every integer label while preserving profiles.
from unsupervised_cases import segment_drift_case
r = segment_drift_case()
assert r['customers'] == 360
assert r['aligned_match_rate'] >= r['raw_label_match_rate']
assert r['aligned_migrations'] == 4
assert r['mapping_new_to_old'] == {0: 0, 1: 1, 2: 2}
print(r)Run it in the unsupervised lab. The mapping is calculated, not assumed, even when it reduces to the identity.
Separate label swaps, profile drift and customer movement
After alignment, compare centroid profiles, cluster sizes and within-cluster spread. A stable assignment rate can hide centroid drift affecting every customer. A large migration rate can be real behaviour change or a shifted scaler and feature pipeline.
Use a fixed reference scaler when the goal is comparable coordinates, or document why a new scaler is necessary. Version feature definitions and missing-value handling. Compare models on a common eligible population before attributing changes to customers.
Define a migration threshold and persistence rule. Customers near a boundary can oscillate with small noise. Hysteresis or minimum-duration requirements may improve operations, but they change the policy and need validation.
Do not infer that moving from one segment to another caused an outcome. Segment migrations are model assignments. If actions differ by segment, audit how retraining changes eligibility and protect in-flight experiments.
Exercise: permute new cluster labels deliberately and verify that raw match falls while aligned match stays unchanged. Then refit the scaler on a shifted population and decompose assignment changes into scaling, centroid and customer-feature components.
NeuraPath's Data Science course teaches segment monitoring with explicit label alignment. A retraining report should distinguish renaming from behavioural change.
Continue learning
This article is part of the Clustering, reduction and recommendations sequence. Use the neighbouring tasks when you need the prerequisite or the next application.
- Review the prerequisite or neighbouring task in Isolation forests with an analyst-review budget.
- Continue with Decide when rules are better than unsupervised learning.
Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.
This article is part of our Data Science programme — 6 months. From data foundations to machine learning, deep learning and deployment.
Explore Data Science