Basic Business Application of Machine Learning

Alexander Del Mundo
7 min readJun 7, 2022

We have learned a handful of machine learning algorithms, and we thought it is about time to put it into real-life action by using a customer segmentation model. In this article, we are going to use data from UCI Machine Learning Repository which includes purchasing transaction details.

What is Customer Segmentation?

Customer segmentation is the process of grouping customers based on similar characteristics so the organization can do a targeted marketing approach to each group effectively and efficiently.

In B2B or B2C marketing, one might segment customers according to a variety of features, including but not limited to;

  • Geographic
  • Demographic
  • Psychographic
  • Technographic
  • Behavioral

In this sample we are just going to group customers on the following criteria;

  • The volume of monetary transactions
  • Frequency of purchases
  • The recency of customer activities

Why Segment Customers?

Segmentation allows organizations to tailor fit their marketing strategies to best suit the diverse customer groups relating to not only customer communications but also product introductions. In general, it helps the company:

  • Identify ways to improve products or new product or service opportunities.
  • Establish better customer relationships, while focusing on the most profitable customers.
  • Create and communicate targeted marketing messages that will instill to specific groups of customers which is fitting to their needs and interests.
  • Define the optimum channel for the segment, which might be anything between email, social media posts, radio ads, or another approach, depending on the particular group.

6 Customer Segmentation Tools

There are several online tools available to help with the customer segmentation process. Here are a few in particular that can aid this approach.

  1. Google Analytics can monitor your overall website traffic, and this can help you discover the demographics and behaviors surrounding your customers. In turn, you can determine the best ways to segment them into groups.
  2. HubSpot provides a host of tools you can utilize in your segmentation efforts, such as contact scoring, contact lists (both static and active), event-based segmentation, and more.
  3. Experian can build, view, and even manage your customer segments within Experian. At the heart of this tool is a focus on data, that is, collecting, combining, and analyzing it to achieve a greater understating of customers with an eye on meeting their needs.
  4. Qualtrics is a software that provides segmentation tools based on the actions of your customers and the insights gained from these. This software can organize customer groups and determine the best communication methods.
  5. Sprout Social can help by making it easier to create and share various messages through Audience Targeting on both Facebook and LinkedIn.
  6. Mailchimp continues to be a useful tool for marketing teams in a variety of ways. It can assist with better management of customized, segmented email campaigns.

For this article, we employ the use of python and try to come out with a group of customers with specific targeted approaches using an unsupervised machine learning approach.

Sample Python Code Snippets For Customer Segments

The data can be downloaded following this link: https://archive.ics.uci.edu/ml/datasets/online+retail

# import required libraries for dataframe and visualization
import NumPy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import datetime as dt
import plotly as py
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
# import required libraries for clustering
import sklearn
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
from scipy.cluster.hierarchy import linkage
from scipy.cluster.hierarchy import dendrogram
from scipy.cluster.hierarchy import cut_tree
# Step 1: Read data
retail = pd.read_excel('/content/gdrive/My Drive/Colab Notebooks/data/datasets/AML/Clustering/Online Retail.xlsx')
retail.head()
# Calculating the Missing Values % contribution in DF
df_null = round(100*(retail.isnull().sum())/len(retail), 2)
# Droping rows having missing values
retail = retail.dropna()

New Attributes

# New Attribute : Monetary
retail[‘Amount’] = retail[‘Quantity’]*retail[‘UnitPrice’]
rfm_m = retail.groupby(‘CustomerID’)[‘Amount’].sum()
rfm_m = rfm_m.reset_index()
rfm_m.head()
# New Attribute : Frequency
rfm_f = retail.groupby(‘CustomerID’)[‘InvoiceNo’].count()rfm_f = rfm_f.reset_index()
rfm_f.columns = [‘CustomerID’, ‘Frequency’]
rfm_f.head()
# New Attribute : Recency
# Convert to datetime to proper datatype
retail[‘InvoiceDate’]=pd.to_datetime(retail[‘InvoiceDate’],format=’%d-%m-%Y %H:%M’)
# Compute the maximum date to know the last transaction date
max_date = max(retail['InvoiceDate'])
max_date
# Compute the difference between max date and transaction date
retail['Diff'] = max_date - retail['InvoiceDate']
retail.head()
# Compute last transaction date to get the recency of customers
rfm_p = retail.groupby(‘CustomerID’)[‘Diff’].min()
rfm_p = rfm_p.reset_index()
rfm_p.head()
# Extract number of days only
rfm_p['Diff'] = rfm_p['Diff'].dt.days
rfm_p.head()
# Merge tha dataframes to get the final RFM dataframe
rfm = pd.merge(rfm, rfm_p, on=’CustomerID’, how=’inner’)
fm.columns = [‘CustomerID’, ‘Amount’, ‘Frequency’, ‘Recency’]
print(rfm.shape)
rfm.head()

Rescaling the Attributes

It is extremely important to rescale the variables so that they have a comparable scale.
There are two common ways of rescaling:

  1. Min-Max scaling
  2. Standardization (mean-0, sigma-1)

Here we execute Standard Scaling.

# Rescaling the attributes
rfm_df = rfm[[‘Amount’, ‘Frequency’, ‘Recency’]]
# Instantiate
scaler = StandardScaler()
# fit_transform
rfm_df_scaled = scaler.fit_transform(rfm_df)
rfm_df_scaled = pd.DataFrame(rfm_df_scaled)
rfm_df_scaled.columns = ['Amount', 'Frequency', 'Recency']
rfm_df_scaled.head()
from sklearn.preprocessing import MinMaxScaler
# Rescaling the attributes
rfm_df = rfm[[‘Amount’, ‘Frequency’, ‘Recency’]]
# Instantiate
minmaxscaler = MinMaxScaler()
# fit_transform
rfm_df_minmax = minmaxscaler.fit_transform(rfm_df)
#creating a dataframe
rfm_df_minmax = pd.DataFrame(rfm_df_minmax, columns = ['Amount', 'Frequency', 'Recency'])
rfm_df_minmax.head()

K-Means Clustering

K-means clustering is one of the simplest and most popular unsupervised machine learning algorithms.

The algorithm works as follows:

  • First, we initialize k points, called means, randomly.
  • We categorize each item to its closest mean and we update the mean’s coordinates, which are the averages of the items categorized in that mean so far.
  • We repeat the process for a given number of iterations and at the end, we have our clusters.
# k-means with some arbitrary k
kmeans = KMeans(n_clusters=3, max_iter=50) #model
kmeans.fit(rfm_df_scaled)
def K_Means(X, n):
scaler = StandardScaler()
X = scaler.fit_transform(X)
model = KMeans(n)model.fit(X)
clust_labels = model.predict(X)
cent = model.cluster_centers_
return (clust_labels, cent)
clust_labels, cent = K_Means(rfm_df, 3)
kmeans = pd.DataFrame(clust_labels)
rfm_df.insert((rfm_df.shape[1])

Finding the Optimal Number of Clusters

Elbow Curve to get the right number of Clusters

A fundamental step for any unsupervised algorithm is to determine the optimal number of clusters into which the data may be clustered. The Elbow Method is one of the most popular methods to determine this optimal value of k.

wcss = []for i in range(1,10):kmeans = KMeans(n_clusters=i,init='k
means++',max_iter=300,n_init=7,random_state=0)
kmeans.fit(rfm_df_scaled)
wcss.append(kmeans.inertia_)
plt.plot(range(1,10),wcss)
plt.title('The Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()
# Import the KMeans module so we can perform k-means clustering with sklearn
from sklearn.cluster import KMeans
from yellowbrick.cluster import KElbowVisualizer
X = rfm_df_scaled
model = KMeans()
visualizer = KElbowVisualizer(model, k=(1,10))
visualizer.fit(X)
visualizer.poof()

Choosing the final data features

rfm_final = rfm_df_scaled[[‘Amount’, ‘Frequency’]]
rfm_final.head()
# Select both features by creating a copy of the data variable
x = rfm_final.copy()
# Import a library which can do that easily
from sklearn import preprocessing
# Scale the inputs
# preprocessing.scale scales each variable (column in x) with respect to itself
# The new result is an array
x_scaled = preprocessing.scale(x)
x_scaled
# Fiddle with K (the number of clusters)
kmeans_new = KMeans(3)
# Fit the data
kmeans_new.fit(x_scaled)
# Create a new data frame with the predicted clusters
clusters_new = x.copy()
clusters_new[‘cluster_pred’] = kmeans_new.fit_predict(x_scaled)
# Plot
plt.figure(figsize=(10,6))
plt.scatter(clusters_new[‘Frequency’],clusters_new[‘Amount’],c=clusters_new[‘cluster_pred’],cmap=’rainbow’)
plt.xlabel(‘Frequency’)
plt.ylabel(‘Amount’)
plt.title(‘3 Cluster Plot’)

Formulated insights based on the analysis:

  • The biggest spenders normally have very low recencies, the volume of repeated orders contributes to most of the amount.
  • There are a lot of outliers from cluster 0, which covers the region where most of the average spending is made.
  • The cluster that has the highest recency does not normally contribute to the sales volume.
  • The main drivers are the amount and frequency and if both are increased we can optimize the sales.

To ensure an effective marketing strategy, the customer needs to be front and center as the saying goes, “Customer is always right”.

Instead of focusing on the masses by implementing an all-in-one approach, you’ll benefit from segmenting customers into groups that can be targeted with fitting marketing actions.

It will allow you to learn more about your customers, help you fit and customize content more effectively, create successful targeted campaigns and increase customer loyalty.

There are however many ways to break your customers out into groups. You may need to keep exploring to find what works best for your brand, you can then create buyer personas to guide you, then follow each one along their customer’s journey path keeping in mind that only “change” is constant. That we should keep on learning to keep pace with technology and our markets.

--

--