Hands-on: Customer Segmentation
Tags: Unsupervised Learning, Customer Segmentation, Python, Clustering, Data Science

Knowing your customers is the foundation of any successful business. The better you understand their needs, their desires and wishes, the better you may serve them. That’s the reason why market or customer segmentation is so useful in the long run: You create profound knowledge about your customers, their characteristics and their behaviours to finally improve your business model, marketing campaigns, product features and many more…

Hands-on: Customer Segmentation (Photo by Max McKinnon on Unsplash)

In this article you will learn all necessary basics about customer segmentation and the application of an unsupervised learning method with the help of Python to finally build clusters for a customer sample dataset. This tutorial is set up in a way that you will succeed in identifying clusters with little to even no prior coding knowledge. Have Fun !

How will we segment our customers?

We will start out by learning the basic theory about clustering and clustering with K-means. Afterwards the ingested theory will be applied to our sample customer segmentation dataset which we will firstly explore, secondly prepare and thirdly cluster our dataset with the help of K-means algorithm.

High Level Process

To segment our customer we are working with Python and its’ amazing open source libraries. First of all we use Jupyter Notebook, used as an open-source application for live coding and it allows us to tell better stories with our code. Furthermore we import Pandas, which puts our data in an easy-to-use structure for data analysis and data transformation. To make data exploration more graspable, we use Plotly to visualise some of our insights. Finally with Scikit-learn we will split our dataset and train our predictive model.

Tech Stack To Build Segments

Basics about clustering with K-Means

While we distinguish between supervised and unsupervised learning, clustering belongs to the unsupervised learning algorithms and is probably considered to be the most important one.

Machine Learning Overview

Given a collection of unlabelled data, meaning the dataset is not tagged with a desired outcome. The goal is to identify patterns in this data. Clustering describes the process of finding structures where similar points are grouped together. Following that definition, a cluster is a collection of similar data points. Dissimilar data points shall belong to different clusters.

Clustering

There are various clustering algorithms identifying these patterns such as DBCSAN, Hierarchical Clustering or Expectation Maximisation Clustering. While each algorithm has its individual strengths, we are starting with K-means as one of the simplest clusterings algorithms.

How does the K-Mean algorithm work?

K-means belongs to the centroid-based cluster algorithms and assigns each object or datapoint to the nearest cluster center in such way that the squared distances from the clusters are minimised. “K” stands in this context for the amount of clusters, or more specifically cluster centroids. The objective is to minimise the within cluster sum of squares:

Step 1: Initialisation

As first step we have to choose the amount of centroids for our clustering algorithm. While a good choice can save a lot of effort, a bad one may result in missing out on natural clusters. But how can we choose the optimal number of clusters? For our purpose this will be done with the elbow method. An heuristic approach towards finding the right amount of clusters.

Elbow Method Example (Code Further Below)

Recall that the basic idea of K-means clustering is to minimise the within cluster sum of square. It measures the compactness of the cluster and we want it to be as small as possible. For the elbow method the sum of square is calculated for a decreasing amount of clusters and plotted accordingly. We choose then the number of clusters were the sum of square does not change significantly — basically where we can see the “elbow” in our plot.

Step 2: Building the Clusters

Secondly we are determining the minimum distance for each datapoint to the nearest cluster centroid. No worries this has not to be done manually but will be solved by Python. It is just good to understand what the algorithm is basically doing repetitively.

Step 3: Update & Iterate

Thirdly the cluster means or centroids have to be updated. This is done until there are no more changes in the assignment of data points towards other centroids.

While dividing the clustering process with K-Means in three simple steps sounds pretty straightforward, there are certain disadvantages we should be aware of. One is that K-Means is very sensitive towards outliers as they strongly influence the within cluster sum of squares. Therefore we should consider removing them before applying the algorithm. Second disadvantage is the random choice of cluster centroids with K-Means. This may leaves us ending up with slightly different results on different runs of the unsupervised learning algorithm, which is not optimal for a reproducible research approach. Nevertheless by understanding these weaknesses we can still apply K-Means especially when we want quick and practical useful results.

The Dataset

For the purpose of this project we are working with a publicly available dataset from Kaggle. The dataset includes some basic data about the customer such as age, gender, annual income, customerID and spending score. In this scenario we want to find out which customer segments show which characteristics in order to plan an adequate marketing strategy with individual campaigns for each segment.

# for basic mathematic operations

import numpy as np

import pandas as pd # for visualizations

import matplotlib.pyplot as plt

import seaborn as sns data = pd.read_csv('../Clustering/Mall_Customers.csv')

data.head(10)

For better insights and unprepared datasets it is recommended to do an explorative data analysis, data cleaning and data preparation upfront. For the sole purpose of demonstrating K-Means and customer segmentation we will keep this to an absolute minimum and focus on our main objective.

The Clustering — Elbow Method

Once the dataset is loaded and cleaned, we can start clustering the dataset. In this case we will cluster initially according to Annual Income and Spending Score as our main objective is a marketing campaign targeting people with high income and willing to spend. To do so we have to select all rows and column 3 and 4.

x = data.iloc[:, [3, 4]].values

As previously described we have to find out which amount of centroids is the optimal amount to minimise the within cluster sum of squares. To do so we run our code from one to ten cluster with the help of a for loop. The result for each amount of clusters is then appended to the wcss list.

from sklearn.cluster import KMeans wcss = []

for i in range(1, 11):

km = KMeans(n_clusters = i, init = 'k-means++', max_iter = 300, n_init = 10, random_state = 0)

km.fit(x)

wcss.append(km.inertia_)



plt.plot(range(1, 11), wcss, c="purple")

plt.title('The Elbow Method', fontsize = 30)

plt.xlabel('No of Clusters', fontsize = 20)

plt.ylabel('WCSS', fontsize = 20)

plt.show()

To identify the optimum amount of centroids we have to look for the “elbow” by plotting each within cluster sum of squares value on the y-Axis and the amount of centroids on the x-Axis.

Elbow Method

It is found that after five clusters the wcss value is decreasing very marginally if adding more clusters. In this case we got what we want: The optimum amount of clusters seems to be five.

The Clustering — Visualising K-Means

What we want to do next is visualising our five cluster in order to identify our target customers and have an opportunity to present our results to colleagues and other stakeholders. To do so we run our K-Means algorithm and determine the clusters within Annual Income and spending score (the previously defined x).

km = KMeans(n_clusters = 5, init = 'k-means++', max_iter = 300, n_init = 10, random_state = 0) y_means = km.fit_predict(x)

With the prediction alone we cannot see much and have to use plotly to create a nice graph for our clusters.

plt.scatter(x[y_means == 0, 0], x[y_means == 0, 1], s = 100, c = 'orangered', label = 'potential')

plt.scatter(x[y_means == 1, 0], x[y_means == 1, 1], s = 100, c = 'darksalmon', label = 'creditcheck')

plt.scatter(x[y_means == 2, 0], x[y_means == 2, 1], s = 100, c = 'goldenrod', label = 'target')

plt.scatter(x[y_means == 3, 0], x[y_means == 3, 1], s = 100, c = 'magenta', label = 'spendthrift')

plt.scatter(x[y_means == 4, 0], x[y_means == 4, 1], s = 100, c = 'aquamarine', label = 'careful')

plt.scatter(km.cluster_centers_[:,0], km.cluster_centers_[:, 1], s = 200, c = 'darkseagreen' , label = 'centroid') sns.set(style = 'whitegrid')

plt.title('K Means Clustering', fontsize = 30)

plt.xlabel('Annual Income', fontsize = 20)

plt.ylabel('Spending Score', fontsize = 20)

plt.legend()

plt.grid()

plt.show()

The visualisation allows us to clearly identify the five clusters. The five centroids are visible in a darkgreen. Our main target group, named as “target” and in gold color, has the highest spending score and annual income.

Clustering with K Means

Furthermore we can find four additional groups that may be interesting for us to approach. In this case we named them “potential”, “creditcheck”, “spendthrift” and “careful”. Well done — we did some very basic clustering to segment a customer dataset.

What’s next ?

For now we have segmented our customers according to Annual Income and Spending Score. But of course there are other factors that may influence your decision on which customers you want to target. In our example you could further investigate “Age” as a feature and see its impact on the clustering results. For businesses it is most common to segment their customers according to four different categories:

Business Customer Segmentation Categories

After expanding, exploring and defining the different customer segments be creative on how to use your gained knowledge. Optimise pricing, reduce customer churn, increase retention, improve your product, … there are endless opportunities. Now it’s up to you :)

**************************************************************

Articles related to this one:

Hands-on: Predict Customer Churn

Applied Machine Learning For Improved Startup Valuation

Hands-on: Setup Your Data Environment With Docker

Eliminating Churn is Growth Hacking 2.0

Misleading with Data & Statistics

***************************************************************