K-Means Clustering and Choosing K
Explore how K-means clustering groups customers based on behavior by standardizing features to ensure fair distance measurement. Learn to assign clusters, calculate centroids, and evaluate model quality using inertia and silhouette scores. Understand how to choose the number of clusters (K) using diagnostic tools like the elbow method and silhouette analysis, while considering practical business constraints. This lesson helps you create meaningful customer segments for targeted marketing actions and interpret clustering results effectively.
K-means groups retail customers when no outcome column tells us which customers belong together. For example, a marketing team may need segments based on annual_spend and visits_per_month before deciding whether to send a discount, a loyalty offer, or a reactivation message.
Clustering groups data points so members of the same group are more similar to one another than to members of other groups. Similarity in K-means comes from distance between numeric feature values. Feature scale is the numeric magnitude and unit of a feature. Dollar amounts such as 1200 and visit counts such as 4 occupy very different scales.
That difference creates a baseline failure. If K-means receives unscaled annual_spend values and visits_per_month values, differences measured in dollars can overwhelm differences in visit behavior. The resulting segments may mostly separate customers by spending amount, even when visit frequency should influence the marketing decision.
Scale customer behavior before measuring distance
The customer table contains two useful but differently scaled columns. Standardization transforms each column so its mean becomes 0 and its standard deviation becomes 1. After standardization, a one-standard-deviation difference in annual spending has the same distance weight as a one-standard-deviation difference in monthly visits.
Lines 6–17: The code builds a table of eight customers, then prints the raw rows and their descriptive statistics.
Lines 19–25:
StandardScaler().fit_transform(...)standardizes each column independently (subtract ...