Machine Learning — Linear Regression: E-Commerce Case

Erwindra Rusli
The Startup
Published in
5 min readAug 10, 2020

--

Disclaimer: This story is just a scenario for Project Practice in Purwadhika School. All the dataset used here is fake, so there is no confidential issue.

One day, I just got some contract work with an E-Commerce company based in New York City that sells clothing online but they also have in-store style and clothing advice sessions. Customers come into the store, have sessions/meetings with a personal stylist, then they can go home and order either on a mobile app or website for the clothes they want.

The company is trying to decide whether to focus their efforts on their mobile app experience or their website. They’ve hired me on contract to help them figure it out! So, I decide to use Machine Learning modeling with the Linear Regression method to get insight into this problem.

First, I need the data for this work. I got an E-Commerce CSV file from the company. It has Customer info, such as E-Mail, Address, and their color Avatar. Then it also has numerical value columns.

  • Avg. Session Length: Average session of in-store style advice sessions.
  • Time on App: Average time spent on App in minutes
  • Time on Website: Average time spent on Website in minutes
  • Length of Membership: How many years the customer has been a member.

After putting the CSV data to the data frame “customers”, I checked the data cleanliness. I didn’t found any anomalies (like a null cell), so we can continue to explore and analyze the data.

I use Seaborn to create a Jointplot to compare the Time on Website and Yearly Amount Spent columns. Then, compare the Time on App and Yearly Amount Spent columns.

Now, I use Seaborn Jointplot to create a 2D hex bin plot comparing Time on App and Length of Membership.

Let’s explore these types of relationships across the entire data set. Use pairplot to recreate the plot below.

Based on this plot we can look that the Length of Membership column is the most correlated feature with Yearly Amount Spent. It makes sense because the longer you become a member the bigger your possibility to spent more money on it. We can see that on the correlation value table also.

Now that I’ve explored the data a bit, let’s go ahead and split the data into training and testing sets. I set a variable “X” equal to the numerical features of the customers and a variable “Y” equal to the Yearly Amount Spent column.

I use model_selection.train_test_split from SKLearn library to split the data into training and testing sets. I set test_size=0.3 and random_state=101. It means I split the existing dataset into 70% for data training, and 30% for data test.

Now its time to train our model on our training data. I need to import LinearRegression from SKLearn.linear_model library. Then, create an instance of a LinearRegression() model named lm. Train/fit lm on the training data. Print out the coefficients of the model.

Now that we have fit our model, let’s evaluate its performance by predicting off the test values. I use lm.predict() to predict off the X_test set of the data. Then, let’s see the result of the real test values versus the predicted values by creating a scatterplot.

It seems that our Model has a good prediction. :)

After making this prediction, I still want to figure out the answer to the original question, do we focus our effort on mobile app or website development? Or maybe that doesn’t even really matter, and Membership Time is what is really important. Let’s see if we can interpret the coefficients at all to get an idea.

And this is what I suggest to the E-Commerce company:

We can see that Length of Membership has a top coefficient value. It means that is the most important variable to get the high customer’s Yearly Amount Spent. Let’s see another coefficient value. Time on App has a bigger value than Time on Website. It indicates us to focus our effort on Mobile App rather than Website Development.
We should make a good UI/UX on the mobile app to make customer feels great during their time on the app. It will lead to the longer Length of Membership and will be converted to the better Yearly Amount Spent by the customer.

You can see the full python script on my GitHub.

Thank You.
See you in another Data Exploration.

BR,
Erwindra Rusli
Data Scientist Student at Purwadhika School.

--

--