How to cluster marketing audiences with k-Means in Machine Learning

Published by Patrick Mebus on

How to cluster marketing audiences with k-Means in Machine Learning

Share on facebook
Share on twitter
Share on linkedin

Clustering audiences is a crucial task in online marketing. It’s the basis for personalized messaging and user experience  Make the segment too big and you’ll target too many irrelevant users with your message. Make the segment too small and you’ll loose potential customers. Unsupervised Machine Learning can help to group your users automatically based on pattern-recognition in data. In this article we’ll provide an approach how to cluster collected analytics data with k-means algorithm.

Audience segmentation is the foundation of every successfull marketing campaign

Setup & Approach

The approach is Unsupervised: This means that we don’t know about the output, we ignore dependencies of different variables and don’t define a lable.

In our example we use the k-means algorithm, which is one of the most commonly used algorithms for clustering-problems. What it does is basically grouping different datapoints (in our case website-user-data) together based on similiarities and patterns in the dataset.

As a dataset we’ll use the popular Kaggle advertising dataset, which can also be used to become more familiar with ad click prediction. We have 1000 training-examples and 10 features like age, City or area income.

Advertising dataset from kaggle, provided by fayomi 2018

The k-Means Algorithm

The k-Means algorithm is widely used for clustering-problems like user- or market segmentation. What it does, is to allocate datapoints to certain, randomly initialized centers (centroids)

based on the distance between those. Centroids can be seen as the optimization-starting-points.

Following an iterative approach k-Means then tries to minimize the square distance between the datapoints and
their centroids to learn the optimal values and boundaries.

Clustering with k-Means. On the left: unclustered data. On the right: clustered data-groups centered around their centroids. Source:

The k-Means algorithm can quickly be implemented and provides solid results. Tough the weak point is the definition of cluster-numbers, which needs to be done manually.This makes it even more important to bring in experience, knowledge about your subject and a certain feeling for the right number of clusters.

The great advantage of Search Engine Marketing is, that we can make quick adjustments on the go, if we detect unexpected low performance. We don’t need to stop the printing presses for hours. So, if your ROAS at the end of the day is below the target try to increase the number of clusters, to be able to provide more specific cluster-messaging.

Clustering Audiences with k-Means

1. Import the libraries and dataset
As always we need to import the required libraries and our data at first.
from sklearn.cluster import KMeans

import pandas as pd

from sklearn.preprocessing import MinMaxScaler

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

df = pd.read_csv('/content/advertising.csv')

2. Data Exploration

Now we should have a look on our data to get a feeling for the best possible number of clusters.

By calling




we will get a first impression about the dimensions and the content of our dataset

Then we’ll have a first look on the datapoint-distribution in a scatter-plot imported from the matplotlib library

plt.scatter(df['Daily Time Spent on Site'], df['Area Income'])

In this case we’ve decided to build our audience-cluster based on the user’s area-income (hypothesis: these people are willing to leave more money in our online shop) and the daily time they’ve spent on our website (as a tracked quality metric for user engagement).

3. Define the clusters

In the next step we’ll define the number of clusters. For this example we’ll randomly choose five clusters. 

km = KMeans(n_clusters=5)

When building the algorithm-object with can fine-tune our model with additional parameters from the scikit-learn library like max. number of iterations or random state. The whole documentation you’ll find here: 

4. Make prediction

To train our model and build the clusters we’ll use the fit-method from scikit-learn. The output is an array, that includes all the cluster-allocations for the datapoints in the dataset.

y_predicted = km.fit_predict(df[['Daily Time Spent on Site', 'Area Income']])

To assign the new clusters to our dataset we can add a new column “cluster”, which includes the assignment for each row.

df['cluster'] = y_predicted

5. Visualize new clusters

Finally to visualize thew new audience-clusters we can build a scatterplot by calling plt.scatter from the matplotlib library

df1 = df[df.cluster==0]

df2 = df[df.cluster==1]

df3 = df[df.cluster==2]

df4 = df[df.cluster==3]

df5 = df[df.cluster==4]

plt.scatter(df1['Daily Time Spent on Site'],df1['Area Income'], color= 'blue')

plt.scatter(df2['Daily Time Spent on Site'],df2['Area Income'], color ='yellow')

plt.scatter(df3['Daily Time Spent on Site'],df3['Area Income'], color ='red')

plt.scatter(df4['Daily Time Spent on Site'],df4['Area Income'], color = 'green')

plt.scatter(df5['Daily Time Spent on Site'],df5['Area Income'], color = 'purple')

plt.xlabel('Daily Time Spent on Site')

plt.ylabel('Area Income')


Was this article helpful for your work? Leave a comment 🙂

exited about this post? feel free to share

Patrick Mebus

I’m a Digital Marketer with deep passion for Search Engines, Automation and AI. I’m here to make Machine Learning more feasible for Search Engine Marketers.

1 Comment

phone interview questions · July 20, 2020 at 9:03 am
Good day! This post could not be written any better! Reading through this post reminds me of my
previous room mate! He always kept talking about this. I will forward this article to him.
Fairly certain he will have a good read. Many
thanks for sharing!

Leave a Reply

Your email address will not be published. Required fields are marked *