Linear Regression using Python

Abhimanyu Dwivedi
Quick Code
Published in
2 min readAug 26, 2019

This blog is about the first machine learning algorithm that I learned, Linear Regression and its implementation. I am not going to the in-depth mathematical implementation of this algorithm.

So let’s get started..

What is Regression?

Regression techniques are used where problems have continuous and real output rather than discrete. For ex. “temperature for last ten days”, “increment in salary for 12 months” etc.

What is Linear Regression?

Linear Regression is the first step to any programmer who wants to get started in machine learning. This algorithm is a linear statistical approach for modelling relationship between a dependent variable and one or more independent variable.

Fig. A sample linear regression output.

Our task here is to find a line which best fits with all the points scatter in graph which can nearly predict the value of any new feature which was not previously present.

Types of Linear Regression

The most common type of linear regression techniques are simple linear regression and multiple linear regression.

In simple linear regression, one feature is used to predict the output whereas in multiple linear regression more than one feature is used to predict the value.

How does this work ?

In both types, the main objective is to form a linear graph, in simple LR one feature (x) is used and in multiple LR more than one features (X) unitedly used to predict one feature.

After the scatter graph is plotted, we find a best fit line which is minimum distance from all the points. The cumulative distance between each point and the line is calculated using any distance formula like euclidean or manhattan distance formula. Then using the training data we find the error in the values. This is the error we wish to minimize to fib the corresponding best fit line.

Implementation using scikit-learn

#importing libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
x = 11 * np.random.random((10,1))
y = a * x + b
y = 1.0 * x + 3.0
# create a linear regression modelmodel = LinearRegression()
model.fit(x,y)
# predict y from the data where the x is predicted from the xx_pred = np.linspace(0, 11, 100)
y_pred = model.predict(x_pred[:, np.newaxis])
# plot the resultsplt.figure(figsize =(3, 5))
ax = plt.axes()
ax.scatter(x,y)
ax.plot(x_pred, y_pred)
ax.set_xlabel('predictors')
ax.set_ylabel('criterion')
ax.axis('tight')
plt.show()
Fig. Output of the above implementation.

--

--

Abhimanyu Dwivedi
Quick Code

Software developer | A constant learner |“The computer was born to solve problems that did not exist before.” Know more@->>http://tinyurl.com/abhi-portfolio