The Most Important Data Science Tool for Market and Customer SegmentationUse K-means and let AI advise you how many segments there (really) are.

Tern Poh LimBlockedUnblockFollowFollowingJan 7Market and customer segmentation are some of the most important tasks in any company.

The segmentation done will influence marketing and sales decisions, and potentially the survival of a company.

Surprisingly, despite the advance in machine learning, few marketers are using such technology to augment their all-important market and customer segmentation efforts.

In this article, I will show you how to augment your segmentation analysis with a simple, yet powerful machine learning technique called K-means.

Learning this will give you an edge over your competitors (and colleagues).

So what’s K-means?K-means is a popular clustering algorithm for unsupervised machine learning.

It groups similar data point into a predefined number of groups.

Let me explain each term for you:Clustering: a machine learning technique for identifying and grouping similar data points (e.

g.

customers) together.

Unsupervised machine learning: you don’t need to provide labelled data to the algorithm on how to group the customers.

It will scan through all information associated with each customer and learn the best way to group them together.

A predefined number of groups: you need to tell K-means how many groups to form.

This is the only input needed from you.

Here is an analogy to the above concepts: Imagine you have some toys and without providing further instruction, you ask your kid to separate the toys into three groups.

Your kid will play around and eventually find his own best way to form three groups of similar toys.

OK…so how does K-means work?Let’s assume you think there are 3 potential segments of customers.

K-means will randomly initiate 3 points (i.

e.

centroids) at random locations and slowly fit each data point to the nearest centroid.

Each data point represents one customer, and the customer closest to the same centroid will be in the same group.

The centroids’ locations are adjusted automatically based on the last nearest customer allocated to them.

Doing so, it will learn on its own to find other customers with similar characteristics.

K-means identifying 3 clusters in a data set.

Source: WikipediaWhat?.That looks simple.

I could do the grouping visually myself!The 2-dimensional representation of customers above is a simplified form of visualising the data.

Each information associated with a customer represents one dimension of data.

For instance, if you are just plotting the items and quantity purchased, then that’s 2-dimension.

Once you consider additional information for each customer, such as country of residence and total spending, the complexity jumps to 4-dimension!Visualisation of different dimensions.

Source: WikipediaIt is hard for us to imagine grouping items together beyond 3-dimensional space, but not so for machine learning.

This makes machine learning much more powerful than traditional methods in finding meaningful segments.

Machine learning can make sense of multiple dimensions beyond our imagination, find similar characteristics of customers based on their information, and group similar customers together.

That’s the beauty of it!But how do I know what’s the optimal number of groups to form?You can find the optimal number of groups by following these two principles:Customers in the same cluster should be close together (tight intra-cluster distance)Each different cluster of customers should be far from each other (far inter-clusters distance)Here’s another way of interpreting for the above principles:Birds of the similar feather flock together.

They flock close to each other to find like-minded friends; the more like-minded they are, the closer they flock together.

Different flocks do not come near each together.

Each flock is proud of their unique identity; the more distinct their identity, the further they will distance themselves from other flocks.

One method for finding the optimal number of groups is to use Silhouette Score.

It takes into consideration both the intra-cluster and inter-clusters distance and returns a score; the lower the score, the more meaningful the clusters formed.

One of the most challenging aspects of using K-mean is deciding how many clusters to form.

This can be identified mathematically by using Silhouette Score.

Great.

Could you illustrate using K-means to segment an actual customers dataset?I will illustrate using K-means to perform RFM (Recency, Frequency, and Monetary) customer segmentation.

The data source is from an actual online retailer in the UK.

I have already pre-processed the data by performing the following step:Extract most recent 1-year transactions data.

Calculate the Recency of each customer by their latest transaction date.

Calculate the Frequency of each customer by summing the number of invoices tagged to each customer.

Calculate the Monetary Value of each customer by summing up their respective total spend.

Below is a snapshot of the RFM values of each customer that I created:RFM value of each customer.

Anything else that I need to do before implementing K-means?K-means gives the best result under the following conditions:Data’s distribution is not skewed (i.

e.

long-tail distribution)Data is standardised (i.

e.

mean of 0 and standard deviation of 1).

Why?.Recall that K-means group similar customers together based on their distance from centroids.

The location of each data point on the graph is determined by considering all information associated with the specific customer.

If any of the information is not on the same distance scale, K-means might not form meaningful clusters for you.

Machine learning means learning from data.

To get the best result, you should prepare the data to make it easy for the machine to learn.

Here are the exact steps to prepare the data before using K-mean:Plot distribution charts to check for skewness.

If the data is skewed (i.

e.

has long-tail distribution), perform log transformation to reduce the skewness.

Scale and centre the data to have a mean of 0 and variance of 1.

I first check for skewness of data by plotting a distribution plot of Recency, Frequency, and MonetaryValue:Distribution Plots of RFM.

All variables are heavily skewed.

I performed log transformations to reduce the skewness of each variable.

Below is the distribution plots of RFM after log transformation:Distribution Plots of RFM.

The skewness is reduced after log transformation.

Once the skewness is reduced, I standardised the data by centring and scaling.

Note all the variables now have a mean of 0 and a standard deviation of 1.

Basic statistics of RFM.

All variables have mean of 0 and standard deviation of 1 after centring and scaling.

How about finding the optimal number of groups?Once the data is prepared, the next step is to run iterations of K-means (usually up to 10 clusters) to calculate the Silhouette Score for each cluster.

A function to identify the optimal number of clusters based on the Silhouette Score.

Lower Silhouette Score denotes the formation of better and more meaningful clusters; the result below shows the optimal number of clusters is four.

Silhouette Score of 2 to 10 clusters.

The optimal number of clusters is 4.

Nonetheless, it is a common practice to implement K-means clustering on +/- 1 of optimal cluster identified; here, it is 3, 4, and 5 clusters.

This gives a wider perspective and facilitates meaningful discussion with your stakeholders to determine the appropriate number of customers segments.

Perhaps there could be some market peculiarities and your stakeholders might decide to implement their marketing strategies on 5 clusters instead of the optimal 4 clusters identified.

How does the end result of K-means segmentation look like?Now we are ready to run the data through K-means of 3, 4 and 5 clusters to segment our customers.

A function to implement K-means on data set.

Below is the result of the customer segmentation:Flattened (t-SNE) graph of 3,4 and5 clusters.

Recall that each information associated with a customer creates an additional dimension.

The above image is obtained by flattening three-dimensional graphs (created from Recency, Frequency, and MonetaryValue) into two-dimensional graphs for ease of visualisation.

This visualisation can give you a sense of how well the clusters are formed.

In case you are wondering, the technique for flattening high dimensional graph and visualising it in a two-dimensional format is known as t-Distributed Stochastic Neighbor Embedding (t-SNE).

You can read up more on this if you are interested; the explanation for this is beyond the scope of this article.

How do I make use of the segmentation results in my marketing?By this stage, each customer in the dataset is tagged with their respective group number.

You can proceed to use any industry’s common practices to visualise the results.

Below is an example of using Snake Plot and Relative Importance of Attributes Chart to build personas of each cluster of the segmentation.

Both are commonly used in the marketing industry for customer segmentation.

Snake Plot of 3, 4, 5 clusters formed using K-means.

Relative Importance Chart of 3, 4, and 5 clusters formed using K-means.

You can take this result and compare it against your original segmentation done using traditional methods.

Is there any big difference?It is a good practice to perform a deep dive and understand why K-means thinks customers of a particular group belong together (yes, sadly K-means is unable to write us a marketing report on their segmentation decision yet).

With this understanding, you could initiate discussion with relevant stakeholders to seek their opinion and get alignment on how to best segment the customers before launching the next big marketing campaign.

All the relevant codes for this article can be found at my repo.

ConclusionK-means is a simple but powerful segmentation method.

Anyone doing customer or market segmentation should use this to augment traditional methods.

Otherwise, they risk becoming obsolete in the age of artificial intelligence.

Enjoyed this article?.Hit the applause button below.

It will help me write more and better articles.

.