[New Book] Click to get The Beginner's Guide to Data Science!
Use the offer code 20offearlybird to get 20% off. Hurry, sale ends soon!

Simple Linear Regression Tutorial for Machine Learning

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:

  • How to calculate a simple linear regression step-by-step.
  • How to perform all of the calculations using a spreadsheet.
  • How to make predictions on new data using your the model.
  • A shortcut that greatly simplifies the calculation.

This tutorial was written for developers and does not assume any prior background in mathematics or statistics.

This tutorial was written with the intention that you will follow along in your own spreadsheet, which will help to make the concepts stick.

Kick-start your project with my new book Master Machine Learning Algorithms, including step-by-step tutorials and the Excel Spreadsheet files for all examples.

Let’s get started.

Update #1: Fixed a bug in the calculation of RMSE.

Simple Linear Regression Tutorial for Machine Learning

Simple Linear Regression Tutorial for Machine Learning
Photo by Catface27, some rights reserved.

Tutorial Data Set

The data set we are using is completely made up.

Below is the raw data.

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.

Below is a simple scatter plot of x versus y.

Plot of the Dataset for Simple Linear Regression

Plot of the Dataset for Simple Linear Regression

We can see the relationship between x and y looks kind of linear. 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.

Get your FREE Algorithms Mind Map

Machine Learning Algorithms Mind Map

Sample of the handy machine learning algorithms mind map.

I've created a handy mind map of 60+ algorithms organized by type.

Download it, print it and use it. 


Also get exclusive access to the machine learning algorithms email mini-course.

 

 

Simple Linear Regression

When we have a single input attribute (x) and we want to use linear regression, this is called simple linear regression.

If we had multiple input attributes (e.g. x1, x2, x3, etc.) 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.

In this section we are going to create a simple linear regression model from our training data, then make predictions for our training data to get an idea of how well the model learned the relationship in the data.

With simple linear regression we want to model our data as follows:

y = B0 + B1 * x

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.

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.

We can start off by estimating the value for B1 as:

B1 = sum((xi-mean(x)) * (yi-mean(y))) / sum((xi – mean(x))^2)

Where mean() is the average value for the variable in our dataset. The xi and yi refer to the fact that we need to repeat these calculations across all values in our dataset and i refers to the i’th value of x or y.

We can calculate B0 using B1 and some statistics from our dataset, as follows:

B0 = mean(y) – B1 * mean(x)

Not that bad right? We can calculate these right in our spreadsheet.

Estimating The Slope (B1)

Let’s start with the top part of the equation, the numerator.

First we need to calculate the mean value of x and y. The mean is calculated as:

1/n * sum(x)

Where n is the number of values (5 in this case). You can use the AVERAGE() function in your spreadsheet. Let’s calculate the mean value of our x and y variables:

mean(x) = 3

mean(y) = 2.8

Now we need to calculate the error of each variable from the mean. Let’s do this with x first:

Now let’s do that for the y variable

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.

Summing the final column we have calculated our numerator as 8.

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.

Calculating the sum of these squared values gives us up denominator of 10

Now we can calculate the value of our slope.

B1 = 8 / 10

B1 = 0.8

Estimating The Intercept (B0)

This is much easier as we already know the values of all of the terms involved.

B0 = mean(y) – B1 * mean(x)

or

B0 = 2.8 – 0.8 * 3

or

B0 = 0.4

Easy.

Making Predictions

We now have the coefficients for our simple linear regression equation.

y = B0 + B1 * x

or

y = 0.4 + 0.8 * x

Let’s try out the model by making predictions for our training data.

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.

Simple Linear Regression Model

Simple Linear Regression Model

Estimating Error

We can calculate a error for our predictions called the Root Mean Squared Error or RMSE.

RMSE = sqrt( sum( (pi – yi)^2 )/n )

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.

First we must calculate the difference between each model prediction and the actual y values.

We can easily calculate the square of each of these error values (error*error or error^2).

The sum of these errors is 2.4 units, dividing by n and taking the square root gives us:

RMSE = 0.692

Or, each prediction is on average wrong by about 0.692 units.

Shortcut

Before we wrap up I want to show you a quick shortcut for calculating the coefficients.

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.

Really it is a shortcut for calculating B1. The calculation of B1 can be re-written as:

B1 = corr(x, y) * stdev(y) / stdev(x)

Where corr(x) is the correlation between x and y an stdev() is the calculation of the standard deviation for a variable.

Correlation (also known as Pearson’s correlation coefficient) is a measure of how related two variables are in the range of -1 to 1. 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.

You can use the function PEARSON() in your spreadsheet to calculate the correlation of x and y as 0.852 (highly correlated) and the function STDEV() to calculate the standard deviation of x as 1.5811 and y as 1.4832.

Plugging these values in we have:

B1 = 0.852  * 1.4832 / 1.5811

B1 = 0.799

Close enough to the above value of 0.8. Note that we get 0.8 if we use the fuller precision in our spreadsheet for the correlation and standard deviation equations.

Summary

In this post you discovered how to implement linear regression step-by-step in a spreadsheet. You learned:

  • How to estimate the coefficients for a simple linear regression model from your training data.
  • How to make predictions using your learned model.

Do you have any questions about this post or linear regression? Leave a comment and ask your question, I’ll do my best to answer.

Discover How Machine Learning Algorithms Work!

Mater Machine Learning Algorithms

See How Algorithms Work in Minutes

...with just arithmetic and simple examples

Discover how in my new Ebook:
Master Machine Learning Algorithms

It covers explanations and examples of 10 top algorithms, like:
Linear Regression, k-Nearest Neighbors, Support Vector Machines and much more...

Finally, Pull Back the Curtain on
Machine Learning Algorithms

Skip the Academics. Just Results.

See What's Inside

131 Responses to Simple Linear Regression Tutorial for Machine Learning

  1. Avatar
    chen September 3, 2016 at 5:43 am #

    I took half of one day to learn, It is very good, Thanks to Jasom.

  2. Avatar
    AmiMo meshaq September 19, 2016 at 6:12 pm #

    You r a wonderful tutor. God send
    Keep up the beautiful work..

  3. Avatar
    Dominika Tkaczyk September 30, 2016 at 2:17 am #

    Hi Jason,

    many thanks for an amazing blog! I am reading everything and there are so many new things I learned here.

    Correct me if I am wrong, but I think there is a small mistake in the calculation of RMSE in this post. I believe the sum of squared errors should be averaged and then squared. The results is weird as well: RMSE = 1.549 exceeds the error for each data point.

    • Avatar
      Jason Brownlee September 30, 2016 at 7:51 am #

      Yes, thanks Dominika. It is on my TODO list to fix this up.

      Update: I have fixed the calculation of RMSE.

  4. Avatar
    chandan November 3, 2016 at 7:12 pm #

    First of all, it is good article with explanation..
    I have a query.
    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.

    • Avatar
      Jason Brownlee November 4, 2016 at 9:06 am #

      Hi chandan, on some problems we may not be able to get zero error because of the noise in the problem.

  5. Avatar
    pranaya November 18, 2016 at 5:04 pm #

    hello jason this is really helpful.
    I have query.
    how to set value of theta for minimise cost function in linear regression

    • Avatar
      Jason Brownlee November 19, 2016 at 8:47 am #

      What do you mean by theta in this context pranaya? Learning rate? A coefficient value?

  6. Avatar
    Deependra December 1, 2016 at 5:33 am #

    hi Jason ,

    your article on simple linear regression is awesome. is there any tutorial for Ridge Regression .

    • Avatar
      Jason Brownlee December 1, 2016 at 7:33 am #

      Thanks Deependra. Sorry, nothing on ridge regression right now.

  7. Avatar
    Amar January 12, 2017 at 11:37 am #

    Could you please explain the following equation?

    B1 = sum((xi-mean(x)) * (yi-mean(y))) / sum((xi – mean(x))^2)

    How did you get this equation? I mean B1 is the slope, so it should look something like ((y2-y1)/(x2-x1)).

    New to this

  8. Avatar
    Asbar January 18, 2017 at 3:19 am #

    Thankzzz a lot 🙂

  9. Avatar
    Vijay April 2, 2017 at 11:31 am #

    You are Awesome Jason. Thanks for the article.

  10. Avatar
    Munir Hussain April 13, 2017 at 4:17 am #

    A big work done by Jason.
    Thanks Jason,

  11. Avatar
    Nuwan C May 5, 2017 at 11:42 am #

    Hi Json,
    Thank you for the good tutorial.God bless you

  12. Avatar
    P J Kulkarni June 7, 2017 at 5:03 pm #

    Very simple and convenient to apply.
    Jason you made it simple. Pl carry on the job of educating.

  13. Avatar
    Rahul Sharma June 13, 2017 at 5:53 am #

    Hi Jason, M a little confused here, may be its because of the formula interpretation that is mentioned here:

    B1 = corr(x, y) * stdev(y) / stdev(x)

    Also,

    corr(x,y) = (Covariance of x & y)/stdev(x)*stdev(y)

    So does that makes: B1= (Covariance of x & y) / (stdev(x))^2)

    Please correct me if I am wrong, This is bugging me. With this we don’t have to calculate stdev(y) .Kindly explain !

  14. Avatar
    Pradeesh August 5, 2017 at 6:54 pm #

    wonderful tutorial. I had a lot of confusion on finding Theta. I had gone through a lot of youtube and other web site tutorials.. Now i understood.

  15. Avatar
    Hima September 2, 2017 at 4:06 am #

    Explained very well in excellent manner. I am glad I found this tutorial.. Thank you Jason

  16. Avatar
    Anu September 10, 2017 at 6:42 pm #

    Thank you Jason for the wonderful article,
    One clarification, for the given dataset list, this is the beat fit and we don’t need to adjust B0 and B1, is my assumption right?

    • Avatar
      Jason Brownlee September 11, 2017 at 12:06 pm #

      It is a fit, perhaps not the best as we used a stochastic process.

  17. Avatar
    Jeersseg September 12, 2017 at 7:29 pm #

    i am not able to understand whole. please explain in good way

  18. Avatar
    Arokia September 14, 2017 at 3:59 pm #

    Very good Article. I don’t understand why we calculated RMSE here. Could you please explain?

    • Avatar
      Jason Brownlee September 15, 2017 at 12:10 pm #

      It is the error of the predictions used to summarize the skill of the model.

      You can use other error metrics if you prefer.

  19. Avatar
    Richard Weisberger September 25, 2017 at 4:55 am #

    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?

    • Avatar
      Jason Brownlee September 25, 2017 at 5:39 am #

      By focusing on the skill of the predictive model over all other concerns.

      It transforms the problem from “what is going on in the data” to “what will make the predictions more skillful”.

  20. Avatar
    Arjun October 3, 2017 at 4:15 pm #

    In the section : Making predictions: How did you get the value of field ” Predicted Y”

    • Avatar
      Jason Brownlee October 4, 2017 at 5:43 am #

      By using the regression equation that we developed and entering in the input data (x).

  21. Avatar
    Esther Guite October 4, 2017 at 10:26 am #

    This is great help. Thank you, Jason!

  22. Avatar
    Scott Johnson October 12, 2017 at 7:12 am #

    Hi Jason, the algorithms mind map link is not working !

  23. Avatar
    AJ November 14, 2017 at 2:41 pm #

    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

  24. Avatar
    Aditya Jain January 17, 2018 at 12:20 am #

    Thanks Jason for this wonderful explanation.

    Have you done any blogs on multiple linear regression as well.

    Plz attach the link if so…

  25. Avatar
    Aditya Jain January 17, 2018 at 11:23 pm #

    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….

  26. Avatar
    vk February 12, 2018 at 10:16 pm #

    jason can you please derive it for logistic and multiple regessions

  27. Avatar
    vishnu kumar March 8, 2018 at 7:28 pm #

    Really your blogs are very helpful in learning.

  28. Avatar
    Neha April 4, 2018 at 6:52 pm #

    Hi Jason,

    Hope u are doing great!

    Really appreciate your idea of using Excel to understand the algo better. We sometimes, ignore the power of simpler things.

    Thanks.

  29. Avatar
    John Hurford April 9, 2018 at 9:50 am #

    How do you create the data OR better yet if I copied the data into a csv file, how do import it into python?

  30. Avatar
    Akshay April 28, 2018 at 1:07 am #

    First of all thank you very much for explaining in very simple manner.

    Now the question 🙂

    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?

    Thank you

  31. Avatar
    Bhaumik Shah April 28, 2018 at 3:33 am #

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

  32. Avatar
    BHAVIK AMARDAS DUDHREJIYA May 23, 2018 at 4:23 am #

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

  33. Avatar
    Kumar May 29, 2018 at 4:59 pm #

    Excellent Jason for simplifying the analysis.

    I have doubt in calculating the error which u mentioned “each prediction is on average wrong by about 0.692 units.”

    What does this mean. Anyhow the prediction is good by looking at the scatter plot.

    Is there any range available for RMSE. It means if it close to value 1, then more error available.

    I am newbie and do not understand.

    • Avatar
      Jason Brownlee May 30, 2018 at 6:35 am #

      Error scores are usually relative to the scale and units of the output variable (e.g. cm, feet, dollars, etc). They are best interpreted in the context of a little domain expertise for the problem.

  34. Avatar
    Priya June 19, 2018 at 2:33 pm #

    Awesome Explanation. I am much clearer about the concept.

  35. Avatar
    Sara June 24, 2018 at 2:57 pm #

    Hi Jason,

    Thanks a lot for all your posts. I am really enjoying myself learning ML!!!

    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.

    Regards
    Sara.

    • Avatar
      Jason Brownlee June 25, 2018 at 6:18 am #

      Yes, the process will give you one set of coefficients, that is the model.

      The learning process will minimize error.

  36. Avatar
    Akshay July 26, 2018 at 12:26 am #

    Hey Jason,

    It was a great explanation for simple linear regression.

    But what will be the equation of multiple linear regression and how will I calculate the constants in it?

    Thanks in advance.

    • Avatar
      Jason Brownlee July 26, 2018 at 7:43 am #

      You simply add more variables (X) and coefficients (beta).

  37. Avatar
    hosjiu July 27, 2018 at 8:19 pm #

    Hi, Jason

    I read “Fitting the regression line” part on Wiki ( https://en.wikipedia.org/wiki/Simple_linear_regression#Fitting_the_regression_line ). But I cant find alpha_hat and beta_hat for minimization problem myself. So Can you explain or give me some math references so that i can solve the problem myself ?.

    Thanks.

  38. Avatar
    Akhileshwar Reddy September 19, 2018 at 6:52 pm #

    Hi Jason,

    I’m a student of Bachelor of Technology. Can you suggest me a book to get start with Data Science and Machine Learning.

  39. Avatar
    Krishna Kiriti Chinthapalli October 17, 2018 at 1:25 am #

    Hi Jason, very good article with detailed explanation which I couldn’t find it in any other sites. Thanks a lot for that.

    “How to use” is very clear, it would be nice if “when to use” is also mentioned.

    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. please help me out how to find “when to use what”.

    Thanks again for the detailed explanation.

  40. Avatar
    Maulik Patel November 19, 2018 at 3:38 am #

    Great explanation.

    I’m very happy and much more excited to learn using your this blog post as my first machine learning algorithm.

    Thanks a lot…!!!

  41. Avatar
    Syamala December 11, 2018 at 5:11 pm #

    Very nice explanation sir.

  42. Avatar
    بلوک سبک March 5, 2019 at 5:20 pm #

    could you please record video for ML tutorials? it would catch so many attentions

    • Avatar
      Jason Brownlee March 6, 2019 at 7:43 am #

      Thanks for the suggestion.

      I think video is too passive, we are a community of “doers” not “watchers”.

  43. Avatar
    Emmanuel March 29, 2019 at 10:59 am #

    Thank Jason,
    Very helpful point.

    can you please continue to multiple linear regression.

  44. Avatar
    Nilesh April 10, 2019 at 4:13 pm #

    Thank u so much

  45. Avatar
    Armin April 20, 2019 at 11:35 pm #

    Thank Jason.
    I searched a lot about Linear regression but I didn’t find a good source. This source is very good. Thanks, bro.

  46. Avatar
    datajango June 10, 2019 at 5:22 pm #

    your article is very good , i able to understand the simple linear regression concepts very easily. thanks for great article.

  47. Avatar
    BHARATH R June 27, 2019 at 9:59 pm #

    Thank you very much sir for a wonderfull explanation..

  48. Avatar
    Gull Mohammad September 25, 2019 at 10:37 pm #

    Thank you sir, you explained very good. keep it up. can you explain Kernel PCA ?

  49. Avatar
    Hala October 18, 2019 at 7:40 pm #

    Very simple and interested way in explanation. Keep going fot other prediction algorithm ????

  50. Avatar
    Hemanth Kumar November 27, 2019 at 2:09 am #

    Thank you so much for sharing the valuable information. Easy to understand and can get the details without using any tools.

    After doing these calculations, I hope we can consider the model which we built is accurate. Please suggest.

    • Avatar
      Jason Brownlee November 27, 2019 at 6:09 am #

      A model can be evaluated by making predictions on data not used during training and calculating the error between the predictions and expected values.

  51. Avatar
    Hemanth Kumar November 27, 2019 at 7:07 pm #

    Got it. Thanks for the clarification

  52. Avatar
    jasper December 11, 2019 at 8:31 pm #

    Why not use the pinv () Pseudoinverse to get the B0 and B1 parameters ? anyway reference for me to check out the “short-cut” method ? this method is good in not doing the inverse-matrix operation. thank you.

  53. Avatar
    Mona April 9, 2020 at 1:09 am #

    Thank you for this explanation. It was easier to learn the concept by reading your guide for 15 mins vs a 2 hour class! Hope you’re keeping well and safe

  54. Avatar
    Remi April 9, 2020 at 4:46 am #

    Hi Jason, great article. I was wondering if you have similar article for Multiple linear regression

  55. Avatar
    Max May 23, 2020 at 4:26 am #

    Hi Jason,

    I have data which records my mood every day. I collected it over almost a year, it has 2 columns, date and mood. There are 5 types of moods which were recorded along with the date. Now using this data I want to predict my mood on a certain date in future. I want to know can you help me with this, I’m a beginner absolute newbie in DS & ML. I’m thinking of using LinearRegression we can predict the outcome.

    Can you suggest me how to approach the solution? How can I pass a date as an argument to ML Algo to predict the mood? Please help me.

    NOTE: If you want I can share the data, nothing personal in it. Anybody who is interested in this please contact me on maxlinkdir[at]Gmail[dot]com

    Thanks.

  56. Avatar
    Imrat Haqqani May 31, 2020 at 11:21 pm #

    Hi Jason,

    It is a awesome resource, just one doubt, as RMSE is 0.69, does it mean our model is 69 % not predicting the values correctly, in other words our model is 31 % giving correct predictions.
    Also, after estimating the errors , do we need to sum the square numbers or average them, as
    pointed by Dominika Tkaczyk on 30th September 2016.
    Would appreciate if you could clarify this.

    • Avatar
      Jason Brownlee June 1, 2020 at 6:22 am #

      Thanks.

      No it is the prediction error of the model. Think about it as on average a prediction will be about 0.69 units wrong, whatever the units of the target variable happen to be.

  57. Avatar
    S Dash September 17, 2020 at 5:01 am #

    Hi Jason,

    It took few hours to do it. It is really worth doing it without coding.

    Regards,
    S Dash.

  58. Avatar
    Mohit chaudhari March 2, 2021 at 10:55 pm #

    Thanks, for the tutorial.One of the best tutorials over the internet for a basic understanding of regression.

  59. Avatar
    Rishabh Parikh May 9, 2021 at 9:31 pm #

    Hi Jason, this helped me clear my doubts and made it fairly simple to understand the concept and application of simple linear regression. Really appreciate the effort. Thanks

  60. Avatar
    Shima May 11, 2021 at 4:22 pm #

    Hi Jason,

    It is an awesome article and one of the best resources in the machine learning tutorials. could you please explain about finding coefficients in multiple regression with kerasregressor?

Leave a Reply