Downloads · 30 days
0
galsolomon9/Retail-revenue-prediction
Retail-revenue-prediction is a machine learning model from galsolomon9. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
This project focuses on analyzing online retail transactions to understand what truly drives the total revenue of a purchase and how accurately this value can be predicted using machine learning. Later in the project,…
Downloads · 30 days
0
Access
Public
Updated Dec 10, 2025
Repo size
118 MB
Likes
0
Public
Click a slice to open those files.
.csv94.9 MB · 79%
From the Hugging Face model README
This project focuses on analyzing online retail transactions to understand what truly drives the total revenue of a purchase and how accurately this value can be predicted using machine learning.
Later in the project, the problem is also reframed as a classification task in order to separate high-revenue and low-revenue transactions.
The dataset includes information such as quantity, unit price, country and customer ID.
Before moving to the modeling stage, I performed a full data cleaning process.
I removed rows with negative values for quantity or price, dropped entries where the customer ID was missing, fixed date formatting issues and eliminated duplicated transactions.
After this process, a clean dataset of a little over sixty thousand transactions remained.
I created the main target variable of the project, TotalRevenue, by multiplying quantity by price.
This value became the central measure for both regression and classification tasks.
Boxplots were used to visualize extreme values in Quantity and Price before filtering.
This helped identify unrealistic transactions that could distort the regression model.

The first stage of the EDA showed that most transactions produce relatively low revenue, while high-revenue cases are much less common.

When examining the relationship between quantity and revenue, the pattern was not linear.
A wide spread of values exists even for similar quantities.

The same behavior appears when looking at price versus revenue.
The diagonal lines reflect the mathematical relationship
TotalRevenue = Quantity × Price,
not a behavioral buying pattern.

When grouping revenue by country, the United Kingdom clearly dominates the dataset.
Most of the total revenue comes from the UK, while all other countries contribute only a small fraction.
This immediately suggested that country is an important predictive feature.

To establish a reference point, I trained a simple Linear Regression model using the basic features: quantity, price and country.
The performance was modest.
MAE ≈ 4.97
RMSE ≈ 6.43
R² ≈ 0.34
The model explained only a small portion of the variance in revenue.
This result indicated that the revenue pattern in the dataset is more complex than a simple linear relationship.
To better understand what drives this baseline model, I plotted the linear regression coefficients.
Price has the strongest positive impact on revenue, while several country dummy variables have negative coefficients,
showing that transactions from those regions tend to generate lower revenue compared to the baseline.

To address these limitations, I expanded the feature space.
I applied logarithmic transformations to quantity and price in order to reduce extreme values and stabilize the relationships between variables.
I then standardized the numeric columns so that all features would share a similar scale.
In addition, I used K-Means clustering on the standardized quantity and price to identify groups of transactions with similar purchasing patterns.
I added both the cluster label and the distance to the cluster center as new features.
A PCA visualization confirmed that the clusters were clearly separated and carried meaningful structure.

After retraining the linear regression model with the engineered features, the improvement was significant.
The R² value increased sharply and the error rates dropped.
This showed that the additional structural information captured by the clustering and log features addressed the core limitations of the baseline model.
After improving the feature space, I trained more powerful non-linear models, including:
Random Forest Regressor
Gradient Boosting Regressor
These models captured the non-linear relationships in the data extremely well.
Among all tested models, the Random Forest Regressor delivered the strongest performance.
Feature importance analysis showed that the most influential variables were:
Quantity
Log_Quantity
Price
Log_Price
Cluster related features

(https://huggingface.co/galsolomon9/Retail-revenue-prediction/resolve/main/winning_model%20(1).pkl)
This pickle file contains the full trained regression pipeline, including feature engineering and model parameters.
In the second part of the project, I transformed the task into a binary classification problem.
I split the target variable at the median to distinguish between:
Low Revenue
High Revenue
The classes were nearly balanced, so no re-sampling or adjustment was required.
I trained three classification models:
Logistic Regression
KNN
Random Forest Classifier

The Random Forest Classifier achieved the best performance with very high accuracy and near-perfect separation between the two classes.


https://huggingface.co/galsolomon9/Retail-revenue-prediction/resolve/main/classification_model.pkl
This pickle file contains the trained classification pipeline and can be used to classify new transactions into low or high revenue categories.
In this setting, identifying high-revenue transactions is more important than avoiding false positives.
From a business perspective, missing a truly high-value transaction represents a real financial loss.
Incorrectly classifying a low-revenue transaction as high mainly wastes attention or resources.
For that reason, recall is the more critical metric than precision in this task.
The project shows that TotalRevenue depends on a combination of price, quantity, country and underlying purchase behavior that becomes visible through clustering.
The relationships in the data are not linear, which explains why tree-based models such as Random Forest outperform linear methods.
Feature engineering, especially the use of log transformations and clustering, dramatically improved model performance.
The classification stage demonstrated that transactions can be reliably separated into high- and low-revenue groups with near-perfect accuracy.
Overall, this project highlights the importance of understanding the structure of transaction data and using the right combination of preprocessing, engineered features and suitable models to achieve strong predictive performance.