Subject:

Last Updated on August 12, Linear regression is a very simple method but has proven to be very useful for a large number of situations. In this post, you will discover exactly how linear regression works step-by-step.

After reading this post you will know:. This tutorial was written for developers and does not assume any prior background in mathematics or statistics.

## Simple linear regression tutorial pdf

This tutorial was written with the intention that you will follow along in your own spreadsheet, which will help to make the concepts stick. Discover how machine learning algorithms work including kNN, decision trees, naive bayes, SVM, ensembles and much more in my new book , with 22 tutorials and examples in excel. The attribute x is the input variable and y is the output variable that we are trying to predict. If we got more data, we would only have x values and we would be interested in predicting y values.

Plot of the Dataset for Simple Linear Regression. As in, we could probably draw a line somewhere diagonally from the bottom left of the plot to the top right to generally describe the relationship between the data. This is a good indication that using linear regression might be appropriate for this little dataset.

Sample of the handy machine learning algorithms mind map. When we have a single input attribute x and we want to use linear regression, this is called simple linear regression.

## Scatterplot Performance with IQ

If we had multiple input attributes e. This would be called multiple linear regression. The procedure for linear regression is different and simpler than that for multiple linear regression, so it is a good place to start.

With simple linear regression we want to model our data as follows:. This is a line where y is the output variable we want to predict, x is the input variable we know and B0 and B1 are coefficients that we need to estimate that move the line around.

## Tutorial Data Set

Technically, B0 is called the intercept because it determines where the line intercepts the y-axis. In machine learning we can call this the bias, because it is added to offset all predictions that we make. The B1 term is called the slope because it defines the slope of the line or how x translates into a y value before we add our bias.

The goal is to find the best estimates for the coefficients to minimize the errors in predicting y from x. Simple regression is great, because rather than having to search for values by trial and error or calculate them analytically using more advanced linear algebra, we can estimate them directly from our data.

Where mean is the average value for the variable in our dataset. Where n is the number of values 5 in this case. Now we need to calculate the error of each variable from the mean. We now have the parts for calculating the numerator. All we need to do is multiple the error for each x with the error for each y and calculate the sum of these multiplications.

Now we need to calculate the bottom part of the equation for calculating B1, or the denominator. This is calculated as the sum of the squared differences of each x value from the mean. We have already calculated the difference of each x value from the mean, all we need to do is square each value and calculate the sum. We can plot these predictions as a line with our data. This gives us a visual idea of how well the line models our data.

## Excel - Simple Linear Regression

Where sqrt is the square root function, p is the predicted value and y is the actual value, i is the index for a specific instance, n is the number of predictions, because we must calculate the error across all predicted values.

Simple linear regression is the simplest form of regression and the most studied.

There is a shortcut that you can use to quickly estimate the values for B0 and B1. Where corr x is the correlation between x and y an stdev is the calculation of the standard deviation for a variable.

A value of 1 indicates that the two variables are perfectly positively correlated, they both move in the same direction and a value of -1 indicates that they are perfectly negatively correlated, when one moves the other moves in the other direction. Standard deviation is a measure of how much on average the data is spread out from the mean. Close enough to the above value of 0.

Note that we get 0. You learned:. Do you have any questions about this post or linear regression? It covers explanations and examples of 10 top algorithms , like: Linear Regression , k-Nearest Neighbors , Support Vector Machines and much more But this stuff is so bizarre. I have a machine learning course on Udemy.

But I have absolutely no clue what this stuff means or does. And the code in python is just weird. Anybody got a suggestion for a Udemy course on machine learning for dumb people?

This stuff is really really hard. Well one hour into my course definitely made me feel stupid.

There is a tutorial on youtube help me a lot on understanding a bit of linear regression you should definitely check it out. I am reading everything and there are so many new things I learned here. I believe the sum of squared errors should be averaged and then squared. First of all, it is good article with explanation.. I have a query.

## SPSS tutorials

At the end we got RMSE value, but what part of the equation train , so that our error reduces at optimal level or near to zero. Please explain,if feasible. Hi chandan, on some problems we may not be able to get zero error because of the noise in the problem. I have query. How did you get this equation? Very simple and convenient to apply. Jason you made it simple. Pl carry on the job of educating. Hi Jason, M a little confused here, may be its because of the formula interpretation that is mentioned here:.

Please correct me if I am wrong, This is bugging me. Kindly explain! I had a lot of confusion on finding Theta. I had gone through a lot of youtube and other web site tutorials..

## Simple Linear Regression Tutorial for Machine Learning

Now i understood. Very good Article. Could you please explain? As you are probability in statistics there are many assumptions that you make with respect to the underlying data. How is it the machine learning can dismiss these?

Thank you very much Jason for the wonderful explanation,,as everyone knows how tough the subject machine learning is ,,u made it so simple ,keep doing ,God bless u,once again thank you.

I am sorry but I am unable to find that blog through the search box at the top, so plz can you send me the link of that blog…. Really appreciate your idea of using Excel to understand the algo better. We sometimes, ignore the power of simpler things. How do you create the data OR better yet if I copied the data into a csv file, how do import it into python?

At the end of the tutorial you have explained the shortcut method to calculate coefficient B1, Do we have the shortcut method for the other coefficient B0 as well?

Please can you explain how linear regression works in detail like for getting the best fit line how we use OLS estimates and all?

It is a very simple way to explain the overall concept of linear regression. Very impressive and superb. Very easy to understand.

## Get your FREE Algorithms Mind Map

Is there any range available for RMSE. It means if it close to value 1, then more error available. Error scores are usually relative to the scale and units of the output variable e. They are best interpreted in the context of a little domain expertise for the problem. I have one doubt in the above post. Using the above method, we will get only one value for B0 and B1 right?

How do we go about minimising the errors using this method and finding new B0 and B1? Pls correct me if i have misinterpreted something here. But what will be the equation of multiple linear regression and how will I calculate the constants in it? So Can you explain or give me some math references so that i can solve the problem myself?.

Can you suggest me a book to get start with Data Science and Machine Learning. Thanks a lot for that. Many forums mentioned, that 1 dependent variable and 1 independent variable is the criteria, but I feel with same criteria there can be non-linear data also. Thank Jason.