← Back to list

Understanding Customer Behavior Through Clustering

#inst414spr26a04

Amanze Maryanne in INST414: Data Science Techniques · 2026-05-08 01:35 · 0 claps · 5.5 min read
#data-science #machine-learning #k-means-clustering #euclidean-distance #inst414spr26a04
Open on Medium ↗
Wiki topics: ML · Machine Learning EDU · Education & Learning 🔬 · Science · General

Understanding Customer Behavior Through Clustering

inst414spr26a04

Introduction

Retail businesses generate large amounts of transactional data, but making sense of customer behavior within the data available can be very challenging. One key question businesses can ask is:

Does location affect customers’ purchasing habits?

Answering this question will not only give them a better sense of who their customers are and what they like but it will also help them understand what products to invest more money in and what products to drop.

This question is important for marketing managers, who rely on customer insights to improve sales strategies, personalize marketing, and increase customer retention. In this analysis, I use clustering techniques to identify distinct groups of customers based on their purchasing patterns.

The Dataset

The dataset used in this analysis contains transactional sales data from a retail supermarket chain, capturing detailed information about customer purchases across multiple store locations. Each row represents a single transaction and includes a mix of customer demographics, product details, and purchase behavior. Key fields include invoice ID, branch, city, customer type, gender, product line, unit price, quantity, total purchase amount, tax, and date of transaction.

These features are relevant to the research question because they provide a comprehensive view of how customers interact with the retail environment. For example, the total purchase amount and quantity reflect spending behavior, while product line reveals preferences across categories such as health and beauty, food and beverages, and electronics.

By combining these variables, the dataset supports a multidimensional analysis of customer activity, making it well-suited for clustering.

Link to Data Source:

https://www.kaggle.com/c/walmart-recruiting-store-sales-forecasting

Here is a preview of the fields contained in this dataset:

  • Invoice ID — a unique identifier for each transaction
  • Branch — the store branch where the purchase occurred (A, B, or C)
  • City — the city in which the branch is located (e.g., Yangon, Mandalay, Naypyitaw)
  • Gender — demographic attribute of the customer
  • Product Line — category of the purchased product (e.g., Health and Beauty, Food and Beverages, Electronic Accessories)

Data Collection

The dataset was obtained from a publicly available source and imported into Python using the pandas library. The file was provided in CSV format, which made it easy to load and work with using standard data analysis tools. Since the dataset was already cleanly structured with clearly defined columns and consistent formatting, no additional data collection methods such as web scraping or API requests were required. After loading the data, an initial inspection was performed to understand the variables, check data types, and confirm that the dataset was suitable for analysis. This ensured that the data could be used directly for clustering without extensive preprocessing or restructuring.

Clustering Analysis of Walmart Sales Data

1. Measuring Similarity Between Data Points

To measure similarity between data points, I used the following features from the dataset:

  • City
  • Product line
  • Total (purchase amount)

Since clustering algorithms like k-means require numerical input, I converted categorical variables (City and Product line) into numeric form using encoding (e.g., one-hot encoding or label encoding). I also kept Total as a continuous numerical feature. I measured similarity between data points using Euclidean distance, which is the default distance metric used by k-means. This means that transactions are grouped together based on how close they are in terms of: Spending amount, Product category, Location. So, two transactions are considered similar if they: Occur in the same city, Belong to similar product lines or, Have similar total purchase amounts.

2. Selecting the Value of k (Number of Clusters)

To determine the number of clusters (k), I used the Elbow Method. Firstly, I ran k-means clustering for values of k from 1 to 9. And then, I plotted the inertia (within-cluster sum of squares) for each k. From my graph, there is a steep decrease in inertia from k = 1 to k = 3 and after that, the curve begins to flatten. So I chose k = 4.

3. Description of Each Cluster

Cluster 0 is largely made up of transactions from Yangon and reflects customers with relatively high spending across a variety of product lines. The purchases in this cluster include categories such as health and beauty, home and lifestyle, sports and travel, and electronic accessories. The spending amounts in this group are generally on the higher side, with several transactions exceeding 400 or even 600. This suggests that customers in Yangon tend to make larger purchases and are not limited to a single product category, indicating a more diverse and possibly more financially flexible customer base.

Cluster 1 is dominated by transactions from Naypyitaw and represents another high-spending group, but with a slightly different focus. In this cluster, there is a strong emphasis on electronic accessories and food and beverages, along with some variation in lifestyle-related purchases. The spending values are among the highest in the dataset, including transactions above 600. Compared to Cluster 0, this group appears more concentrated in specific categories, suggesting that customers in Naypyitaw may have more targeted purchasing habits, especially toward electronics and essential goods, while still maintaining high spending levels.

Cluster 2 is primarily composed of transactions from Mandalay and stands out due to its generally lower spending amounts. The purchases in this cluster span multiple categories, including food and beverages, fashion accessories, electronic accessories, and sports and travel, but the transaction values are noticeably smaller, with some as low as around 60. This indicates that customers in Mandalay may be more price-sensitive or budget-conscious. Even when they purchase across different product lines, their overall spending remains relatively low compared to the other clusters.

Cluster 3 also consists mainly of customers from Naypyitaw, but it differs from Cluster 1 in terms of spending variability and product focus. This cluster includes a mix of high and moderate spending, with some of the highest values in the dataset, particularly in categories like home and lifestyle and health and beauty. The presence of both essential and lifestyle purchases suggests that this group represents customers who are willing to spend more on comfort, quality of life, or non-essential goods. Compared to the other Naypyitaw cluster, this group appears less focused on a single category and more oriented toward a broader, lifestyle-driven purchasing pattern.

Overall, the clusters reveal that location plays a significant role in customer behavior, with Yangon associated with diverse and high spending, Mandalay associated with lower spending, and Naypyitaw showing two distinct high-value segments with different purchasing priorities.

Visualizations:

Validation of Input Data and Results

To ensure the analysis was accurate and reasonable, several validation steps were performed. The dataset was first examined for missing or incorrect values, and it was confirmed that the relevant fields were properly formatted. Categorical variables such as city and product line were converted into numerical representations so they could be used in the clustering algorithm, while the total purchase amount was retained as a continuous variable. Because AI assistance was used, additional steps were taken to verify its accuracy. The explanations generated were compared directly with the actual output from the notebook and code in google colab.

Limitations of my Analysis

There are several limitations to this analysis that should be considered. One limitation is that only a small number of features were used, specifically city, product line, and total purchase amount. Other potentially important variables, such as customer demographics, purchase dates, or quantity of items purchased, were not included and could provide additional insights if incorporated.

Additionally, the process of encoding categorical variables introduces some limitations, as it can impose artificial numerical relationships between categories that do not naturally exist. The choice of k, while supported by the elbow method, is still somewhat subjective, and selecting a different number of clusters could lead to different interpretations.

GitHub Link of Code:

https://github.com/MaryanneAmanze/INST414_Module4Assignment-.git


메타데이터
post_id
d517d55f2e7d
slug
understanding-customer-behavior-through-clustering-d517d55f2e7d
url
https://medium.com/inst414-data-science-tech/understanding-customer-behavior-through-clustering-d517d55f2e7d
canonical_url
https://medium.com/inst414-data-science-tech/understanding-customer-behavior-through-clustering-d517d55f2e7d
author_url
https://medium.com/@amanzemaryanne
status
ok
fetched_at
2026-07-13 06:23:13