Linear Regression in Python: Predict e-Commerce Revenue
By: Ayça Erbaşı
Tags: Machine Learning, Python, Linear Regression Python, Linear Regression, Data Science

Photo by ev on Unsplash

In this post, the goal is to build a prediction model using Simple Linear Regression and Random Forest in Python. The dataset is available on Kaggle and my codes on my Github account.

Let’s get started to understand the dataset.

Discover and Visualize Data

In this dataset, there are 3 categorical features Email, Address, and Avatar; 5 numeric features Avg. Session Length, Time on App, Time on Website, Length of Membership, and Yearly Amount Spent. And there are 500 instances. That is small but perfect for beginners.

Figure 1: First 10 columns of the data frame.

Here is the summary of statistics pertaining to the columns.

Figure 2: Summary of Statistics.

Attribute Histogram Plots created for a visual interpretation of numerical data.

Figure 3: Attribute Histogram Plots.

Looking for Correlations

Let’s glance at how much the ‘Yearly Amount Spent’ correlates with other attributes.

The output of the correlation.

The attribute of ‘Length of Membership’ seems most correlated with ‘Yearly Amont Spent’. And ‘Yearly Amont Spent’ is negatively correlated with ‘Time on Website’. So, it shows that customers who stay on the website longer spend less money.

Let’s check the correlation using a scatter matrix.

Figure 4: Scatter Matrix Plot.

As a result, the revenue is most relevant with the length of membership and the time on app is the second attribute where the correlation is high. Let’s compare time on app and time on web. We can see which group the customers who spend the most money belong to in Figure 5.

The red dots represent a higher amount of spent and the blue ones are for lower amounts, the width of dots represents the length of membership. When the width of the points increases, it means the length of membership is longer. (Multiplying by 10 doesn’t matter, it just made the dots bigger on the graph.) Figure 5 tells us customer using app spends more money. And long-time members can be interpreted that they prefer to use app.

Figure 5

Linear Regression

Linear Regression is a Supervised Learning Technique. Regression is the process of predicting a continuous value. It’s done by training the model and the model predicts future instances using previously labeled data. X represents the independent variables, y represents the dependent variable it is also called the target and this variable will be tried to be predicted.

The key point in the regression is that the dependent value should be continuous.

Linear Regression Formula:

Theta0 and theta1 are also coefficients of the linear equation, y hat is the predicted variable.

Train the Model

Most machine learning algorithms can’t work with null values. This dataset has been checked, there is no missing value.

I defined the X and y values. I have chosen only numerical attributes for X.

Here is the model evaluation output:

These coefficients are for these features respectively Avg. Session Length, Time on App, Time on Website, Length of Membership that is what we define X.

R-squared shows how the distance of data from the fit line. The best possible score is 1. The proximity of the r-squared value to 1 indicates the suitability of the model. For our model, it’s 0.986.

RMSE measures the distance between the vector of prediction and the vector of target values. It’s 9.755 for our model.

Random Forest

Let’s evaluate the model with the Random Forest Regression.

The output of Random Forest Regression.

Linear regression gave better results for this dataset.

References

Please don’t hesitate to give me feedback.

Thank you for reading.