The Wayback Machine - https://web.archive.org/web/20240717024443/https://www.geeksforgeeks.org/customer-segmentation-using-unsupervised-machine-learning-in-python/
Open In App

Customer Segmentation using Unsupervised Machine Learning in Python

Last Updated : 21 Nov, 2022
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

In today’s era, companies work hard to make their customers happy. They launch new technologies and services so that customers can use their products more. They try to be in touch with each of their customers so that they can provide goods accordingly. But practically, it’s very difficult and non-realistic to keep in touch with everyone. So, here comes the usage of Customer Segmentation.

Customer Segmentation means the segmentation of customers on the basis of their similar characteristics, behavior, and needs. This will eventually help the company in many ways. Like, they can launch the product or enhance the features accordingly. They can also target a particular sector as per their behaviors. All of these lead to an enhancement in the overall market value of the company.

Customer Segmentation using Unsupervised Machine Learning in Python

Today we will be using Machine Learning to implement the task of Customer Segmentation.

Import Libraries

The libraries we will be required are : 

  • Pandas – This library helps to load the data frame in a 2D array format.
  • Numpy – Numpy arrays are very fast and can perform large computations.
  • Matplotlib / Seaborn – This library is used to draw visualizations.
  • Sklearn – This module contains multiple libraries having pre-implemented functions to perform tasks from data preprocessing to model development and evaluation.

Python3




import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
 
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.cluster import KMeans
 
import warnings
warnings.filterwarnings('ignore')


Importing Dataset

The dataset taken for the task includes the details of customers includes their marital status, their income, number of items purchased, types of items purchased, and so on.

Python3




df = pd.read_csv('new.csv')
df.head()


Output:  

Customer Segmentation using Unsupervised Machine Learning - Input

 

To check the shape of the dataset we can use data.shape method.

Python3




df.shape


Output:

(2240, 25)(2240, 25)

To get the information of the dataset like checking the null values, count of values, etc. we will use .info() method.

Data Preprocessing

Python3




df.info()


Output:

Customer Segmentation using Unsupervised Machine Learning

 

Python3




df.describe().T


Output:

Customer Segmentation using Unsupervised Machine Learning

 

Improving the values in the Accepted column.

Python3




df['Accepted'] = df['Accepted'].str.replace('Accepted', '')


To check the null values in the dataset.

Python3




for col in df.columns:
    temp = df[col].isnull().sum()
    if temp > 0:
        print(f'Column {col} contains {temp} null values.')


Output:

 Column Income contains 24 null values.

Now, once we have the count of the null values and we know the values are very less we can drop them (it will not affect the dataset much).

Python3




df = df.dropna()
print("Total missing values are:", len(df))


Output:

 Total missing values are: 2216

To find the total number of unique values in each column we can use data.unique() method. 

Python3




df.nunique()


Output:

Customer Segmentation using Unsupervised Machine Learning

 

Here we can observe that there are columns which contain single values in the whole column so, they have no relevance in the model development.

Also dataset has a column Dt_Customer which contains the date column, we can convert into 3 columns i.e. day, month, year. 

Python3




parts = df["Dt_Customer"].str.split("-", n=3, expand=True)
df["day"] = parts[0].astype('int')
df["month"] = parts[1].astype('int')
df["year"] = parts[2].astype('int')


Now we have all the important features, we can now drop features like Z_CostContact, Z_Revenue, Dt_Customer.

Python3




df.drop(['Z_CostContact', 'Z_Revenue', 'Dt_Customer'],
        axis=1,
        inplace=True)


Data Visualization and Analysis

Data visualization is the graphical representation of information and data in a pictorial or graphical format. Here we will be using bar plot and count plot for better visualization.

Python3




floats, objects = [], []
for col in df.columns:
    if df[col].dtype == object:
        objects.append(col)
    elif df[col].dtype == float:
        floats.append(col)
 
print(objects)
print(floats)


Output:

['Education', 'Marital_Status', 'Accepted']
['Income']

To get the count plot for the columns of the datatype – object, refer the code below.

Python3




plt.subplots(figsize=(15, 10))
for i, col in enumerate(objects):
    plt.subplot(2, 2, i + 1)
    sb.countplot(df[col])
plt.show()


Output:

Customer Segmentation using Unsupervised Machine Learning

 

Let’s check the value_counts of the Marital_Status of the data.

Python3




df['Marital_Status'].value_counts()


Output:

Customer Segmentation using Unsupervised Machine Learning

 

Now lets see the comparison of the features with respect to the values of the responses.

Python3




plt.subplots(figsize=(15, 10))
for i, col in enumerate(objects):
    plt.subplot(2, 2, i + 1)
    sb.countplot(df[col], hue=df['Response'])
plt.show()


Output:

Customer Segmentation using Unsupervised Machine Learning

 

Label Encoding  

Label Encoding is used to convert the categorical values into the numerical values so that model can understand it.

Python3




for col in df.columns:
    if df[col].dtype == object:
        le = LabelEncoder()
        df[col] = le.fit_transform(df[col])


Heatmap is the best way to visualize the correlation among the different features of dataset. Let’s give it the value of 0.8

Python3




plt.figure(figsize=(15, 15))
sb.heatmap(df.corr() > 0.8, annot=True, cbar=False)
plt.show()


Output:

Customer Segmentation using Unsupervised Machine Learning

 

Standardization

Standardization is the method of feature scaling which is an integral part of feature engineering. It scales down the data and making it easier for the machine learning model to learn from it. It reduces the mean to ‘0’ and the standard deviation to ‘1’.

Python3




scaler = StandardScaler()
data = scaler.fit_transform(df)


Segmentation

We will be using T-distributed Stochastic Neighbor Embedding. It helps in visualizing high-dimensional data. It converts similarities between data points to joint probabilities and tries to minimize the values to low-dimensional embedding.

Python3




from sklearn.manifold import TSNE
model = TSNE(n_components=2, random_state=0)
tsne_data = model.fit_transform(df)
plt.figure(figsize=(7, 7))
plt.scatter(tsne_data[:, 0], tsne_data[:, 1])
plt.show()


Output:

Customer Segmentation using Unsupervised Machine Learning

 

There are certainly some clusters which are clearly visual from the 2-D representation of the given data. Let’s use the KMeans algorithm to find those clusters in the high dimensional plane itself

KMeans Clustering can also be used to cluster the different points in a plane.

Python3




error = []
for n_clusters in range(1, 21):
    model = KMeans(init='k-means++',
                   n_clusters=n_clusters,
                   max_iter=500,
                   random_state=22)
    model.fit(df)
    error.append(model.inertia_)


Here inertia is nothing but the sum of squared distances within the clusters.

Python3




plt.figure(figsize=(10, 5))
sb.lineplot(x=range(1, 21), y=error)
sb.scatterplot(x=range(1, 21), y=error)
plt.show()


Output:

Customer Segmentation using Unsupervised Machine Learning

 

Here by using the elbow method we can say that k = 6 is the optimal number of clusters that should be made as after k = 6 the value of the inertia is not decreasing drastically.

Python3




# create clustering model with optimal k=5
model = KMeans(init='k-means++',
               n_clusters=5,
               max_iter=500,
               random_state=22)
segments = model.fit_predict(df)


Scatterplot will be used to see all the 6 clusters formed by KMeans Clustering.

Python3




plt.figure(figsize=(7, 7))
sb.scatterplot(tsne_data[:, 0], tsne_data[:, 1], hue=segments)
plt.show()


Output:

 



Previous Article
Next Article

Similar Reads

Unsupervised Machine Learning - The Future of Cybersecurity
Cybersecurity is like Tom and Jerry! While Tom always tries new ways to catch Jerry, he manages to escape in some way or another. Most of the Cybersecurity teams find themselves in the unenviable position of Tom, where they can try whatever methods they like, Jerry always escapes and tries to get the cheese in even more creative ways next time! Tod
7 min read
Unsupervised Machine Learning Examples
Unsupervised machine learning represents a pivotal domain within artificial intelligence, emphasizing the extraction of patterns and structures from data devoid of prior labeling. Unlike its supervised counterpart, which relies on labeled outcomes to guide predictions, unsupervised algorithms delve into the intrinsic characteristics of data to disc
6 min read
Can I Use Unsupervised Learning Followed by Supervised Learning?
Answer : Yes, you can use unsupervised learning to discover patterns or features and then apply supervised learning for prediction or classification tasks.Combining unsupervised learning with supervised learning is a powerful strategy that leverages the strengths of both approaches to enhance the performance of machine learning models. This combina
2 min read
Real-Life Examples of Supervised Learning and Unsupervised Learning
Two primary branches of machine learning, supervised learning and unsupervised learning, form the foundation of various applications. This article explores examples in both learnings, shedding light on diverse applications and showcasing the versatility of machine learning in addressing real-world challenges. Examples of Supervised Learning and Uns
6 min read
Customer Segmentation using KMeans in R
Customer segmentation is one of unsupervised learning's most important applications. Employing clustering algorithms to identify the numerous customer subgroups enables businesses to target specific consumer groupings. In this machine learning project, K-means clustering, a critical method for clustering unlabeled datasets, will be applied. In the
10 min read
Customer Segmentation via Cluster Analysis
Customer segmentation via clustering analysis is a critical part of the current marketing and analytics systems. Customer segmentation is performed by grouping customers based on their common traits that permit the businesses to plan, develop, and deliver their strategies, products, and services thus more efficiently. Through data mining, retailers
4 min read
Semantic Segmentation vs Instance Segmentation
Image segmentation task involves partitioning the image into many segments or regions based on color, intensity, texture or spatial proximity. In this article, we are going to understand semantic segmentation, instance segmentation and their key differences. What is Image Segmentation?Image segmentation is a computer vision task that aims at identi
5 min read
Revolutionizing Retail: Leveraging Machine Learning for Customer Insights
As we know the integration of machine learning is not just a trend but a necessity for staying competitive. Machine learning, with its capability to analyze vast amounts of data and extract valuable insights, is revolutionizing how retailers understand and engage with their customers. This article explores the importance of customer insights and ho
7 min read
What is Data Segmentation in Machine Learning?
In machine learning, the effective utilization of data is paramount. Data segmentation stands as a crucial process in this landscape, facilitating the organization and analysis of datasets to derive meaningful insights. From enhancing model accuracy to optimizing decision-making processes, data segmentation plays a pivotal role. Let's delve deeper
15 min read
Supervised and Unsupervised Learning in R Programming
Arthur Samuel, a pioneer in the field of artificial intelligence and computer gaming, coined the term “Machine Learning”. He defined machine learning as – “Field of study that gives computers the capability to learn without being explicitly programmed”. In a very layman manner, Machine Learning(ML) can be explained as automating and improving the l
8 min read
Practice Tags :