YAML Metadata Warning:empty or missing yaml metadata in repo card

Check out the documentation for more information.

E-Commerce Customer Behavior and Sales Analysis

Project Presentation

▶️ Watch the video presentation on YouTube


Overview

This project analyzes the E-Commerce Customer Behavior and Sales Analysis Dataset.
The goal of the project is to explore customer purchasing behavior, understand which factors influence spending, and build machine learning models that can predict customer purchase value.


Dataset Information

The dataset contains transactional and behavioral information about customers in an e-commerce environment.

Main features include:

Feature Group Examples Description
Customer Information Customer ID, Gender, Age, Location Describes customer demographic and identity-related information
Product Information Product Category, Product Name Describes the type of products purchased by customers
Transaction Information Purchase Frequency, Quantity, Total Spend Describes customer purchasing activity and spending behavior

This variety of features provides a strong basis for analyzing customer behavior from multiple perspective


Research Goal

The main question explored in this project is:

What factors influence customer spending behavior, and how accurately can we predict the total purchase amount?

The project first treats Total_Amount as a numerical regression target. Later, the target is converted into three purchase classes — Low, Medium, and High — for classification modeling.


Data Cleaning

The dataset was examined for data quality issues before analysis.

Missing Values

No missing values were found in the dataset.
This means the dataset was complete and did not require imputation or removal of missing entries.

Duplicate Rows

Duplicate rows were checked, and no duplicate records were found.
This ensures that each observation is unique and that repeated data does not affect the analysis.

Date Parsing

The dataset includes a Date column representing transaction time.
The date column was reviewed and later converted into datetime format when time-based features were needed.

Categorical Consistency

Categorical columns were reviewed for spelling mistakes, typos, and inconsistent values.

The checked categorical columns included:

  • Gender
  • City
  • Product_Category
  • Payment_Method
  • Device_Type

All categorical values appeared consistent and well-structured.

Scaling Issues

Numerical features were reviewed for scale differences.
Features such as Total_Amount and Unit_Price have much larger values than features such as Quantity or Customer_Rating.

Scaling was not required for the initial EDA, but it was later applied during feature engineering and modeling.


Outlier Detection and Handling

Outliers were examined as part of the exploratory data analysis in order to identify unusually high or low values that could affect the results.

First, summary statistics were reviewed to check the value ranges of the numerical features. Then, distribution plots were created for selected variables such as Unit_Price, Total_Amount, and Age to better understand how the values are spread.

In addition, the IQR method was used to detect outliers across the numerical columns.

The results showed that most features had reasonable value ranges. However, some variables, especially Unit_Price, Discount_Amount, and Total_Amount, contained extreme high values and showed a right-skewed distribution.

Since these values may represent real customer behavior, they were not removed from the dataset. Instead, capping was applied using the IQR method in order to reduce the influence of extreme values while keeping the original data structure.

Features that did not show significant outliers were left unchanged.

Decision

Outliers were mainly found in Unit_Price, Discount_Amount, and Total_Amount.
These values may represent real customer behavior, but they can also strongly affect model performance.

Therefore, capping was applied to the strongest outlier features using the IQR method.
This approach limits extreme values while preserving the overall structure of the dataset.


Descriptive Statistics

Descriptive statistics were used to understand the central tendency, spread, and range of the numerical variables.

A new feature called Discount_Percentage was created to better understand discounts relative to product price.

image

image

Main Insight

Total_Amount is mainly driven by product-related and transaction-related factors.

Key observations:

  • Unit_Price has a strong positive relationship with Total_Amount.
  • Quantity has a moderate positive relationship with Total_Amount.
  • Discount_Amount has a weaker positive relationship.
  • Discount_Percentage shows a negative relationship with Total_Amount.
  • Age, Session_Duration_Minutes, Pages_Viewed, and Delivery_Time_Days show weak or minimal correlation with Total_Amount.

Overall, spending behavior is mainly affected by product price, quantity, and discount-related patterns rather than customer demographic features alone.


Visualizations, Questions, and Insights

1. Product Category vs Total Purchase Amount

What was done:
A bar chart was used to compare the average Total_Amount across product categories.

Question:
Does product category affect the total purchase amount?

image

Answer / Insight:
Customers spend the most on Electronics and the least on Books.
This shows that product category has a clear influence on total spending.


2. City vs Total Purchase Amount

What was done:
A pie chart was used to show the distribution of total spending across cities.

Question:
Does the city influence the total purchase amount?

image

Answer / Insight:
Spending is distributed across several cities, with Istanbul contributing the most.
However, no single city completely dominates the dataset, suggesting that city has a moderate influence on total purchase amount.


3. Unit Price, Quantity, and Total Amount

What was done:
A visualization was created to examine how Unit_Price and Quantity together affect Total_Amount.

Question:
How do price and quantity together influence the total purchase amount?

image

Answer / Insight:
The graph shows a clear relationship between unit price, quantity, and total purchase amount.
As both price and quantity increase, the total amount also increases.
This confirms that total spending is strongly driven by the combined effect of price and quantity.


4. Gender and Product Category Spending

What was done:
Charts were used to compare spending patterns by gender across product categories.

Question:
Does gender influence spending across product categories?

image

Answer / Insight:
Spending distribution across product categories is very similar for male and female customers.
Electronics is the largest spending category for both groups, followed by Home & Garden and Sports.
This suggests that gender does not strongly influence total spending in this dataset.


5. Age Group vs Total Purchase Amount

What was done:
Age was divided into groups, and average spending was compared across those groups.

Question:
How does age influence the total purchase amount?

image

Answer / Insight:
Spending levels are relatively similar across all age groups.
Customers aged 65 and above show slightly higher spending, but the overall differences are small.
This indicates that age does not strongly influence the total purchase amount.


6. Payment Method and Device Type

What was done:
A heatmap was used to examine average spending by payment method and device type.

Question:
How do payment method and device type together influence the total purchase amount?

image

Answer / Insight:
Spending patterns vary slightly across payment methods and device types.
Bank Transfer and Digital Wallet tend to show higher spending, especially on Desktop and Tablet devices.
Cash on Delivery generally shows lower spending levels, particularly on Desktop.


7. Discount Percentage vs Total Amount

What was done:
Discount percentages were grouped into ranges, and average spending was compared across discount groups.

Question:
How does discount percentage influence the total purchase amount?

image

Answer / Insight:
As discount percentage increases, the total purchase amount decreases significantly.
The highest spending occurs at low discount levels, especially 0–10%.
The lowest spending occurs at high discount levels, especially 50%+.
This suggests that higher discounts are associated with lower total spending.


8. Discount Percentage and Product Category

What was done:
A heatmap was used to examine average total amount across product categories and discount percentage groups.

Question:
How does discount percentage influence spending across different product categories?

image

Answer / Insight:
Discount percentage has a clear impact on spending across categories.
In most categories, the highest spending occurs at low discount levels, while higher discount levels are associated with lower purchase amounts.

Electronics, Home & Garden, and Sports consistently show higher spending compared to other categories.


9. City and Product Category Heatmap

What was done:
A heatmap was created to examine the average Total_Amount for each combination of city and product category.

Question:
How does the average purchase amount vary by city and product category?

image

Answer / Insight:
Electronics has the highest average Total_Amount across almost all cities.
Home & Garden and Sports also show relatively high values compared to Books, Food, Beauty, and Toys.

Some cities, such as Antalya, Bursa, Izmir, and Kayseri, show high average values for Electronics.
This provides useful business insight for targeted marketing, inventory planning, and city-based product promotions.


10. High Purchase Rate by Product Category

What was done:
An interactive chart was created to show the percentage of High Purchase transactions within each product category.

Question:
Which product categories are most likely to generate high-value purchases?

High Purchase Rate Product Category

Answer / Insight:
Product categories with higher High Purchase Rates are more strongly associated with expensive transactions.
This analysis helps identify which categories are most important for revenue growth, marketing campaigns, and inventory planning.


11. Advanced Business Opportunity Dashboard

What was done:
An interactive dashboard was created to evaluate city-category combinations using multiple business metrics.

Question:
Which city and product category combinations represent the best business opportunities?

Business Opportunity Dashboard

Answer / Insight:
The dashboard combines:

  • Average purchase amount
  • High purchase rate
  • Total revenue
  • Transaction volume
  • Business score

Combinations in the upper-right area represent the strongest opportunities because they have both high average spending and a high percentage of high-value purchases.
This can support marketing strategy, inventory planning, and business growth decisions.


Feature Engineering

In this stage, new features were created in order to improve the dataset and help the machine learning models identify stronger patterns.

The feature engineering process included several main steps:

1. Transaction-Based Features

New features were created from existing transaction columns in order to better describe the actual purchase value.

Examples of created features:

Feature Purpose
Total_Before_Discount Represents the total price before applying a discount
Discounted_Unit_Price Represents the unit price after discount
Discount_Percentage Converts the discount amount into a percentage
Estimated_Final_Amount Estimates the final amount after discount

These features help describe the relationship between price, quantity, discount, and final spending.


2. Interaction Features

Some new features were created by combining categorical variables together.

Examples:

Feature Meaning
City_Category Combines city and product category
City_Payment_Method Combines city and payment method
Category_Device_Type Combines product category and device type
City_Discount_Level Combines city and discount level

These features help the model capture patterns that may not be visible when each column is analyzed separately.

For example, a specific product category may perform better in one city than in another.


3. Encoding Categorical Variables

Machine learning models cannot directly work with text values such as city names, gender, or product categories.

Therefore, categorical variables were converted into numerical format using One-Hot Encoding.

This allowed the models to use categorical information during training.


4. Time-Based Features

The Date column was converted into a datetime format.

From this column, time-based features such as month and day of the week were considered in order to check whether purchase behavior changes over time.


5. Numerical Interaction Features

Interaction features were also created from numerical columns such as:

  • Unit_Price
  • Quantity
  • Discount_Percentage

These interactions help the model understand combined effects between numerical variables.

For example:

Interaction Meaning
Unit_Price × Quantity Shows the combined effect of price and quantity on total spending
Unit_Price × Discount_Percentage Shows how discount changes the effect of price
Quantity × Discount_Percentage Shows how discount changes the effect of quantity

6. Scaling and PCA

After creating new features, the numerical values had very different ranges.
For example, Unit_Price and Total_Amount can have much larger values than features such as Quantity or Discount_Percentage.

To solve this, StandardScaler was applied.
Scaling makes the numerical features more balanced and helps some machine learning models perform better.

After scaling, PCA was applied to reduce the number of features while keeping 95% of the important information in the data.

This helped reduce complexity and improve the efficiency of the modeling process.


Clustering Analysis

K-Means clustering was used to group transactions into different purchasing behavior segments.

The clustering was based on transaction-related features:

  • Unit_Price
  • Quantity
  • Discount_Percentage
  • Total_Before_Discount

The target variable, Total_Amount, was not used in the clustering process in order to avoid target leakage.

Clustering Results

The K-Means model created 3 clusters:

Cluster Number of Transactions Interpretation
Cluster 0 10,712 Regular purchases with lower prices and moderate purchase values
Cluster 1 3,661 High-value purchases with higher unit prices and higher total amounts before discount
Cluster 2 2,676 Discount-driven purchases with higher quantities and higher discount percentages

Cluster-Based Features

After creating the clusters, three new features were added to the dataset:

Feature Meaning
Purchase_Cluster The cluster assigned to each transaction
Distance_To_Cluster_Center How far the transaction is from the center of its cluster
Cluster_Confidence How strongly the transaction belongs to its assigned cluster

Main Insight

The clustering analysis helped identify three main types of purchasing behavior:

  1. Regular purchases
  2. High-value purchases
  3. Discount-driven purchases

These cluster-based features were later added to the modeling stage to help the models better understand different transaction patterns.


Baseline Regression Model

A Linear Regression model was used as the baseline model.

Regression goal

The goal was to predict the numerical value of Total_Amount using customer, product, and transaction-related features.

Selected features included:

  • Unit_Price
  • Quantity
  • Product_Category
  • Discount_Percentage
  • City
  • Payment_Method
  • Device_Type
  • Is_Returning_Customer
  • Customer_Rating
  • Delivery_Time_Days
  • Engineered interaction features

Train-test split

The dataset was split into:

  • 80% training data
  • 20% testing data

A fixed random_state=42 was used for reproducibility.

Baseline model evaluation

Metric Value
MAE 165.91
MSE 63,466.31
RMSE 251.93
0.9307

Insight

The baseline model performed strongly, explaining about 93% of the variation in Total_Amount.
However, the high score should be interpreted carefully because some engineered features are closely related to the target variable, which may create a risk of data leakage.


Improved Regression Models

Three improved regression models were trained using the engineered dataset.

Models trained:

  1. Improved Linear Regression
  2. Random Forest Regressor
  3. Gradient Boosting Regressor

Model comparison:

Model MAE MSE RMSE
Baseline Linear Regression 544.68 1,138,333.00 1,066.93 0.9310
Improved Linear Regression 160.07 50,623.41 225.00 0.9447
Random Forest Regressor 21.80 9,397.69 96.94 0.9897
Gradient Boosting Regressor 30.75 9,134.01 95.57 0.9900

image

Regression winner

The Random Forest Regressor was selected as the final regression model.

Although Gradient Boosting achieved a slightly higher R² and slightly lower RMSE, Random Forest achieved the lowest MAE and showed very strong overall performance.
It was selected because it can capture complex, non-linear relationships while producing stable predictions through multiple decision trees.

The winning regression model was exported as:

winning_model.pkl

Regression-to-Classification

After completing the regression task, the original numerical target Total_Amount was converted into classification labels.

Classification target

Total_Amount was converted into three classes:

  • Low Purchase
  • Medium Purchase
  • High Purchase

Method used

Quantile binning was used to create relatively balanced classes.
The thresholds were calculated using only the training data to avoid data leakage and then applied to both training and testing sets.

Class balance:

Class Train Count Train Percentage Test Count Test Percentage
Low Purchase 4,547 33.34% 1,133 33.23%
Medium Purchase 4,546 33.33% 1,154 33.84%
High Purchase 4,546 33.33% 1,123 32.93%

image

Main Insight

The classes are balanced because quantile binning was used.
This makes accuracy useful, but macro F1-score was also used because it evaluates all classes equally.


Classification Models

Three classification models were trained to predict whether a purchase belongs to the Low, Medium, or High class.

Models trained:

  1. Logistic Regression
  2. Random Forest Classifier
  3. Gradient Boosting Classifier

Evaluation priorities

For this classification task, recall is especially important for the High Purchase class.
Missing a high-value transaction can be more harmful than incorrectly labeling a transaction as high-value.

Therefore, the evaluation focused on:

  • Accuracy
  • Precision Macro
  • Recall Macro
  • F1 Macro
  • Confusion matrices
  • Classification reports

Classification model comparison:

Model Accuracy Precision Macro Recall Macro F1 Macro
Gradient Boosting Classifier 0.9947 0.9947 0.9948 0.9947
Random Forest Classifier 0.9947 0.9947 0.9948 0.9947
Logistic Regression 0.9633 0.9634 0.9635 0.9634

image

Full Classification Output

The full classification output, including the detailed comparison table, classification reports, and confusion matrices, can be viewed in the Colab notebook.

Open the full classification output in Colab

Classification Reports

The classification reports show that Random Forest and Gradient Boosting achieved almost perfect results across all purchase classes.
Logistic Regression also performed well, but made more mistakes compared to the tree-based models.

image

Confusion Matrices

The confusion matrices show that most predictions were correct for all three models.
Gradient Boosting and Random Forest made fewer classification errors, especially for the High Purchase class.

image

Classification winner

The Gradient Boosting Classifier was selected as the best classification model.

It achieved the strongest macro F1-score while also maintaining very high accuracy, precision, and recall.

The winning classification model was exported as:

winning_classification_model.pkl

Key Decisions Made

During the project, the following decisions were made:

  • Total_Amount was selected as the main regression target.
  • Discount_Percentage was created to compare discounts across products more meaningfully.
  • Missing values and duplicates were checked, and no major data quality issues were found.
  • Categorical values were checked and found to be consistent.
  • Strong outliers were capped using the IQR method instead of fully removing them.
  • One-hot encoding was applied to categorical variables.
  • Interaction features were created based on EDA insights.
  • K-Means clustering was used to create purchase behavior segments.
  • PCA was applied to reduce dimensionality while preserving 95% of explained variance.
  • Quantile binning was used to convert Total_Amount into Low, Medium, and High purchase classes.
  • Macro F1-score was used as an important classification metric because the classification task includes three classes.
  • Random Forest Regressor was selected as the final regression model.
  • Gradient Boosting Classifier was selected as the final classification model.
  • The possibility of data leakage was noted because some engineered features are closely related to the target variable.

Main Findings

The analysis shows that customer spending behavior is influenced by a combination of product, transaction, discount, and behavioral factors.

Key findings:

  • Product Category: Electronics produces the highest average spending, while Books produces the lowest. Product category is one of the strongest factors affecting purchase value.

  • Unit Price and Quantity: These are the strongest direct drivers of Total_Amount. Higher price and higher quantity lead to higher total spending.

  • Discount Percentage: Higher discount percentages are associated with lower total spending. This may suggest that larger discounts are more common in lower-value purchases.

  • City: City has a moderate influence. Istanbul contributes the most total spending, but no single city dominates the entire dataset.

  • Gender: Spending patterns are very similar across genders, suggesting that gender has a weak effect on total spending.

  • Age: Age does not strongly influence total spending. Spending is relatively similar across age groups.

  • Payment Method and Device Type: These features show slight differences in spending behavior. Bank Transfer and Digital Wallet are associated with higher spending in some device groups.

  • City and Product Category Together: Electronics performs strongly across many cities, while Home & Garden and Sports also show strong performance in specific locations.

  • Clustering: K-Means identified three meaningful purchase behavior groups: regular purchases, high-value purchases, and discount-driven purchases.

  • Regression Modeling: Tree-based models performed better than linear regression because they capture complex, non-linear relationships.

  • Classification Modeling: Gradient Boosting and Random Forest performed extremely well in predicting Low, Medium, and High purchase classes.


Conclusion

This project demonstrates how e-commerce customer spending can be analyzed using EDA, feature engineering, clustering, regression, and classification.

The EDA showed that spending behavior is mainly influenced by product and transaction-related factors, especially product category, unit price, quantity, and discount percentage. Customer demographic variables such as age and gender had weaker effects.

Feature engineering significantly improved the dataset by creating transaction-based features, interaction features, clustering-based features, and numerical interaction terms. These features helped the models capture deeper purchasing patterns.

For regression, the Random Forest Regressor was selected as the final model because it achieved the lowest MAE and strong overall predictive performance. For classification, the Gradient Boosting Classifier was selected as the best model because it achieved the highest macro F1-score and very strong performance across all classes.

Overall, the project shows that customer purchase value is shaped by multiple interacting factors. The dataset provides valuable insights for business decision-making, targeted marketing, inventory planning, customer segmentation, and predictive modeling.

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support