K-Means: The Marketer’s Algorithm
By: Gade Venkatesh
Tags: Machine Learning, Marketing, Retail, Data Science, Marketing Automation

Introduction —

We shall cover the following aspects in this article:

Exploratory Data Analysis (EDA) to build our intuitions K-means Clustering in action What do marketers do with these clusters?

Let’s take a dataset and try to demystify concepts on the go.

Our data set is a collection of customer data at a shopping mall. This type of data is usually collected through a myriad of sources — Customer Relationship Management tools (CRM), transaction data, parking tickets, lucky draw coupons etc.

Our data frame has 200 rows of customer data across 5 features or variables.

Data set snapshot

Data dictionary — containing details of various features

Various feature columns in the data

No null values in the data set

Since there are no null values in the dataset and values in various fields appear to be of the desired datatype, we can directly proceed for EDA.

1. Exploratory Data Analysis –

Univariate analysis

Understanding the gender split of the mall traffic — there are 112 Females and 88 Males, clearly more number of females visit the mall than males.

People of all ages visit the mall — 20 to 70-years. The age distribution is slightly skewed to the right, meaning there are some very elder customers of the mall. The mode class of customers is the age group of 30 to 40.

The customers of the mall come from a diverse set of income levels — 15k to over 125k per annum. Customers in the income range of 50k to 75k form the leading demographic. There are also a few very high-income customers that belong to the income bracket of 125k and above.

Spending score is almost symmetrically distributed around the mean. Most of the customers have a spending score in the range of 40 to 60.

Bivariate analysis

Age and Spending score seem to have a negative correlation, i.e. younger customers tend to buy more than the elder ones.

The plot of income and spending score shows some inherent grouping for a few sets of variable combinations. At this stage, 5 such groups can be visibly identified from the scatter plot, it would be interesting to see how many clusters we actually end up identifying during clustering. Due to the symmetrical nature of the plot there appears to be no correlation between the two variables.

Age vs annual income plot also has a fairly uniform spread, implying that there exists a huge range of customers at a particular age and at a particular income level. There appears to be no correlation between them for the current data.

Let’s validate our observations by checking the correlation matrix and a heat map.

Correlation Matrix

Heat map of all numerical features

Annual income and Customer ID have a correlation of 0.977 which is awesome but is of no particular use as Customer ID is a nominal feature.

Our initial guess about of the correlation between age and spending score is correct as indicated by a correlation of -0.328.

2. K-means Clustering in Action—

By K-means clustering, we want to identify points that are closer to each other based on the distance (read Euclidean distance) between them and group them in one cluster. Doing this, we would be able to achieve a cluster of data points which are closely packed, i.e. share similar characteristics with points within the same group but differ from those outside the group.

Ideally, we want to achieve clusters such that within cluster variance is minimum and between cluster variance is maximum.

Image by author — Points ABC are similar to each other, hence grouped as Cluster 1, whereas DEFG are similar to each other and grouped as Cluster 2. However ABC and DEFG are different from each other.

Let’s put our intuition to test and apply clustering on income and spending score plot.

But before we do that, K — Means algorithm requires a user input for k i.e. the number of clusters we want to visualize in our data.

We can choose all the data points in 1 cluster or we can have as many clusters as data points i.e. 200. But ultimately, we would want to identify meaningful customer groups which are sufficiently large in size and share a set of characteristics.

So how do we pick the optimum number of clusters?

— The elbow method.

Let’s say we have a model with ’n’ clusters. We can find within cluster variance — how far are the data points from the cluster center, also called within-cluster-sum of squares (WCSS). The sum of WCSS for all clusters would give us the WCSS for the entire model.

Our aim is to have an optimum model with number of clusters convenient enough to identify customer groups distinctly and at the same time do so with a low value of total WCSS. As we increase the number of clusters our total WCSS decreases but we also end up with more clusters which may be impractical to deal with — clearly there’s a trade off.

To identify that sweet spot, we observe the rate of decline in WCSS with incremental change in number of clusters. The point beyond which there is no significant drop in total WCSS is our optimum value of ‘n’.

In our analysis, we experiment with cluster scenarios where n varies from 1 to 8 and obtain the plot.

Elbow method showing optimum number of clusters

At n = 3 and n = 5, we can see an elbow-like formation (hence the name). But the slope of WCSS reduces even further at n = 3, but WCSS decline beyond n = 5 is very less.

Since there is no radical reduction in the value of WCSS beyond n = 5, we pick 5 clusters and implement the K-means clustering algorithm.

5 Clusters identified by K — Means Algorithm

3. What do marketers do with these clusters?

Let’s understand it through STP.

STP — Segmentation Targeting Positioning, is one of the most fundamental frameworks used by marketers to identify and reach out to their target customers.

Segmentation is basically, categorizing customers into groups that share a set of characteristics.

We want to classify our customers based on –

Demographics — age, gender, education, income, marital status

Geography — country, city, locality, climate

Behavior — offline or online purchase behavior, usage — heavy user or light, buying frequency, benefits sought, loyalty

Psychographics — personality, interests, opinions, attitude, beliefs, lifestyle, values

At the end of segmentation, we should be able to distinctly identify such clusters.

Next, targeting would allow us to identify the most attractive or relevant customer groups. With a set of customer groups identified, we want to evaluate the attractiveness of each group basis parameters like –

revenue potential and size of the target group — we don’t want to be spending a lot for a very few customers who don’t buy a lot

accessibility — our ability to reach out to them etc.

Targeting allows us to zero-in on one or more target groups depending on what product offerings we have.

Finally, by Positioning we can differentiate our offerings from those of our competitors and create a desired brand image. Positioning would also allow us to figure out what value proposition should we communicate so that it appeals to the particular target group. It would also allow us to conceptualize what our campaign should look like or what should our creatives focus on etc.

Case in point –

We have identified 5 clusters using our model, based on demographic factors like age and income, behavioral factors — Spending score, a proxy or an indicator of behavior.

Let’s take Cluster 2 and understand how it looks like —

There are 21 Females and 18 Males in Cluster 2

We can build a profile for Cluster 2 based on our analysis -

Age — 27 to 40 years — working professionals and/or parent

Spending score — A high scoring group as a whole, mean — 82.13, median — 83, these are the customers who love shopping at our mall.

Annual Income — This is a high earning lot, range — 69k to 137k, mean of 86.5k and median of 79k . Mean is greater than the median, indicating presence of some outliers — presence of a few very high-income individuals.

Based on these attributes we can even label Cluster 2

— ‘Premium Shoppers’

These are customers with a relatively high spending scores as compared to others. We would like to encourage their behavior by acknowledging how important they are to us and plan our promotions accordingly. We can offer deals on our high-end products for these customers, subject to their interests and buying history — which would need more data.

Likewise, we can build profiles for other clusters as well.

Further, we can build a Buyer Persona — an image of our ideal customer.

A detailed version could be made by adding more features including their interests, hobbies, past customer reviews, RFM (Recency Frequency Monetary Value) data etc.

The better we understand our customer, better are the chances of our campaign’s success.

At the next level of analysis we can build robust models to classify our cluster members based on their probability to buy a particular product. This would help make our campaign efforts more focused on a set of high-quality prospects within the cluster and be more efficient with our budget as well.

The possibilities are immense!

Imagine we skip all this and run a one size fits all campaign, I bet you would agree that’s a fine recipe for disaster.

P.S — Thanks for reading! Please feel free to leave your constructive feedback in the responses.

Photo by Artem Kniaz on Unsplash

References –

1. Dataset

2. codebasics by Dhaval Patel

3. StatQuest with Josh Starmer

4. Machine Learning A-Z™: Hands-On Python & R In Data Science

5. K-Means Python Code

Gain Access to Expert View — Subscribe to DDI Intel