Simple Linear Regression from Scratch with Python Implementation
A popular question among new ones of Machine Learning. What is a Linear Regression? In this post, we are going to see about Simple Linear Regression and to build a linear model from scratch with python implementation.
Great Success needs Daily Efforts
Table of contents
- What is Simple Linear Regression?
- Why and When?
- Build Simple Linear Regression from Scratch
- Flow Chart
- Python Implementation
What is Simple Linear Regression?
Simple linear regression is a linear model built with a single explanatory variable. It takes two-column (or variable) for training a linear model where one is the Independent column (X) and the other is the Dependent or Target column (y).
Equation of a Straight Line :
y = mx + c
This is the base equation of the Linear Regression.
In the above graph,
- The Green-line represents a regression line.
- The Violet circle represents data points.
- The Violet triangle represents the mean values. (X_mean, y_mean)
Why and When?
If you are having data about an entity where you want to know about the relation of any two numerical factors of the entity then the simple linear regression comes for the rescue.
For Example, Years of Experience and Salary of an Employee, Height and Weight of an Individual, and many more…
Build a Simple Linear Regression from Scratch
Our final goal is to find two values.1. Slope2. Intercept
STEP 1: Get the Data with numerical factors of an entity with one independent column (X) and one dependent column (y).
STEP 2: Compute the mean value of the independent column (X). Add all the values in the independent column X and divide by the number of values in the independent column X.
STEP 3: Compute the mean value of the dependent column (y). Add all the values in the dependent column y and divide by the number of values in the dependent column y.
Note : Both Independent columns and Dependent column must have same number of rows.
STEP 4: Compute two more columns with mean differences from X and y. For each value in the Independent column X, we have to subtract X_mean. Similarly, For each value in the dependent column y, we have to subtract y_mean.
STEP 5: Compute Slope. The slope can be found by using the below formula.
STEP 6: Compute Intercept. The intercept can be found by using the standard equation of a straight line
y = mx + c.
That’s it. We have found what we want to build a linear model using Simple Linear Regression