A machine learning project for segmenting wholesale customers using the DBSCAN density-based clustering algorithm, with a focus on identifying natural customer groups, detecting outliers, and analyzing spending behaviors across various product categories.
This project applies DBSCAN, a density-based clustering algorithm, to the popular Wholesale Customers Dataset.
The goal is to:
- Discover natural customer groups
- Detect abnormal high-spending outliers
- Analyze differences in purchase patterns
- Understand spending behavior across categories such as Milk, Grocery, Fresh, Frozen, and Detergents_Paper
This type of segmentation is widely used in real-world applications like customer profiling, market analysis, and retail intelligence.
- Name: Wholesale Customers Dataset
- Source: Data set
- Records: 440 customers
- Features:
- Channel (Hotel/Restaurant/Retail)
- Region
- Fresh
- Milk
- Grocery
- Frozen
- Detergents_Paper
- Delicassen
These features represent annual spending levels for each customer.
- Previewed the dataset and inspected NaN values
- Basic visualizations:
- Scatterplots (Milk vs Grocery)
- Histograms colored by Channel
- PairPlot by Region
- Correlation heatmap
- Seaborn Clustermap for feature similarity
Since the spending categories vary greatly (some up to 100,000 units), Standard Scaling was applied.
Tested multiple epsilon values:
for eps in np.linspace(0.001, 3, 50):
DBSCAN(eps=eps).fit_predict(scaled_data)
For each model, recorded:
- Number of clusters
- Number of outliers
- Percentage of outliers
This allowed identifying a stable and meaningful eps region.
Final eps chosen: eps = 2
Created cluster-colored scatterplots:
- Grocery vs Milk
- Milk vs Detergents_Paper
- Other spending relationships
A new column Labels stores DBSCAN cluster assignments.
Then compared cluster means:
cat_means = df.groupby("Labels").mean()
Outlier cluster (-1) represented extremely high spenders.
Clusters 0 and 1 represent medium and low spenders.
Used MinMaxScaler to normalize group means and produce a 0–1 heatmap.
This clearly reveals:
- Outlier cluster has extremely high spending in all categories
- Cluster 0: Average spending, moderate levels of Detergents_Paper
- Cluster 1: Low-volume grocery/milk customers
- numpy
- pandas
- seaborn
- matplotlib
- scikit-learn (DBSCAN, StandardScaler, MinMaxScaler)
pip install -r requirements.txt
- requirements.txt → file
or directly:
pip install numpy pandas seaborn matplotlib scikit-learn
Run all cells to train and evaluate the model.
This project is implemented as a Python script.
(No Jupyter Notebook version yet)
The DBSCAN model revealed three meaningful behavioral groups:
- Extremely high spenders across all categories
- Represent unique customer behavior
- Most likely wholesale distributors or very large retailers
- Medium-to-high grocery and detergent spending
- Likely restaurants or hotels
- Low spending across all features
- Small shops or low-volume buyers
This project demonstrates the power of density-based clustering for customer segmentation:
- No need to predefine K
- Natural detection of non-linear clusters
- Automatic identification of extreme-value customers
- Clear interpretation via cluster means and heatmaps
DBSCAN provides robust segmentation for noisy, high-variance datasets like wholesale customer data.
Author: Ali
Field: Data Science & Machine Learning Student
Email: ali.hz87980@gmail.com
GitHub: ali-119