Linear Regression Demystified
By: Gabriel Mayers
Tags: Machine Learning, Linear Regression Python, Machine Learning Ai, Linear Regression, Data Science

In this post, we’ll see the most common Machine Learning Technique and how to apply with simple way to solve problems.

GIF Source here

Suppose you were hired by a big Ecommerce Company to help their decide if, in order to increase your profit, you need more focus on your App or your website.

They provided you a database with informations about their consumers, and now, you need to discover how can you solve their question using your skills in Machine Learning.

Let’s get started!

Warning: I took the data from this Udemy Course, this data isn’t real!

Taking a look from the Dataset

After use Pandas to load our Dataset, we can take a look from the head of our Dataset.

Our Dataset looks like this:

Our Dataset

Now, we can see the types of data that is present in our dataset and we can explore this to find the best Machine Learning Approach to solve our problem.

Discovering the best Machine Learning Approach

It’s simple to see if we can use a Linear Regression Approach or not, we just need to plot our data and find a correlation between X and Y.

Remember, the Ecommerce Company wanna know if they need to focus on their Website or App to increase their revenue, so our Y will be the “Yearly Amount Spent”.

Now, we need the find the X. In Linear Regression, the X represents a variable that directly influences the result of Y, thus establishing a linear regression between X and Y.

For this problem, we can find the X empirically. But first, we need to plot all possible informations who can help us.

All plotted Info

Now, we need the find the correlation between some X and “Yearly Amount Spent”.

We can see this correlation in the “Length of Membership”, like the image below:

Correlation between X and Y

We can plot a Linear Regression graphic to visualize this correlation better:

Linear Regression between X and Y

Now, we discovered the correlation and can apply the Linear Regression!

Applying the Linear Regression

Basically, the Linear Regression is resumed in the formula below:

I’ll write a post approaching the Linear Regression Theory, don’t worry.

For now, you just need to understand the concept behind. Remember, for this problem, we wanna predict what will increase more revenue in the Ecommerce Company.

We found that the “Length of Membership” have a correlation with the “Yearly Amount Spent”, this is our X and Y.

Now, based in this information, we need to predict what will increase more revenue, the Website or the App.

Continuing, let’s apply the Linear Regression!

To build a model and apply Linear Regression easily, we can use the scikit-learn.

Building our Model

Before build our Model, we need to select the data of we’ll use to input into him.

When we’re talking about Machine Learning Models, the universal language to communicate with their is our beloved Math!

So we can delete all the columns where we have strings in our Dataset, like: “Email”, “Address” and “Avatar”. This absolutely doesn’t matter to our model!

Our Dataset after drop unnecessary columns

Now, we need to split our data into Train and Test, to do this, we can use scikit-learn too.

This’s small Dataset, we just have 5000 examples!

In this case, I use to split 90% Train and 10% to Test, but you can test a many ways and find the best approach ;)

For my surprising, the model performed better using 70% to Train, LOL!

After Split our Dataset into Train and Test, we can fit our model passing the X and Y from Train.

Visualizing the Results

After train our model, we can make predictions in our Test data and after, plot the predictions and the real result to compare how our model are performing.

Not bad!

Now, it’s a good practice visualize our errors:

Errors

Remember, this is not real data, so we have a very small number of examples to input in our model and that’s influences directly in the performance!

But now, we need to answer the principal question of this work right?

Answering the Principal Question

To answer the principal question, we just need to visualize the coefficients of our model.

Like the image below:

Linear Regression Result

But looking for this data, what we can interpret about him?

This explanation was taken from this Udemy course:

Keeping all other variables constant, an increase of 1 unit in average usage time is associated with an increase in the total $ 25.98 spent.

Keeping all other variables constant, an increase of 1 unit in the time spent in the application is associated with an increase of US $ 38.59 in the total spent.

Keeping all other variables constant, an increase of 1 unit in time on the website is associated with an increase of US $ 0.19 in dollars.

Keeping all other variables constant, an increase of 1 unit in membership time is associated with an increase of $ 61.27 in dollars.

So, the the Ecommerce company need to focus on their App!

That’s all for today, any question or consideration about this post do not hesitate to comment below, I will do everything to answer you as soon as possible!

You can find the complete files from this project in here.

See you next time!

My Social Medias:

Linkedin: https://www.linkedin.com/in/gabriel-mayer-779b5a162/

GitHub: https://github.com/gabrielmayers

Instagram: https://www.instagram.com/gabrielmayerl/