Math Behind Simple Linear Regression + Scikit Learn
Tags: ai, machinelearning, python, jupyter
Published: 2021-08-25 19:03:08

Linear Regression is basically a used type of predictive analysis and one of the most simple algorithms in machine learning. It attempts to measure the relationship between variables by fitting a linear equation to observed data. For example, when the mobile phone's age increases, the price will go down. So, one variable is an explanatory variable (Age). Or otherwise, we can say it's an independent variable. And the other one is considered to be the dependent variable (Price).

From that example, we can say the future price of the mobile phone using that observed data. Here is a table of the example data.

| Mobile Phone Age (Years)      | Price ($) |
| ----------- | ----------- |
| 1      | 250       |
| 2   | 230        |
| 5   | 190        |
| 6   | 160        |
| 8   | 120        |
| 9   | 90        |
| 10   | 70        |
| 11   | 40        |


In this case, we see that a **negative relationship** between mobile phone age and price. Why do I say that, when the mobile phone's age increases, the price will decrease.

Another example is when experience increases, so do the salary. It's a **positive relationship**.

We're trying to predict the mobile phone's future prices given the age like this.

![simple linear](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kmyisym91nacair8y7lg.png)

The question is **what is the price after 7 years?**. Let's put a point there to see how much it is.

![predicted chart](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4faun2p68nlqyn7g0oda.png)


It's a little bit lower than one hundred and fifty usd. So, Now see the mathematical side behind simple linear regression. The formula is `y = mx + b`. I know you're a little bit familiar with this formula. because mostly we all learned this in school.

- `y` - What we are going to predict. In this case, mobile phone price (dependent variable)
- `m` - Slope or constant
- `x` - Input as 7 years (independent variable)
- `b` - Intercept

And `m` and `b` are given by the following formula.

![formula m](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8taq0ieak2teu3ow1blb.png)

![formula b](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lwhpwg5dxib07z3oqtmm.png)


Find the linear regression equation for that mobile phone price data set.

| x (Years)      | y (Price) | x<sup>2</sup> |  xy |
| ----------- | ----------- | ----------- |----------- |
| 1      | 250       | 1 | 250 |
| 2   | 230        | 4 | 460 |
| 5   | 190        | 25 | 950 |
| 6   | 160        | 36 | 960 |
| 8   | 120        | 64 | 960 |
| 9   | 90        | 81 | 810 |
| 10   | 70        | 100 | 700 |
| 11   | 40        | 121 | 440 |
| (∑x) = 52 | (∑y) = 1,150 | (∑x<sup>2</sup>) = 432 | (∑xy) = 5,530 |


Okay, now we can assign those values to that formulas and get the value of `m` and `b`.

**Find `m` - Slope**
![find m](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3w4q58j3no3zm5e6s01d.png)

**Find `b` - Intercept**
![find b](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gb7n26334wzomg5929a7.png)

Predict the mobile phone price after 7 years. using `y = mx + b`. The `y` is the price of the mobile phone after 7 years (that we're going to predict). `x` is 7 years.

![predicted 7 years price](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qitrebx0lqwzzmec1sgt.png)

The mobile phone price after 7 years is **133.40 usd**. Now do the same thing with ***scikit learn linear regression*** model using Python.


&nbsp;

## Linear Regression Model (Scikit Learn)

First, We have to save that data set into a csv file. To do that, create a new csv file as `mobiledata.csv` and add those data like this.
![csv file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d7oae1rs5218o69ovla7.png)

Let's code it! I'm using `Jupyter Notebook`. You can use any Python IDE as you prefer. Next, Install the libraries that we need. *(If you are using `Jupyter Notebook`, add an exclamation mark before the command to act as if it is executed in the terminal)*

```
!pip install scikit-learn
!pip install numpy
!pip install pandas
!pip install matplotlib
```

Import those libraries

```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
```
Read the `mobiledata.csv` file using `pandas`

```python
data_set = pd.read_csv('mobiledata.csv')
```

Create a chart and put the points there

```python
plt.scatter(data.age, data.price, color='red')
plt.xlabel('Mobile phone Age')
plt.ylabel('Price')
```
You can see the chart like this.
![matplotlib chart](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sfjyfrtgxoh45zhitv4e.png)

Get the age values as `x` and price values as `y`. We need to convert those values to a `numpy` array.

```python
x = np.array(data.age.values)
y = np.array(data.price.values)
```

Create a linear regression class object and train the model using the `fit` function. Also, the `model.fit` function allows a two-dimensional array to `x` position.

```python
model = LinearRegression()
model.fit(x.reshape((-1,1)), y)
# x.reshape((-1,1) is convert numpy array to two dimensional array
```

We can find the best fit line for this data set if we want. And get the values of `m` (Slope) and `b` (Intercept).

```python
plt.scatter(data.age, data.price, color='red')
plt.xlabel('Mobile phone Age')
plt.ylabel('Price')
m,b = np.polyfit(x,y,1)
plt.plot(x,m*x+b)
```
![best fit line](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gi58kuhm5tqih3439wp.png)

Finally, predict the mobile phone price after 7 years using the model. The model object is defined as `model`. Predict the price to see whether it's equal to the previously calculated value or not. To do that, We need to convert x value (7) to a numpy array and two-dimensional array.

```python
year_seven = np.array([7]).reshape((-1,1))
# Predict the price
model.predict(year_seven)
```

You'll see the price after predict using the model is exactly the same as the previously calculated value that We using the formula.
```python
# array([133.40425532])
```
You can check the values of `m` and `b` by executing the variable in the notebook.

```python
m
# -20.691489361702125
```

```python
b
# 278.2446808510638
```

Happy Coding🎉