This GitHub repository contains a Jupyter notebook with Python code for customer segmentation and visualization using KMeans clustering. The code reads transaction data from an Excel file, preprocesses the data, performs KMeans clustering to segment customers, and visualizes the results using various plots.
Customer segmentation is a crucial task in understanding and targeting different customer groups. This repository provides a Jupyter notebook that demonstrates how to segment customers using KMeans clustering based on their transaction behavior. Additionally, it includes visualizations to better understand the characteristics of each customer segment.
Ensure you have the following Python libraries installed before running the notebook:
- pandas
- seaborn
- matplotlib
- scikit-learn
You can install the dependencies using the following command:
pip install pandas seaborn matplotlib scikit-learn-
Clone the repository:
git clone {url} -
Navigate to the repository directory:
cd customer-segmentation -
Open the Jupyter notebook:
jupyter notebook customer_segmentation.ipynb
-
Run the notebook cell by cell to execute the code.
The notebook reads transaction data from the 'online_retail_II.xlsx' file, preprocesses the data by cleaning and transforming it, and then generates customer features. KMeans clustering is applied to group customers into distinct segments based on their behavior.
The notebook includes visualizations to help interpret and understand the customer segments. It provides count plots of the number of customers in each cluster and box plots to visualize the distribution of features across different clusters. Additionally, t-SNE (t-distributed stochastic neighbor embedding) is used to create a 2D scatter plot for visualizing the clusters.
Feel free to explore and customize the notebook to suit your specific use case. If you have any questions or suggestions, please create an issue in this repository.
to run jupyter:
jupyter notebook
(Use Control-C to stop this server)
pip install -r requirements.txt
python -m pip install jupyter
memory profile:
@memory_profiler.profile
python -m memory_profiler main.py
from line_profiler_pycharm import profile
@profile
python -m line_profiler main.py.lprof > results.txt