E-Commerce (Pakistan): Making Predictions using Machine Learning
Background
To understand trends in Pakistan’s E-Commerce industry, we did some research on a publicly available dataset as part of our course, ‘CS334: Principles and Techniques of Data Science’ at the Lahore University of Management Sciences (LUMS). We chose Zeeshan Usmani’s renowned dataset titled ‘Pakistan’s Largest E-Commerce Dataset’ for our project.Our goals included identifying top-selling categories, identifying sales trends, etc. We started by cleaning the dataset. This included removing any unwanted columns, aggregating label names, etc. We then proceeded with Exploratory Data Analysis (EDA) on the cleaned dataset. Using python’s built-in libraries, such as NumPy, pandas, Seaborn, and matplotlib, we visualized our data to get an in-depth view of all the categories and to proceed further with our research.
For an in-depth look into our data cleaning and EDA process, you can read the following article:
E-Commerce (Pakistan): Exploratory Data Analysis
Our research continues and has led us to apply machine learning to predict the following outcomes (based on our dataset):
● Can we accurately predict refunds by training our model on refunded items in the dataset based on variables like item category, price, etc.?
● What payment options do consumers prefer based on variables like item category, price, etc.?
Predicting Refunds using Logistic Regression
To go about this, we started by calculating the percentage of refunded and non-refunded items in the dataset. We also grouped unit price, quantity, total price, discount, and year based on refunds and calculated their means.
Our data showed that ~11.5% of all orders are refunded. A quick glance at the means indicates that there may be a correlation associated with our variables on the item being refunded. This was very exciting as this opened a door for us to try and predict refunds with a mathematical model. Exciting, right? However, the data is not ideal by any means. There is a significant class imbalance for classifying orders as there are many more non-refunded orders than refunded ones. Nevertheless, our curiosity led us to continue and explore this further.
Pre-processing the data
To try and address the issue of class imbalance, we moved on to oversample the data set using the SMOTE algorithm. You can read more about the SMOTE algorithm at this link, but in short, it helps with oversampling, giving us synthetic data points to work with to cater to class imbalance.
Get Armishjaved’s stories in your inbox
Join Medium for free to get updates from this writer.
Moving further, we one-hot encoded the categories and payment modes so that we could train models on the dataset using logistic regression. In layman’s terms, one-hot encoding is the process of classifying the data in a binary manner to allow for easier decision-making for the model as it must choose between a ‘1’ or a ‘0’. A snapshot of our encoded variables is illustrated below:
This raises the question of how we dealt with the price. The price may be 1,000 for a particular item and 10,000 for another. How can we use a ‘1’ or a ‘0’ to represent this? Well, we catered this for two of our variables, total price and discount, by taking the mean of the values in the dataset and assigning a ‘1’ or a ‘0’ if the prices are above or below the mean, respectively. A snapshot of this is shown below, along with some other variables we incorporated into our final dataset:
We trained the model using logistic regression on the finalized data set. The accuracy of our model can be seen below:
Given that we could have used better techniques to cater to the price columns, along with carrying out over-sampling of the dataset, a 70% accuracy was enough to get us excited. The confusion matrix, which shows segmentations based on actual and predicted values of our data, can be seen below:
We can interpret from the matrix above that the ratio of False Negatives is much less than False Positives, i.e., our model usually predicts ‘refunded’ for many items that were actually not refunded.
According to the above Receiver Operating Characteristic (ROC) curve, it can be said that the model will generally make accurate predictions, and it is far from a 50–50 situation. However, it’s not very close to the baseline and is rising toward a higher True Positive Rate.
For this part of the research, it would be safe to say that our model works well mostly, with a 0.70 accuracy value. We could continue working on it to train a model that provides higher accuracy by exploring better data sampling and classification techniques.
Predicting Choice of Payment Methods using Decision Trees
To expand on this, we used Decision Trees to train our model and get predictions. Just like in the previous part, we one-hot encoded the categories and prices to be able to train the model. A snapshot is shown below:
The accuracy of our model came out to be 0.68, which, considering the size of the dataset, and our data-cleaning techniques, is not bad.
Using the tree library from sklearn, we made our decision tree and visualized it using graphviz. A close-up snapshot can be seen below:
If we look at the terminal nodes (may not be clearly visible in the image above), we can see that most of the purchases are made by cash on delivery (COD). COD is definitely the first choice of many Pakistanis, as we already built upon in our EDA.
Conclusion
We can conclude our research here based on the questions we posed above with the models we created. The regression and the Decision Tree models have provided us with an acceptable baseline accuracy, but it may be improved. The graphs and models have helped us better understand buyers’ sentiments in Pakistan, specifically regarding refund patterns and payment modes. These areas are fast-growing, and it is imperative more data is collected in this domain so that more research may be carried out to help businesses make better decisions.
Future Research
The research can be expanded further, first by using the same questions and improving the accuracy. Furthermore, we can use these models and the dataset to explore more questions to build more context into Pakistan’s E-Commerce industry.





