Gradient Decent in Linear Regression

Ankita Banerji
Analytics Vidhya
Published in
4 min readApr 5, 2021

A linear regression model attempts to explain the relationship between a dependent (output variables) variable and one or more independent (predictor variable) variables using a straight line.

This straight line is represented using the following formula:

Where,

y: Dependent variable

x: Independent variable

m: Slope of the line (For a unit increase in the quantity of X, Y increases by m.1 = m units.)

c: y intercept (The value of Y is c when value of X is 0)

Equation of straight line

The first step in finding a linear regression equation is to determine if there is a relationship between the two variables. We can do this by using Correlation coefficient and scatter plot. When a correlation coefficient shows that data is likely to be able to predict future outcomes and a scatter plot of the data appears to form a straight line, we can use simple linear regression to find a predictive function. Let us consider an example.

Sales vs Marketing Spent Scatter plot

From the scatter plot we can see there is a linear relationship between Sales and marketing spent. Next step is to find a straight line between Sales and Marketing that explain the relationship between them. But there can be multiple lines that can pass through these points.

So how do we know which of these lines is the best fit line? That’s the problem that we will solve in this article. For this we will first look at the cost function.

Cost Function

The cost is the error in our predicted value. We will use Mean Squared Error function to calculate the cost.

Our goal is to minimize the cost as much as possible in order to find the best fit line. We are not going to try all the permutation and combination of m and c (inefficient way) to find best fit line. For that we will use Gradient Decent Algorithm.

Gradient Decent Algorithm

Gradient Decent is an algorithm that finds best fit line for a given training dataset in less number of iteration.

If we plot m and c against MSE, it will acquire a bowl shape (As shown in the diagram below)

For some combination of m and c we will get the least Error (MSE). That combination of m and c will give us our best fit line.

The algorithm starts with some value of m and c (usually starts with m=0, c=0). We calculate MSE (cost) at point m=0, c=0. Let say the MSE (cost) at m=0, c=0 is 100. Then we reduce the value of m and c by some amount (Learning Step). We will notice decrease in MSE (cost). We will continue doing the same until our loss function is a very small value or ideally 0 (which means 0 error or 100% accuracy).

Step By Step Algorithm

  1. Let m = 0 and c = 0. Let L be our learning rate. It could be a small value like 0.01 for good accuracy.

Learning rate gives the rate of speed where the gradient moves during gradient descent. Setting it too high would make your path instable, too low would make convergence slow. Put it to zero means your model isn’t learning anything from the gradients.

2. Calculate the partial derivative of the Cost function with respect to m. Let partial derivative of the Cost function with respect to m be Dm (With little change in m how much Cost function changes)

Similarly let’s find the partial derivative with respect to c. Let partial derivative of the Cost function with respect to c be Dc (With little change in c how much Cost function changes).

3. Now update the current values of m and c using the following equation:

4. We will repeat this process until our Cost function is a very small (ideally 0).

Gradient Decent Algorithm gives optimum values of m and c. With these values of m and c we will get the equation of the best-fit line and ready to make predictions.

--

--