Hello,

In this post we’ll show another way different to solve the problem of error minimization in linear regression. Instead of using gradient descent, we could solve this linear system using matrices.

## Normal equation

As we know, a system of linear equations can be solved by matrices. In this case, we don’t need to define learning rate , we don’t need to iterate, we don’t need to scale features.

We minimize the square errors by taking the derivatives and setting them to zero and then we solve this linear system with the following matrix equation:

**Remember**: is a vector

How this equation works and why? As seen before, once we define the error function, the whole idea is find a way to minimize it. In our case we are using mean squared error (MSE). We can get the minimum error by taking the derivatives:

and setting them to zero

Working on the equation we have

and finally

Now, we can solve this using matrices:

i)

ii)

iii)

You can find a very good (and very long) explanation on how it works here.

I wrote a script on octave to calculate the parameters using normal equation method with both datasets used for simple and multiple linear regression. Here is the results compared to those using gradient descent:

**Simple linear regression**

Starting gradient descent algorithm with and and 10000 iterations

Normal equation | Gradient descent | |
---|---|---|

1.2921 | 1.4826 | |

9.7777 | 0.065248 | |

Error | 53.776 | 55.529 |

We can see that gradient descent finally converged to a local minimum.

**Multiple linear regression**

Starting the algorithm with , and and 10000 iterations

Normal equation | Gradient descent | |
---|---|---|

77.09660 | 73.632116 | |

0.33745 | 0.652041 | |

-1.01820 | -0.859574 | |

Error | 11.529 | 11.716 |

I did another test with 100,000 iterations and for multiple linear regression it finally converged to the global minimum since I got the very same parameters.

## Conclusion

As we can see, normal equation is another way to solve linear regression. It’s more direct, it finds the optimum solution and we don’t need any parameters to be tuned. But why we don’t use it always? Why do we need gradient descent?

Well, as anything is perfect, normal equation need much more processing that gradient descent. For few features (less than 10,000 accordingly to Andrew Ng) it’s acceptable but more than that, we can start thinking about gradient descent. Gradient descent can be thought as an approximation of the ideal solution since it may or may not converge to the global minimum.

See ya!

## Leave a Reply