Customer segmentation is a powerful way for businesses to understand and group their customers based on behavior. This project performs RFM Analysis (Recency, Frequency, Monetary) and uses K-Means Clustering to discover unique customer segments from retail transaction data.
- File:
Online Retail.xlsx - Description: Real-world online retail dataset containing transactions from 2010–2011.
- Key Fields:
CustomerID,InvoiceNo,InvoiceDate,Quantity,UnitPrice
- Removed rows with missing
CustomerID - Filtered out transactions with negative
QuantityorUnitPrice - Created a new column:
TotalPrice = Quantity × UnitPrice
- Recency: Days since last purchase (from the most recent date)
- Frequency: Number of unique purchases (
InvoiceNo) - Monetary: Total spending per customer
- Optimal number of clusters (
k) is chosen based on lowest inertia and silhouette evaluation.
- Created a reusable scikit-learn pipeline with:
StandardScalerKMeans
- Trained on the RFM features and predicted segments across the dataset.
customer_segments.csv:- Contains:
CustomerID, RFM metrics, assigned cluster
- Contains:
- Use these segments to identify:
- VIP customers
- At-risk/dormant users
- Frequent but low spenders
- New or seasonal shoppers
- Python 3
pandas,numpy,matplotlib,seaborn,yellowbrickscikit-learnfor clustering and pipelinejupyterfor interactive development
-
Clone the repo
git clone https://github.com/yourusername/customer-segmentation.git cd customer-segmentation -
Install dependencies
pip install -r requirements.txt
-
Place dataset in the project root.
-
Run the script to generate visualizations and the final csv.
python main.py



