Skip to content
C

Customer Segmentation with K-Means

Mediumpython

Group customers into 3 clusters using K-Means based on age and spending, and report which customers ended up in the same group together.

Why report groupings instead of raw cluster numbers? K-Means assigns each cluster an arbitrary integer label (0, 1, 2, ...) that isn't guaranteed to come out the same way across every environment, even with a fixed random_state — but WHICH customers end up grouped together is stable. Printing customer-index groups (sorted by their smallest member) sidesteps that arbitrary-numbering issue entirely.

Approach: fit KMeans(nclusters=3, randomstate=42, n_init=10) on the customer data, then group customer indices by their assigned label and sort the groups for a deterministic, label-order-independent result.

Input: No input.

Output: One line: the customer index groups, sorted by each group's smallest index, printed as a Python list of lists.

Input (stdin)

Output

Run your code to see output here...