Vishal Reddy Jakkareddy & PruthviRaj Amgoth (Advisor: Dr. Cohen)
2025-04-14
Introduction
Customer segmentation is a key strategy in e-commerce that helps businesses classify customers based on their purchasing behavior.
It enables targeted marketing efforts and supports customer retention strategies.
Traditionally, segmentation was based on demographic attributes.
With the rise of machine learning, data-driven methods like K-Means clustering have become more prevalent.
This study uses K-Means clustering to analyze an online retail dataset and identify customer groups based on purchase behaviors.
Recent research emphasizes the effectiveness of clustering algorithms for customer segmentation.
Introduction
K-Means clustering is favored for its efficiency and ease of implementation (Paramita and Hariguna 2024).
The study adopts K-Means clustering using R to enable a data-driven approach for customer analysis and business decision-making.
Methods
K-Means Clustering Algorithm
The K-Means clustering algorithm is an iterative approach for partitioning a dataset into k clusters. It follows these steps to achieve convergence:
Initialize Cluster Centers:
Select k initial cluster centroids randomly or use K-Means++ (K-Means++ is an improved initialization technique that places centroids far apart to enhance convergence and accuracy.) for optimized placement.
This ensures centroids are well-separated initially.
Repeat Until Convergence (Image Source: Shiyu Gong)
Selection of k (Number of Clusters)
The optimal number of clusters k is determined using the Elbow Method, which plots WCSS against k values. The elbow point—where the rate of decrease in WCSS (Within-Cluster Sum of Squares (WCSS) is a measure of the variance within clusters. The elbow point—where the curve bends—indicates the optimal number of clusters.) diminishes—indicates the optimal k.
The Online Retail dataset, sourced from the UCI Machine Learning Repository, contains transactional data from a UK-based e-commerce store. This store specializes in selling unique giftware, which is frequently purchased in bulk by customers. The dataset spans transactions recorded between December 1, 2009, and December 9, 2011. (Chen 2015)
This dataset is particularly useful for customer segmentation, sales analysis, and market trend evaluation. It includes eight attributes that provide insights into customer purchases, product details, and order quantities. The data can be leveraged for analyzing buying behaviors, identifying customer clusters, and predicting future sales trends.
The dataset contains transactional records from an online retail store. The key attributes in the dataset include:
The dataset has 541,909 rows and 8 columns. CustomerID has 135,080 missing values (25% missing rate), and Description has 1,454 missing values. Quantity and UnitPrice show extreme values (e.g., negative quantities, very high prices), indicating the need for cleaning.
The visualization shows that CustomerID has the highest proportion of missing values (0.25), often paired with missing Description values (0.01 proportion of combinations).
Quantity Histogram (A): Most transactions involve quantities between 1 and 5, with a right-skewed distribution.
Price Histogram (B): Most items are priced between 1 and 4 units, also right-skewed.
Top 5 StockCode (C): StockCode 85123A has the highest sales count (~2,000 transactions).
Top 5 Countries (D): The United Kingdom dominates with over 300,000 transactions, followed by EIRE, France, Germany, and Spain.
This reduces the dataset to valid transactions, removing 135,080 rows with missing CustomerID, cancelled transactions (starting with “C”), and entries with non-positive Quantity or UnitPrice.
Modeling and Results
RFM Analysis and Data Preparation
RFM analysis quantifies customer behavior using three metrics:
Recency: Days since the last purchase (relative to December 10, 2011).
Outliers in Recency, Frequency, and Monetary are removed using the IQR method.
Log transformation (log1p) reduces skewness in the RFM metrics.
Data is scaled to ensure equal weighting of features during clustering.
The boxplots show that after transformation, the distributions are more symmetric, though some outliers remain (e.g., high Monetary values).
K-Means Clustering Analysis
Determining Optimal k
In this step, we are using the Elbow Method to determine the optimal number of clusters (k) for K-Means clustering. The plot displays the Total Within Sum of Squares (WSS) for different values of k, and we observe how the WSS decreases as the number of clusters increases. In our case, the elbow clearly appears at k = 3, meaning that three clusters provide the best segmentation of the data without overfitting. Choosing more than three clusters would result in only marginal improvement while increasing complexity unnecessarily.
Applying K-Means Clustering
The visualization illustrates the results of a K-Means clustering algorithm applied to customer data, projected onto two principal components—Dim1 and Dim2—which together capture approximately 95.6% of the total variance (Dim1: 73.3%, Dim2: 22.3%). The plot reveals three distinct customer segments, each represented by a unique color and shape: Cluster 1 (blue circles), Cluster 2 (yellow triangles), and Cluster 3 (grey squares). The separation between clusters is generally clear, indicating well-defined groupings; however, there is a slight overlap between Clusters 2 and 3, suggesting the possibility of similar behaviors or characteristics among customers near the boundary. This dimensionality reduction has proven effective, as most of the important information in the dataset is retained in just two dimensions, allowing for clear interpretation and visual segmentation of the customer base. These insights can be valuable for targeted marketing, personalized services, or strategic business decisions.
Dim1 and Dim2 are principal components resulting from a dimensionality reduction technique
Cluster Profiling
Cluster Profiling is the process of analyzing the characteristics of different customer segments after applying a clustering algorithm (like K-means). It helps us understand the behavior and value of each group, making it easier to tailor marketing strategies and business decisions. In this analysis, we’ve grouped customers into three clusters based on their Recency, Frequency, and Monetary values (RFM analysis). We then calculated the average values of these metrics for each cluster to interpret their purchasing behavior. By visualizing these summaries, we can clearly identify which clusters contain loyal customers, occasional buyers, or those at risk of churn—allowing businesses to take targeted actions accordingly.
Insights
Cluster 1 (Green): Moderate recency (~57 days), low frequency (~3 transactions), and moderate spending (~5.56 log units). These are occasional buyers.
Cluster 2 (Yellow): Low recency (~37 days), high frequency (~4.78 transactions), and high spending (~7.33 log units). These are loyal, high-value customers.
Cluster 3 (Blue): High recency (~256 days), low frequency (~2.60 transactions), and low spending (~5.13 log units). These are inactive customers at risk of churn.
Conclusion
K-Means clustering identified three customer segments:
Occasional Buyers (Cluster 1): Customers who purchase infrequently with moderate spending.
Loyal Customers (Cluster 2): Frequent buyers with high spending, ideal for retention strategies.
These segments enable targeted marketing: loyalty programs for Cluster 2, re-engagement offers for Cluster 3, and promotional deals for Cluster 1.
This study demonstrates the effectiveness of K-Means clustering for customer segmentation in e-commerce. By applying RFM analysis and K-Means clustering to the Online Retail dataset, we identified three distinct customer segments with actionable insights for marketing strategies. Future work could explore alternative clustering methods (e.g., DBSCAN) or incorporate additional features like product categories to enhance segmentation.
References
Bradley, Paul S, Kristin P Bennett, and Ayhan Demiriz. 2000. “Constrained k-Means Clustering.”Microsoft Research, Redmond 20 (0): 0.
Paramita, Adi Suryaputra, and Taqwa Hariguna. 2024. “Comparison of k-Means and DBSCAN Algorithms for Customer Segmentation in e-Commerce.”Journal of Digital Market and Digital Currency 1 (1): 43–62.
Tabianan, Kayalvily, Shubashini Velu, and Vinayakumar Ravi. 2022. “K-Means Clustering Approach for Intelligent Customer Segmentation Using Customer Purchase Behavior Data.”Sustainability 14 (12): 7243.