#Categories

  • Sheikh Aman

How linear regression works? | Easy explanation with code.

Updated: Aug 10


Introduction to linear regression

Machine Learning, Artificial Intelligence, Deep learning has become a trending topic in today's world. Every sector is trying to implement AI in their work to get accurate and fast results.

Applications like predicting the weather, Finding capable employees by feeding resume details as input to AI application. Carbon Monoxide prediction before launching any model by a car company. And many more. Behind every successful Artificial Intelligence application, there is a highly trained and modelled Machine Learning algorithms. Those who wanna make their hand dirty with machine learning they generally start with an algorithm called Linear Regression algorithm. So, what are we waiting for? let's jump together.



Introduction to Linear Regression | What is Linear Regression?


Simple Linear regression is a type of regression technique, where there is a relationship of independent variables with the dependent variables. And the relationship is shown with the sloped straight line, that's why it is called Linear regression. The red straight line is the best fit for the given dataset.


Simple Linear regression graph

Terminologies of Linear regression


Cost Function of linear regression

The linear equation on which the best-fit line is based is:

y = a + bx + c

y = dependent variable
a = y intercept
b = slope
x = independent variable
c = error

The best and possible values of "a" and "b" are provided by cost function to make the best line fit for our data points. This is done by converting this to a minimization problem to get our best values for "a" and "b". Here, the error is minimized between actual and predicted value.

So, the function is chosen below for minimizing the error. It is also called MSE (Mean Squared Error).



Gradient Descent

It is generally a method for updating "a" and "b" to reduce MSE. Partial derivatives are taken with respect to "a" and "b" to find these gradients and then these partial derivatives are used to update "a" and "b". Smaller Learning rate goes closer to minimum value but it takes in the case of the larger learning rate. Here, the time taken is less but there is a chance of overshooting minimum value.



Code snippets of linear regression


Data preprocessing

At first, we have to import all the necessary libraries.

Then we have to load our dataset using the Pandas library.

Now a good practice, always check your data before going further by using head( ) function.


converting to the array and reshaping it.



Extraction of dependent and independent variables.

We have discussed before that linear regression is a relationship between independent and dependent variables.

Splitting the dataset into training and test set

Here, we have to import the train_test_split class of model_selection library from scikit learn.


Fitting the Simple Linear Regression model, Predicting, and getting accuracy.

In this step, we will import LinearRegression class of linear_model from scikit learn. Then we will create an object called regressor. After that, we will predict using predict( ). At last, we will find out the accuracy by r2_score class of metrics from sklearn.


Plotting the linear regression line | Visualization.

With the help of matplotlib.pyplot visualization is done.

Linear regression in python | CODE



Here, we are predicting a demo dataset of employee's salary prediction. It has only 3 columns ID, Experience, and salary. To get The dataset just click me it is totally free.

Conclusion


You have learned 90% of Linear regression during this time session and the remaining 10%

will be completed if you practice on your own. You learned an algorithm than every machine Learning beginner should know. This algorithm is very useful and known for its simplicity.

If you really enjoyed this article then share and like it.

Something Interesting

#Some_Interesting_topics

MR. MACHINE

Subscribe to Our Newsletter
Copyright © 2020 MR. Machine. All Rights Reserved
  • Facebook
  • Twitter
  • Pinterest
  • Instagram