Real Time Prediction: Is the Online Shopper Browsing or Purchasing?
By: Elva Xiao
Tags: Machine Learning, Classification, Ecommerce

When a user enters an ecommerce website, is he or she just browsing or ultimately will buy something? We can use classification algorithms in machine learning to find and keep the best customers.

Photo by Carlos Muza on Unsplash

As we all know, ecommerce is booming. Large platforms such as Amazon are becoming the primary place for people to find, compare and ultimately purchase products. Especially during the pandemic, more consumers have begun shopping online in greater numbers and frequency. This change in buying behavior will remain even after the COVID.

For the ecommerce platform, one of the most important questions they want to know is, whether the customer is just browsing or actually buying. Why is it important? Because customers are heterogeneous and you don’t want to treat them in the same way because you always want to leverage your limited resources to find and keep the best customers. If you have increased confidence that a subset of users are more likely to purchase, you can take proactive actions, such as offering time-limited coupons, or even one-month free trial like what Amazon is doing, to push the users to complete their purchases. At the end of the day, this will help the sellers target the right customers, improve retention and ultimately increase sales and profits.

Data Understanding

The dataset is from UCI Machine Learning Repository and it was collected from an online bookstore. This dataset includes 12,300 instances and each instance is a session that belongs to a different user in a 1-year period. It consists of 10 numerical and 8 categorical attributes.

Here in our case, we want to make prediction at the time when the user enters the platform. To prevent data leakage, we exclude attributes related to clickstream data. We choose these nine attributes to further build the model. The revenue attribute can be used as the class label and it can be only TRUE or FALSE. TRUE means the user purchased and finalized with transaction.

Feature Description

Data at a glance

Out of 12,300 instances, 85% of the users didn’t make a purchase and only 15% of them did. The dataset is imbalanced and we may want to be careful about this because it will affect how we evaluate the performance of the models.

Modeling

Obviously, this is a binary classification problem and we will use these four classification techniques: Decision Tree, k-NN, Logistic Regression and Naïve Bayes. We will build the models using Python.

Model Evaluation

Generalization Performance of Classifiers

If we just look at the accuracy, logistic regression will be the best. However, as we mentioned before, we have a imbalanced dataset: only 15% of users turned out to make a purchase. These classifiers just tend to classify all the users as not purchasing customers, because that’s how the models maximize the accuracy. In our case, accuracy is not an appropriate metric to evaluate the performance and we care more about identifying the purchasing customers and making profits from them. Therefore, recall will be a more suitable metric and we can see that Naive Bayes performs much better than the others.

Handle Imbalanced Classes with SMOTE

In order to further improve the generalization performance, I will rebalance the data before building the model. There are two popular ways to resolve the this challenge: undersampling and oversampling.

Undersampling is eliminating majority class examples and it is done until the majority and minority class instances are balanced out. Oversampling increases the number of instances in the minority class by replicating them in order to present a higher representation in the dataset.

Synthetic Minority Over-Sampling Technique (SMOTE) is an oversampling technique. It works by randomly picking a data point from the minority class, getting the k-nearest neighbors for this point and synthesizing a new instance.

Original Distribution of Target Variable

It is very easy to use SMOTE algorithm to rebalance the data in Python. After doing so, now we have 10,422 instances for both classes.

# import library

from imblearn.over_sampling import SMOTE smote = SMOTE() # fit predictor and target variable

X_smote, y_smote = smote.fit_resample(X, y)

Distribution of Target Variable (After SMOTE)

Generalization Performance of Classifiers After SMOTE

As I mentioned before, identifying the buying visits is more important, which means we want to maximize the recall rate for the positive class. In this case, Naive Bayes outperforms the other models. Meanwhile, this model is very efficient in terms of both storage space and computation time. Therefore, it fits well in our real-time prediction scenario. Naive Bayes is also an incremental learner and it is able to incrementally utilize additional information or evidence, without re-estimating the whole model from scratch.

We should notice that Naive Bayes makes the assumption of probabilistic independence. If this assumption fails, we don’t want to rely on this model to get the exact estimation of the class probability since it is biased and overestimated. But we can still use it to generate good classification decision. If we do care about the accuracy of probability estimation, such as making decision with cost and benefit, Logistic Regression will be more reliable.

Deployment Plan

Obviously, the ecommerce platforms can use the Naive Bayes to predict whether a customer is just browsing or purchasing in real time. If the platform knows which users are more likely to buy, it can allocate more resources on them, such as customized marketing contents and other good offerings.

Looking forward, we also have two more suggestions. First, the platform can do A/B testing to find out what is the optimal discount rate to attract and retain customers. Second, we know that nowadays, it is easier to collect users’ clickstream data, such as how long a user stays on a specific page and whether he or she clicks a specific button. We think these data can be also very useful to predict customers’ purchasing behavior. One use case we come up with is, if a customer didn’t purchase at the first stage, but after observing his or her clickstream data and we think he has a high probability to make a purchase, we can offer him a more generous offer such as by email to push him or her to complete the transaction.