ASSIGNMENT
ANALYSIS USING CLUSTERING ALGORITHM
A sample dataset is created
# Import necessary libraries
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns
from [Link] import StandardScaler
from [Link] import AgglomerativeClustering
from [Link] import dendrogram, linkage
from [Link] import make_blobs
# Step 1: Data Aggregation - Creating a sample customer dataset
[Link](42)
data, _ = make_blobs(n_samples=200, centers=4, cluster_std=1.5, random_state=42)
df = [Link](data, columns=['age', 'monthly_spending'])
df['tenure'] = [Link](1, 100, size=len(df))
df['num_products'] = [Link](1, 5, size=len(df))
# Display the first few rows
print("Sample data:\n", [Link]())
# Step 1: Data Preprocessing
# Check for missing values and handle them (if any)
df = [Link]([Link]())
# Standardize numerical features
scaler = StandardScaler()
features = ['age', 'tenure', 'monthly_spending', 'num_products']
df_scaled = scaler.fit_transform(df[features])
# Step 1: Visualize the distribution of features
fig, axes = [Link](2, 2, figsize=(10, 8))
for i, feature in enumerate(features):
[Link](df[feature], kde=True, ax=axes[i//2, i%2])
axes[i//2, i%2].set_title(f'Distribution of {feature}')
plt.tight_layout()
[Link]()
# Step 2: Clustering Using Hierarchical Clustering
# Use AgglomerativeClustering from scikit-learn
clustering_model = AgglomerativeClustering(n_clusters=4, metric='euclidean', linkage='ward')
df['cluster'] = clustering_model.fit_predict(df_scaled)
# Step 3: Cluster Evaluation - Plot Dendrogram
# Using 'ward' method for linkage to match clustering method
linked = linkage(df_scaled, method='ward')
[Link](figsize=(10, 7))
dendrogram(linked, orientation='top', distance_sort='descending', show_leaf_counts=False)
[Link]('Dendrogram')
[Link]()
# Step 3: Cluster Summary and Profiling
# Calculate summary statistics for each cluster
cluster_summary = [Link]('cluster')[features].agg(['mean', 'median', 'std'])
print("Cluster Summary:\n", cluster_summary)
# Step 4: Cluster Profiling - Visualization
# Scatter plot to visualize clusters based on 'age' and 'monthly_spending'
[Link](figsize=(10, 6))
[Link](data=df, x='age', y='monthly_spending', hue='cluster', palette='viridis')
[Link]('Customer Segmentation based on Age and Monthly Spending')
[Link]('Age')
[Link]('Monthly Spending')
[Link](title='Cluster')
[Link]()
# Pair plot for more comprehensive visualization
[Link](df, hue='cluster', palette='viridis', vars=['age', 'tenure', 'monthly_spending',
'num_products'])
[Link]()
Output :
Sample data:
age monthly_spending tenure num_products
0 7.438541 2.683919 52 1
1 -6.438815 10.247140 93 1
2 -8.856698 5.977641 15 3
3 -11.010454 5.212327 72 2
4 -7.763674 -5.605706 61 4
Cluster Summary:
age tenure \
mean median std mean median std
cluster
0 -6.311539 -7.375648 3.301562 68.253968 63.0 18.248260
1 -6.691180 -6.779066 1.702968 44.120000 44.0 26.481137
2 4.556176 4.728708 1.351635 53.620000 62.0 31.705848
3 -4.797515 -3.871234 2.980236 18.540541 15.0 14.641256
monthly_spending num_products
mean median std mean median std
cluster
0 8.127573 8.236881 1.548352 2.460317 2.0 1.267782
1 -6.894118 -6.730708 1.504644 2.960000 3.0 1.142143
2 2.228132 2.370098 1.531433 2.660000 3.0 1.061574
3 8.184105 8.170855 1.515391 2.567568 3.0 1.041914
REPORT
Objective:
The goal of this project is to analyze a customer dataset, apply hierarchical clustering to group customers
into meaningful clusters, and evaluate how customer features (like age, tenure, monthly spending, and
number of products) relate to these groups. This segmentation can provide valuable insights for
personalized marketing strategies.
Explanation of Steps
Step 1: Import Libraries
We start by loading essential libraries for data processing, visualization, and clustering. These libraries
help handle data (e.g., Pandas, NumPy), create charts (e.g., Matplotlib, Seaborn), and perform clustering
(e.g., Scikit-learn’s clustering module).
Step 2: Data Aggregation - Creating a Sample Dataset
For demonstration, we create a synthetic customer dataset with the following features:
• Age and Monthly Spending: These two primary features were simulated to form clusters based
on spending and age patterns.
• Tenure: Represents how long a customer has been with the company (e.g., in months or years).
• Number of Products: Reflects the variety of products each customer has purchased.
These attributes are commonly used for customer segmentation, helping reveal different types of
customer behaviors.
Step 3: Data Preprocessing
This step ensures the dataset is clean and ready for clustering. It involves:
1. Handling Missing Values: We check for missing data and fill any gaps with feature averages.
2. Feature Scaling: All numeric columns are standardized to ensure each feature contributes
equally to the clustering, as differences in feature scales can heavily impact clustering results.
Step 4: Visualizing Feature Distributions
To understand customer data better, we plot distributions for each feature. These histograms show the
spread of values for each feature, revealing patterns like whether spending is concentrated in certain age
groups or if tenure is normally distributed. These insights guide the selection of features for clustering.
Step 5: Clustering with Hierarchical Clustering
We apply hierarchical clustering using Agglomerative Clustering, which starts with each customer as
its own cluster and successively merges clusters based on similarity until a specified number of clusters
remains. This clustering method uses:
• Euclidean Distance: Measures similarity between customers.
• Ward’s Linkage: Minimizes variance within clusters, creating compact and well-defined
groups.
After clustering, each customer is assigned a cluster label, which indicates the group they belong to.
Step 6: Evaluating Clusters with a Dendrogram
To understand the clustering hierarchy, we plot a dendrogram. The dendrogram visually represents the
clustering process, showing how individual data points and clusters merge step-by-step. The height of
each branching point (merge) represents the distance between clusters at that merge, with larger
distances indicating more distinct groups. This plot helps decide on an appropriate number of clusters
by visually inspecting where large gaps in merging distance appear.
Step 7: Cluster Profiling and Summary Statistics
After assigning clusters, we calculate summary statistics for each cluster, like:
• Mean, Median, and Standard Deviation for features such as age, monthly spending, and
tenure. These statistics reveal the unique characteristics of each cluster, allowing us to label
groups based on traits (e.g., “high spenders with long tenure” or “younger customers with low
product engagement”).
Step 8: Visualizing Clusters
To make clusters more interpretable, we create visualizations that show how each cluster differs from
others:
1. Scatter Plot: Maps clusters based on two features (e.g., age vs. monthly spending), with colors
differentiating clusters. This helps verify that clusters are distinct and visually separate.
2. Pair Plot: A more comprehensive set of scatter plots that displays relationships across multiple
feature combinations, revealing inter-cluster and intra-cluster dynamics.
Conclusion
Hierarchical clustering effectively groups customers based on their behaviors and demographics. Key
takeaways include:
• Dendrogram Analysis: Helps decide the ideal number of clusters.
• Cluster Profiling: Provides specific customer group characteristics, aiding in targeted
marketing.
• Visualizations: Offer a clearer understanding of cluster separations and relationships, ensuring
clusters are meaningful.