{"id":1867,"date":"2018-12-03T12:00:00","date_gmt":"2018-12-03T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/simple-linear-regression-in-machine-learning-a-simple-tutorial\/"},"modified":"2026-07-05T03:21:17","modified_gmt":"2026-07-05T01:21:17","slug":"simple-linear-regression-in-machine-learning-a-simple-tutorial","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/simple-linear-regression-in-machine-learning-a-simple-tutorial\/","title":{"rendered":"Simple Linear Regression in Machine Learning (A Simple Tutorial)"},"content":{"rendered":"<p>In this tutorial we would cover Simple Linear Regression in a very easy-to-understand way. We are assuming you don&#8217;t have much knowledge of Machine Learning and maybe a little knowledge of statistics.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-156 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Simple-Linear-Regression-in-Machine-Learning-300x212.jpg\" alt=\"\" width=\"300\" height=\"212\" \/><\/p>\n<p>We would examine the following topics:<\/p>\n<ol>\n<li><a href=\"#t1\">Introduction to Simple Linear Regression<\/a><\/li>\n<li><a href=\"#t2\">What is\u00a0\u00a0\u03b2<sub>0<\/sub> and\u00a0\u03b2<sub>1<\/sub><\/a><\/li>\n<li><a href=\"#t3\">Estimating Regression Coefficients\u00a0\u03b2<sub>0<\/sub> and\u00a0\u03b2<sub>1<\/sub><\/a><\/li>\n<li><a href=\"#t4\">Method of Least Squares<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4 id=\"t1\">1.\u00a0 Introduction to Simple Linear Regression<\/h4>\n<p><i>Simple linear regression <\/i>lives up to its name: it is a very straightforward approach for predicting a quantitative response <i>Y <\/i>on the basis of a single predictor variable <i>X<\/i>. It assumes that there is approximately a linear relationship between <i>X <\/i>and <i>Y <\/i><\/p>\n<p>Consider the dataset in table 1.0. The table shows the amount of Sales for\u00a0 a given amount spent on advertising.<\/p>\n<table style=\"height: 208px;\" width=\"711\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<td><strong>Year<\/strong><\/td>\n<td><strong>Adverts (X)<\/strong><\/td>\n<td><strong>Sales (Y)<\/strong><\/td>\n<\/tr>\n<tr>\n<td>2008<\/td>\n<td>$60<\/td>\n<td>$1500<\/td>\n<\/tr>\n<tr>\n<td>2009<\/td>\n<td>$75<\/td>\n<td>$2200<\/td>\n<\/tr>\n<tr>\n<td>2010<\/td>\n<td>$77<\/td>\n<td>$3500<\/td>\n<\/tr>\n<tr>\n<td>2011<\/td>\n<td>$89<\/td>\n<td>$4230<\/td>\n<\/tr>\n<tr>\n<td>2012<\/td>\n<td>$93<\/td>\n<td>$5500<\/td>\n<\/tr>\n<tr>\n<td>2014<\/td>\n<td>$101<\/td>\n<td>$5910<\/td>\n<\/tr>\n<tr>\n<td>2015<\/td>\n<td>$104<\/td>\n<td><span style=\"color: #ff0000;\">????<\/span><\/td>\n<\/tr>\n<tr>\n<td>2018<\/td>\n<td>$110<\/td>\n<td><span style=\"color: #ff0000;\">????<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><em>Table 1.0<\/em>: Yearly Sales Values for Adverts<\/p>\n<p>The goal of simple linear regression is to predict the Sales for given advert. In order words, we want to predict the response Y(dependent variable), based on the predictor variable X (independent variable).<\/p>\n<p>In Simple Linear Regression, we make an assumption that there exists a linear relationship between the two variable.\u00a0 The easiest way to make this prediction is to find the function y = f(x) that relates the two variable. This we can do by:<\/p>\n<ul>\n<li>plot the table on a graph<\/li>\n<li>draw the line<\/li>\n<li>find the equation of the line<\/li>\n<\/ul>\n<p>The equation of a line is given by<\/p>\n<p style=\"text-align: center;\">y = mx + c<\/p>\n<p>But in the language of regression we write it as<\/p>\n<p style=\"text-align: center;\">Y\u00a0\u2248 \u03b2<sub>0<\/sub> +\u03b2<sub>1<\/sub>X<\/p>\n<p>Note that we did not use equal sign (=). The sign\u00a0\u2248 is a regression operator that says that Y is &#8220;modeled as&#8221; and not Y is equal to.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4 id=\"t2\">2. What are\u00a0\u03b2<sub>0<\/sub> and\u00a0\u03b2<sub>1<\/sub>?<\/h4>\n<p>These are called the regression coefficients, model coefficients or model parameters which are unknown.<\/p>\n<ul>\n<li>\u03b2<sub>1<\/sub> represents the slope of the\u00a0 model<\/li>\n<li>\u03b2<sub>0<\/sub> represents the intercept term<\/li>\n<\/ul>\n<p>In Machine learning, the training data is used to determine values that are close to\u00a0\u03b2<sub>0<\/sub> and\u00a0\u03b2<sub>1<\/sub> but not exactly. If we call the coefficients produced by the training\u00a0\u03b2<sub>0<\/sub>&#8216; and\u00a0\u03b2<sub>1<\/sub>&#8216;, then we can predict the values of future sales using the formula:<\/p>\n<p>y&#8217; = \u03b2<sub>1<\/sub>&#8216; +\u03b2<sub>0<\/sub>&#8216;x<\/p>\n<p>The &#8216; in the variables indicate the the values are estimates of the unknown parameters.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4 id=\"t3\">3. Estimating the Model Coefficients<\/h4>\n<p>To estimate the coefficients\u00a0\u03b2<sub>0<\/sub> and\u00a0\u03b2<sub>1<\/sub>, we need to use the data we have. In the Table 1.0, you can see that we have 6 known data points and 2 unknown points. We can represent out dataset as:<\/p>\n<p style=\"text-align: center;\">(x<sub>1<\/sub>,y<sub>1<\/sub>), (x<sub>2<\/sub>,y<sub>2<\/sub>), . . . , (x<sub>n<\/sub>, y<sub>n<\/sub>)<\/p>\n<p>where n = 6.<\/p>\n<p>For our dataset, (x<sub>1<\/sub>,y<sub>1<\/sub>) = (60, 1500). (x<sub>2<\/sub>, y<sub>2<\/sub>) = ( 75, 2200) and so on.<\/p>\n<p>If we use the method of plotting the graph and making the line pass through each of the datapoint, we would get the exact values of\u00a0\u03b2<sub>1<\/sub> and\u00a0\u03b2<sub>0<\/sub>. We would use our training data set to\u00a0 obtain estimate\u00a0\u03b2<sub>0<\/sub>&#8216; and\u00a0\u03b2<sub>1<\/sub>&#8216;<\/p>\n<p>So if we want to make an estimate for the value of sales for 2018, we would, apply the coefficients such that:<\/p>\n<p>y&#8217; = \u03b2<sub>0<\/sub>&#8216; +\u03b2<sub>1<\/sub>&#8216; * 110<\/p>\n<p>The objective is to find the estimated coefficietns\u00a0 \u03b2<sub>0<\/sub>&#8216; and \u03b2<sub>1<\/sub>&#8216; that is a close to\u00a0 \u03b2<sub>0<\/sub> and \u03b2<sub>1<\/sub> as possible.\u00a0 The difference between our estimated value and the real values is an error term which needs to be minimized. The method we are going to use to do this is called the Least Squares approach. Let&#8217;s see how it works<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4 id=\"t4\">4. The Least Squares Approach<\/h4>\n<p>Let&#8217;s assume we find the value of\u00a0 \u00a0y<sub>i<\/sub>&#8216; = \u03b2<sub>0<\/sub>&#8216; + \u03b2<sub>1<\/sub>&#8216;x<sub>i<\/sub> for the ith observation.<\/p>\n<p>Then we can calculate the error e<sub>i<\/sub> = y<sub>i<\/sub> &#8211; y<sub>i<\/sub>&#8216;. This value is called the ith <strong>residual<\/strong>. That is, the difference between the actual value of y and the estimated value of y predicted by the model.<\/p>\n<p>Calculating the residual sum of squares (simply square the residual) for all the data, we have<\/p>\n<p>Residual Sum of Squares (RSS) = e<sub>1<\/sub><sup>2<\/sup> + e<sub>2<\/sub><sup>2<\/sup> + . . . + e<sub>n<\/sub><sup>2<\/sup>.<\/p>\n<p>Knowing that e<sub>i<\/sub> = y<sub>i<\/sub> &#8211; y<sub>i<\/sub>&#8216; and\u00a0\u00a0y<sub>i<\/sub>&#8216; = \u03b2<sub>0<\/sub>&#8216; +\u03b2<sub>1<\/sub>&#8216;x<sub>i<\/sub>,<\/p>\n<p>We can write the summation as:<\/p>\n<p>RSS = (y<sub>1<\/sub> &#8211; \u03b2<sub>0<\/sub>&#8216; &#8211; \u03b2<sub>1<\/sub>&#8216;x1)<sup>2<\/sup> + (y<sub>2<\/sub> &#8211;\u00a0\u03b2<sub>0<\/sub>&#8216; &#8211; \u03b2<sub>1<\/sub>&#8216;x2)<sup>2<\/sup> + . . . + (y<sub>n<\/sub> &#8211;\u00a0\u03b2<sub>0<\/sub>&#8216; &#8211; \u03b2<sub>1<\/sub>&#8216;x<sub>n<\/sub>)<sup>2<\/sup><\/p>\n<p>The Method of Least Squares chooses\u00a0\u03b2<sub>0<\/sub>&#8216; and\u00a0\u03b2<sub>1<\/sub>&#8216; so as to minimize the RSS.<\/p>\n<p>The values of\u00a0\u03b2<sub>0<\/sub>&#8216;\u00a0 and \u03b2<sub>1<\/sub>&#8216; that would make the RSS minimum is given by the following equations:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-151 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/RSS-min-1-300x71.jpg\" alt=\"\" width=\"300\" height=\"71\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-152 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/RSS-min-2.jpg\" alt=\"\" width=\"182\" height=\"50\" \/><\/p>\n<p>The formulas above are actually very simple to understand.<\/p>\n<p>x\u0304 is the sample mean of the x dataset\u00a0 \u00a0and is given by:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-154 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Sample-Meana.jpg\" alt=\"\" width=\"156\" height=\"82\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-153\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/ybar.jpg\" alt=\"\" width=\"34\" height=\"35\" \/>\u00a0is the sample mean for the y dataset and is given by:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-155 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Sample-meany.jpg\" alt=\"\" width=\"144\" height=\"75\" \/><\/p>\n<p>In the next Tutorial on the regression series, we would take a real dataset, calculate these values, calculate the regression coefficients and actually use the the coefficients to predict missing values in the dataset.<\/p>\n<p>Or I could give this as an exercise: Use the formulas to predict the missing values in Table 1.0.<\/p>\n<p>Do leave a comment if this have been informative for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial we would cover Simple Linear Regression in a very easy-to-understand way. We are assuming you don&#8217;t have much knowledge of Machine Learning &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[16],"tags":[],"class_list":["post-1867","post","type-post","status-publish","format-standard","hentry","category-machine-learning"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1867"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1867\/revisions"}],"predecessor-version":[{"id":2036,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1867\/revisions\/2036"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}