With Machine Learning and Artificial Intelligence booming the IT market it has become essential to learn the fundamentals of these trending technologies. This blog on Least Squares Regression Method will help you understand the math behind Regression Analysis and how it can be implemented using Python.
To get in-depth knowledge of Artificial Intelligence and Machine Learning, you can enroll for live Machine Learning Engineer Master Program by Edureka with 24/7 support and lifetime access.

###
**What Is the Least Squares Method?**

**Line Of Best Fit**
**Steps to Compute the Line Of Best Fit**
**The least-squares regression method with an example**
**A short python script to implement Linear Regression**
**What is the Least Squares Regression Method?**
**The least-squares regression method is a technique commonly used in Regression Analysis. It is a mathematical method used to find the best fit line that represents the relationship between an independent and dependent variable. **
To understand the least-squares regression method lets get familiar with the concepts involved in formulating the line of best fit.
What is the Line Of Best Fit?

Line of best fit is drawn to represent the relationship between 2 or more variables. To be more specific, the best fit line is drawn across a scatter plot of data points in order to represent a relationship between those data points. Regression analysis makes use of mathematical methods such as least squares to obtain a definite relationship between the predictor variable (s) and the target variable. The least-squares method is one of the most effective ways used to draw the line of best fit. It is based on the idea that the square of the errors obtained must be minimized to the most possible extent and hence the name least squares method. If we were to plot the best fit line that shows the depicts the sales of a company over a period of time, it would look something like this:

**Regression Analysis Example** - Least Squares Regression Method - Edureka

Notice that the line is as close as possible to all the scattered data points. This is what an ideal best fit line looks like.

To better understand the whole process let’s see how to calculate the line using the Least Squares Regression.

Steps to calculate the Line of Best Fit

To start constructing the line that best depicts the relationship between variables in the data, we first need to get our basics right. Take a look at the equation below:

Regression line formula - Least Squares Regression Method - Edureka

Surely, you’ve come across this equation before. It is a simple equation that represents a straight line along 2 Dimensional data, i.e. x-axis and y-axis. To better understand this, let’s break down the equation:

**y: dependent variable**

**m: the slope of the line**

**x: independent variable**

**c: y-intercept**

So the aim is to calculate the values of slope, y-intercept and substitute the corresponding ‘x’ values in the equation in order to derive the value of the dependent variable.

Let’s see how this can be done.

As an assumption, let’s consider that there are ‘n’ data points.

**Step 1: Calculate the slope ‘m’ by using the following formula:**

**Slope of a Line formula - Least Squares Regression Method - Edureka**

**Step 2: Compute the y-intercept (the value of y at the point where the line crosses** the y-axis):