Customer Segmentation with K Means Clustering | by Allison Kelly | Towards Data Science

Not all customers are created equal. — Me (and many other people, too)
You would never speak to your parents the way you speak to your partner, nor would you speak to your children the way you speak to your manager. So why is your marketing team using a one-size-fits-all language in your marketing materials?
Leveraging machine learning and AI with the massive amounts of consumer history, web traffic, and product reviews can have a measurable impact on marketing campaigns. By harnessing the power of statistical and analytical tools, customers can be partitioned into segments based on three simple behavioral traits, influencing the language used in multichannel marketing campaigns and resulting in higher CTR and conversion rates.
As a customer, you don’t want to be seen as just a means to an end. You want to be part of a community. As a retailer, it’s your job to get to know your customers and speak to what you know about them. Don’t be passive, learn their pain points, get to know their purchase habits, and build trust. Revenue will follow.
Generally, there are four ways to segment your consumer base:
Demographic — age, gender, socioeconomic status
Geographic — where in the world are they?
Psychographic — looking for wedding rings, planning to buy a house
Behavioral — what does your customer do once they get to your site?
Google’s BigQuery API allows you to make SQL-like queries on your Google Analytics data. If you have Ecommerce Tracking through your Google Analytics account, you have access to your customer’s behavioral data. The study in question was performed on the Google Merchandise Store’s data from August 2016 to August 2017. Over 740,000 unique people visited the merch store during that time period, resulting in 11,500 purchases totalling over $200,000 dollars.
In order to supplement the web data we have from the Google Merchandise Store, we’ll engineer a few features in order to ‘score’ each customer based on the RFM model. This technique takes the recency (R) of the last purchase, the frequency (F) or count of all purchases minus one, and the monetary value (M) defined by the average order amount.
In order to segment your data, you’ll need to start with three columns: unique customer IDs, dates of transaction, and transaction amount. I used this function to process the Google Merch Store data into a format that we can model from —
Once we run our dataframe through this function, we’re left with the following information:
It’s important to note that you may also have visitors to your site who do not make purchases, but this segmentation method is focused on customers who have already converted. Another study can — and should — be done on the behaviors of those who do not purchase so you can improve the conversion power of your website.
Now that our data is ready to cluster, we’ll test a few cluster levels and compare the sum of squared errors in an elbow plot to identify the best level to cluster each feature individually.
It looks as if Frequency begins to level at five clusters, Recency at four or five, and Revenue at four or five. Depending on your business model, you may have reasons to choose a lower or higher amount of clusters. After a few iterations, I’ve decided on five Frequency, four Recency, and five Revenue.
From here, we’ll need some helper functions to order clusters from worst to best and assign each datapoint with the corresponding cluster.
And here are the results:
Frequency — the vast majority of customers fall into the lower categories and there’s a huge jump between three and four, with only one customer having purchased over 15 times
Recency — Most customers are quite inactive (how many Google tees do you really need?) The window of recency gets larger the better the cluster assignment is.
Revenue — The vast majority of consumers purchased, on average, between 17 and 19 dollars worth of products on the Google Merchandise Store. The five revenue clusters are pretty well spaced, with no more than about 23 dollars spent in an average order.
We’ll determine and overall score by adding up each cluster’s score and determine segment stratification from there.
rfm_df[‘OverallScore’] = rfm_df[‘RecencyCluster’] + rfm_df[‘FrequencyCluster’] + rfm_df[‘RevenueCluster’]rfm_df.groupby(‘OverallScore’)[‘Recency’,’Frequency’,’logRevenue’].mean()
Depending on your business model, you may already have certain cutoffs for segments. Here, I’ve broken them down to three groups. Inactive, infrequent, low spenders are low value, mid value is higher than average revenue with average — more than average recency and average to more than average frequency, and high value customers fall into the upper echelons of at least two features.
# Naming and defining segmentsrfm_df[‘Segment’] = 0rfm_df.loc[rfm_df[‘OverallScore’]>4,’Segment’] = 1 rfm_df.loc[rfm_df[‘OverallScore’]>6,’Segment’] = 2
And now to visualize each cluster to see how well we did:
Look at those beautifully defined clusters!
From this plot, we can see there are customers that have not spent a lot of money but frequently stop by the site and have made purchases recently that also fall into the high value category. Scores seem to be pulled down by either very infrequent purchases or made their last purchase far in the past.
From here, you can pass the information on to your marketing team, or if you ARE the marketing team, use this information to move forward with crafting language for email marketing campaigns or ads that target each customer’s specific purchase behavior. You’ve sewn the seeds, now see how your revenue grows!