Linear Regression the simplest*** way.

Deepanshu Choudhary
Analytics Vidhya
Published in
3 min readJan 2, 2020

What the heck is this Linear Regression?

Linear regression is a linear approach to modeling the relationship between a scalar response (or dependent variable) and one or more explanatory variables (or independent variables).

I know that was rude!

Lets make it easier!

Linear Regression is all about finding a regression line which is a straight line that’s job is to predict the relationship** between two points, also known as a line of best fit.

Let’s make it even more easier!

  • **relationship is the behavior of the features (independent variables) to the dependent variable.
  • y = mx + c, we all studied it in our high school classes, here ‘x’ is the independent variable and ‘y’ is the dependent variable because it depends upon the given value of ‘x’.
  • here ‘m’ is the slope of the line and ‘c’ is the intercept of the same line.

Sounds fair enough?

Why Linear Regression?

Linear Regression’s purpose is to measure to what extent there is a linear relationship between two variables. In particular, the purpose of linear regression is to “predict” the value of the dependent variable based upon the values of one or more independent variables.

Finallyyyy! how to implement the Linear Regression.

First thing first

Import the necessary Machine Learning libraries
Then load the data-set.

Visualizing our data

Now get some insights about the dataset by plotting it, thanks to the Matplotlib library we imported earlier.

The visualization clearly shows the linear nature of the data, which means it will be a wise thing to implement the linear regression on it.

(i) Split the dataset into furthur two sets (training & testing)

Now split the dataset into training and test sets, it is a cumpulsary step as far as machine learning is concerned, it’s the heart of it.

here i divided the dataset into two sets , the ‘test set’ contains the 1/3 no of rows(instances) and rest in the ‘training set’. After the application of model on the ‘training set’ i will check its performance upon the ‘test set’.

(ii) Fit the model to the training set by simply importing it from the sexy ‘scikit-learn’ scientific library’s

linear_model class.
Make a regressor object using the LinearRegression() class.
then fit the model on the training data.

The Conclusion!

Congratulations for making it this far, well it seems our model worked wonderfully well, the blue line fits perfectly and now using its ‘slope’ and ‘intercept’ we can easily predict the salary for the given no of years of experience.

Thanks!!!

The complete code for this article can be found on my github page.
Check this out.
https://github.com/proffdeep/Linear-Regression.git

--

--