Understanding K-Means Clustering for Customer Segmentation
In today‘s data-driven world, businesses are constantly looking for ways to better understand their customers in order to provide targeted offerings, personalize experiences, and ultimately drive growth. One powerful technique that has gained popularity is customer segmentation through clustering algorithms like k-means.
In this blog post, we‘ll take a deep dive into how k-means clustering works and walk through a practical example of using it to segment customers based on their attributes. By the end, you‘ll have a solid understanding of this important machine learning algorithm and how you can start applying it to glean insights from your own customer data.
Clustering 101
Before we jump into k-means specifically, let‘s make sure we‘re on the same page about what clustering is in the first place. Essentially, clustering is a type of unsupervised machine learning that involves grouping data points together based on their similarity. The goal is to end up with clusters where the data points within each cluster are as similar as possible while being as different as possible from data points in other clusters.
There are several different approaches to clustering, but some of the most common include:
- Centroid-based clustering: Algorithms like k-means that represent each cluster by a central vector or centroid.
- Hierarchical clustering: Algorithms that build nested clusters by merging or splitting them successively to form a tree-like structure.
- Density-based clustering: Algorithms like DBSCAN that define clusters as areas of higher density separated by areas of lower density.
Each approach has its own strengths and weaknesses. For our customer segmentation example, we‘ll be focusing on k-means since it is one of the most widely used clustering algorithms due to its simplicity and efficiency, especially with large datasets.
K-Means Clustering Algorithm Explained
The "k" in k-means refers to the number of clusters we want to group our data into. The key idea is that we‘ll represent each cluster by its centroid (i.e. the mean of all the points in the cluster) and iteratively refine these centroids to optimize the clustering.
Here‘s a high-level overview of the steps in the k-means algorithm:
- Specify the desired number of clusters k.
- Randomly initialize k centroids in the data space.
- Assign each data point to the nearest centroid based on Euclidean distance.
- Recalculate the centroids as the mean of all data points assigned to them.
- Repeat steps 3-4 until the centroids no longer change significantly or a maximum number of iterations is reached.
Let‘s unpack a few key concepts here. First, the Euclidean distance 𝑑 between two points 𝑝 and 𝑞 is calculated as:
𝑑(𝑝,𝑞)=√((𝑞_1−𝑝_1)^2+(𝑞_2−𝑝_2)^2+⋯+(𝑞_𝑛−𝑝_𝑛)²)
where 𝑝_𝑖 and 𝑞_𝑖 are the 𝑖-th attributes of data points 𝑝 and 𝑞 respectively, and 𝑛 is the total number of attributes.
We use Euclidean distance to determine which centroid each data point is closest to. Then once the assignments are made, the new centroids are calculated as:
𝐶_𝑖= (1/|𝑆_𝑖 |)∑𝑥 for 𝑥∈𝑆_𝑖
where 𝐶_𝑖 is the centroid of the 𝑖-th cluster, |𝑆_𝑖| is the number of points in the 𝑖-th cluster, and 𝑥 is a data point in the 𝑖-th cluster 𝑆_𝑖.
By repeating this process of assigning data points to centroids and updating the centroids, the k-means algorithm iteratively improves the clustering until it converges (i.e. when the centroid positions stop changing between iterations).
One thing to note is that the random initialization of centroids means that the algorithm can sometimes reach different solutions on different runs. It‘s often a good idea to run k-means multiple times with different initializations and choose the result with the lowest SSE (sum of squared errors), which measures the total squared Euclidean distance between each point and its assigned centroid.
Customer Segmentation with K-Means: A Worked Example
Now that we understand how k-means works conceptually, let‘s see it in action with a customer segmentation problem. We‘ll be using a dataset of mall customers with the following attributes:
- CustomerID: Unique ID for each customer
- Gender: Male or Female
- Age: Age of the customer
- Annual Income (k$): Annual income of the customer in thousands of dollars
- Spending Score (1-100): A score assigned to the customer based on their spending behavior, with higher scores indicating higher spending
Our goal will be to segment these customers into meaningful groups based on their age and spending score so that we can better understand the different types of customers who visit the mall.
First, let‘s load the necessary libraries and the dataset into a Pandas DataFrame:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.cluster import KMeans
df = pd.read_csv(‘Mall_Customers.csv‘)
print(df.head())
print(df.info())
This gives us a first look at the data:
CustomerID Genre Age Annual Income (k$) Spending Score (1-100)
0 1 Male 19.0 15.0 39.0
1 2 Male 21.0 15.0 81.0
2 3 Female 20.0 16.0 6.0
3 4 Female 23.0 16.0 77.0
4 5 Female 31.0 17.0 40.0
RangeIndex: 200 entries, 0 to 199
Data columns (total 5 columns):
CustomerID 200 non-null int64
Genre 200 non-null object
Age 200 non-null float64
Annual Income (k$) 200 non-null float64
Spending Score (1-100) 200 non-null float64
dtypes: float64(3), int64(1), object(1)
We can see there are 200 customers total, with a mix of numerical and categorical attributes. For our segmentation, we‘ll just be using the Age and Spending Score columns.
Next, let‘s do some exploratory visualization of the age and spending score distributions:
plt.figure(figsize=(15,6))
plt.subplot(1,2,1)
sns.distplot(df[‘Age‘])
plt.title(‘Distribution of Age‘)
plt.subplot(1,2,2)
sns.distplot(df[‘Spending Score (1-100)‘])
plt.title(‘Distribution of Spending Score‘)
plt.tight_layout()
plt.show()

We can observe that the age distribution is slightly right skewed with a peak around 30-40 years old, while the spending score is more uniformly distributed.
Now let‘s set up our data for k-means:
X = df[[‘Age‘ , ‘Spending Score (1-100)‘]].values
inertias = []
for k in range(1,11):
kmeans = KMeans(n_clusters=k, random_state=42)
kmeans.fit(X)
inertias.append(kmeans.inertia_)
plt.figure(figsize=(10,5))
plt.plot(range(1,11), inertias, ‘-o‘)
plt.xlabel(‘Number of clusters‘)
plt.ylabel(‘Inertia‘)
plt.title(‘Elbow Method For Optimal k‘)
plt.show()

Here we‘ve extracted just the age and spending score columns and determined the optimal number of clusters using the elbow method. This plots the inertia (SSE) for different values of k. The "elbow" or inflection point is typically chosen as a good number of clusters – in this case it looks like 4 or 5 would be reasonable.
Let‘s fit k-means with 5 clusters and visualize the result:
kmeans = KMeans(n_clusters=5, random_state=42)
labels = kmeans.fit_predict(X)
plt.figure(figsize=(10,8))
plt.scatter(X[:,0], X[:,1], c=labels, s=50, cmap=‘viridis‘)
plt.scatter(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1], s=200, marker=‘*‘, c=‘r‘, label=‘Centroids‘)
plt.xlabel(‘Age‘)
plt.ylabel(‘Spending Score (1-100)‘)
plt.title(‘K-Means Clustering (k=5)‘)
plt.legend()
plt.show()

The scatter plot shows our final clustering, with each data point color-coded by its assigned cluster. We can see some interesting segments here:
- Purple cluster: High spending score, mid age range – big spenders in their prime earning years
- Blue cluster: High spending score, younger age range – possibly young professionals
- Green cluster: Low spending score, older age range – more frugal older customers
- Red cluster: Average spending score and age
- Yellow cluster: Low spending score, very young – students or low income
These segments give us a more nuanced view of the different customer types, rather than just looking at average age and spending score overall. This kind of insight could be used to tailor marketing strategies, product recommendations, loyalty programs etc. to each group.
Considerations and Limitations
While k-means is a powerful and widely used algorithm, it‘s important to be aware of a few considerations and limitations:
- K-means requires specifying the number of clusters in advance, which may not always be obvious. Techniques like the elbow method, silhouette analysis, or domain knowledge can help inform the choice of k.
- K-means is sensitive to scale, so it‘s important to normalize your data before clustering to avoid attributes with larger values dominating the distance calculations.
- The random initialization in k-means means the results can vary between runs. It‘s good practice to run the algorithm multiple times and select the best result.
- K-means struggles with clusters of varying sizes and densities, since it assigns points to the nearest centroid. Density-based algorithms like DBSCAN may be more appropriate in those cases.
- K-means also has trouble with non-convex shapes. If your data has clusters that are non-spherical, algorithms like Gaussian mixture models may perform better.
Despite these limitations, k-means remains a go-to clustering algorithm for its simplicity, efficiency, and interpretability. It‘s a great tool to have in your data science toolkit.
Conclusion
In this post, we‘ve covered a lot of ground in understanding how the k-means clustering algorithm works and how it can be applied to a customer segmentation problem. To recap some key points:
- K-means aims to partition data points into a specified number (k) of clusters based on their feature similarity.
- The algorithm represents each cluster by a centroid and iteratively assigns points to the nearest centroid and updates the centroids until the solution converges.
- Applying k-means to a mall customer dataset allowed us to discover meaningful customer segments differing in age and spending habits.
- Visualizing the clusters provides an intuitive understanding of the different customer groups and can inform more targeted business strategies.
- There are certain limitations to keep in mind with k-means, but it remains a powerful and widely used clustering approach.
I encourage you to try applying k-means clustering to your own data and see what insights you can uncover. Customer segmentation is just one of many applications – this versatile algorithm can be used on everything from document clustering to image compression. The key is to be creative, understand how to interpret the results, and always consider the underlying structure of your data. Happy clustering!