{"id":1996,"date":"2019-09-02T06:00:05","date_gmt":"2019-09-02T00:30:05","guid":{"rendered":"https:\/\/debuggercafe.com\/?p=1996"},"modified":"2023-01-08T16:29:47","modified_gmt":"2023-01-08T10:59:47","slug":"implement-simple-linear-regression-from-scratch","status":"publish","type":"post","link":"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/","title":{"rendered":"Implement Simple Linear Regression from Scratch"},"content":{"rendered":"\n<p>Linear Regression is one of the oldest statistical learning methods that is still used in Machine Learning. Also, it is quite easy for beginners in machine learning to get a grasp on the linear regression learning technique. In this article, we will be implementing Simple Linear Regression from Scratch using Python. <\/p>\n\n\n\n<p><em><strong>Note: <\/strong>If you want to get a bit more familiarity with Linear Regression, then you can go through<\/em><strong><em> <\/em><\/strong><a rel=\"noreferrer noopener\" aria-label=\"this article (opens in a new tab)\" href=\"https:\/\/debuggercafe.com\/linear-regression-in-machine-learning\/\" target=\"_blank\"><strong><em>this article<\/em><\/strong><\/a> <em>first. It will teach you all the basics, including the mathematics behind linear regression, and how it is actually used in machine learning.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Linear Regression Equation<\/h2>\n\n\n\n<p>Before diving into the coding details, first, let&#8217;s know a bit more about simple linear regression.<\/p>\n\n\n\n<p>In simple linear regression, we have only one feature variable and one target variable. Let the feature variable be <strong><em>x<\/em><\/strong> and the output variable be <strong><em>y<\/em><\/strong>. Our job is to find the value of a new <em>y<\/em> when we have the value of a new <em>x<\/em>. <\/p>\n\n\n\n<p>The equation for the above operation is,<\/p>\n\n\n<p>$$<br \/>\ny = \\beta_0 + \\beta_1x<br \/>\n$$<\/p>\n\n\n\n<p>where \\(\\beta_0\\) is the intercept parameter and \\(\\beta_1\\) is the slope parameter. <\/p>\n\n\n\n<p>So, we will have to build a linear model by using the features <strong><em>x<\/em><\/strong> and target <strong><em>y<\/em><\/strong> that should be a straight line plot on a graph. For that, we have to optimize the parameters \\(\\beta_0\\) and \\(\\beta_1\\) using a cost function (also known as <em>objective function<\/em>).<\/p>\n\n\n\n<p>So, fire up your favorite IDE or Jupyter Notebook and follow along.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Scikit-Learn Make Regression<\/h2>\n\n\n\n<p>As this post is meant to be an introductory one, so we will be using Scikit-Learn&#8217;s <code>make_regression<\/code> module to generate random regression data.<\/p>\n\n\n\n<p>Let&#8217;s get our data from Scikit-Learn first.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.datasets import make_regression<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">x, y = make_regression(n_samples=200, n_features=1, noise=20, bias=0)<\/pre>\n\n\n\n<p>Now, we have 1 feature and 200 sample values in total. <code>x<\/code> holds all the feature values and <code>y<\/code> holds all the target values.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def plot_fig(x, y, b0, b1, name):\n    plt.figure()\n\n    plot_x = np.linspace(min(x), max(x), 200)\n    plot_y = b0 + b1 * plot_x\n\n    plt.scatter(x, y)\n\n    plt.plot(plot_x, plot_y, color='red')\n\n    plt.savefig(name)<\/pre>\n\n\n\n<p>The above function defines the scatter plot for the feature values and their corresponding targets. It will also plot our linear model when we give it the slope and intercept parameters. It takes four arguments, the data points <code>x<\/code>, the targets <code>y<\/code>, and both of the simple linear regression parameters.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Initial Plotting<\/h2>\n\n\n\n<p>We do not have any optimal values for <code>b0<\/code> and <code>b1<\/code> till now. We need to optimize both these values so that we will have the best fit for the data in hand.<\/p>\n\n\n\n<p>We can randomly initialize the values for <code>b0<\/code> and <code>b1<\/code> so that we can visualize the initial linear model before any optimization. Later we will be optimizing the values to get the best linear model that we can. The code snippet below initializes the parameters <code>b0<\/code> and <code>b1<\/code> with random values.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">b0= np.random.rand()\nb1 = np.random.rand()<\/pre>\n\n\n\n<p>Now, to visualize the initial linear model and the data points, let&#8217;s call the <code>plot_fig()<\/code> function.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">plot_fig(x, y, b0, b1)<\/pre>\n\n\n\n<p>The above code gives us the following plot as output:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"432\" height=\"288\" src=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/08\/initial-plot.png\" alt=\"Image of linear plot without any learning\" class=\"wp-image-2004\" srcset=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/08\/initial-plot.png 432w, https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/08\/initial-plot-300x200.png 300w\" sizes=\"auto, (max-width: 432px) 100vw, 432px\" \/><figcaption>Initial Plot<\/figcaption><\/figure><\/div>\n\n\n\n<p>We can clearly see that the linear regression line is not at all a good fit, at least for now. Let&#8217;s move ahead and try to get better results.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Define the Cost Function<\/h2>\n\n\n\n<p>In almost all types of learning, to make the algorithm learn from data, we need to minimize some sort of cost function. This cost function is otherwise known as an objective function. In the case of linear regression, we are going to optimize the Mean Square Error (MSE). The following is the mathematical expression of MSE.<\/p>\n\n\n<p>$$<br \/>\nMSE =  \\frac{1}{N} \\sum_{i=1}^{n} (y_i &#8211; (m x_i + b))^2<br \/>\n$$<\/p>\n\n\n\n<p>By using the above function we will optimize the slope and intercept parameters. In some books and articles, you may encounter the word <em>&#8216;weight&#8217;<\/em> for the slope (\\(m\\)) parameter and <em>&#8216;bias&#8217;<\/em> for the intercept parameter (\\(m\\) ). This is a common practice in machine learning terms.<\/p>\n\n\n\n<p>So, what do we do to minimize the cost function? We use the <strong><a rel=\"noreferrer noopener\" aria-label=\"Gradient Descent (opens in a new tab)\" href=\"https:\/\/en.wikipedia.org\/wiki\/Gradient_descent\" target=\"_blank\">Gradient Descent<\/a><\/strong> method. As we have to optimize both the parameters, we will have to use partial derivative for that. Also, the cost functions will be dependent on two parameters now.<\/p>\n\n\n<p>$$<br \/>\nf(m,b) =  \\frac{1}{N} \\sum_{i=1}^{n} (y_i \u2013 (mx_i + b))^2<br \/>\n$$<\/p>\n\n\n\n<p>To get into the mathematical aspects, you see refer to <strong><a rel=\"noreferrer noopener\" aria-label=\"this amazing article (opens in a new tab)\" href=\"https:\/\/mccormickml.com\/2014\/03\/04\/gradient-descent-derivation\/\" target=\"_blank\">this amazing article<\/a><\/strong>.<\/p>\n\n\n\n<p>We are now ready to define the code for implementing the cost function. The following code block does so.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># cost function\ndef cost_function(x, y, b1, b0):\n    num_values = len(x)\n    total_error = 0.0\n    for i in range(num_values):\n        total_error += (y[i] - (b1*x[i] + b0))**2\n    return total_error \/ num_values<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Update the Weights<\/h2>\n\n\n\n<p>Okay, to optimize the parameters, we need to update them, and use those updated values in each iteration. In the next function, we will update the weights. But first, we calculate the partial derivatives. Then we need to update <code>b0<\/code> and <code>b1<\/code>. We will do so by subtracting the partial derivative values from <code>b0<\/code> and <code>b1<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def update_weights(x, y, b1, b0, learning_rate):\n    b1_deriv = 0\n    b0_deriv = 0\n    num_values = len(x)\n\n    for i in range(num_values):\n        # Calculate partial derivatives\n        b1_deriv += -2*x[i] * (y[i] - (b1*x[i] + b0))\n\n        b0_deriv += -2*(y[i] - (b1*x[i] + b0))\n\n    # update the weights\n    b1 -= (b1_deriv \/ num_values) * learning_rate\n    b0 -= (b0_deriv \/ num_values) * learning_rate\n\n    return b1, b0<\/pre>\n\n\n\n<p><code>update_weights()<\/code> function accepts five arguments. We pass the data points <code>x<\/code> and targets <code>y<\/code> as usual along with <code>b0<\/code> and <code>b1<\/code>. The fifth argument is the learning rate. Learning rate specifies how quickly our algorithm will converge to an optimal solution (find a solution).<\/p>\n\n\n\n<p>Learning rate is a very important parameter. We do not want it to be too high, as it may miss the optimal solution because of very large steps. We also do not want it to be too low. In that case, the steps will be more and the convergence may take a lot of time to happen.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Training Function<\/h2>\n\n\n\n<p>Finally, we define the function to train the algorithm. In addition to the five arguments that <code>update_weights()<\/code> function accepts, it takes one more argument. That is the number of iterations. The number of iterations defines how many times our algorithms will be trained on the data fully. For a low learning rate, number iterations will be high and for a high learning rate, it will be less.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def train(x, y, b1, b0, learning_rate, iterations):\n    costs = []\n\n    for i in range(iterations):\n        b1, b0 = update_weights(x, y, b1, b0, learning_rate)\n\n        # calculate the cost\n        cost = cost_function(x, y, b1, b0)\n        costs.append(cost)\n\n        # plot the linear model\n        if i % 50 == 0:\n            plot_fig(x, y, b0, b1, name='linear-epoch-'+str(i))\n\n    return b1, b0, costs<\/pre>\n\n\n\n<p>In the end, we just call the <code>train()<\/code> function. We will run the algorithm for 200 epochs and plot the linear model every 50 epochs to get a good look at the learning process. Also, the learning rate is going to be 0.01.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">train(x, y, b1, b0, 0.01, 200)<\/pre>\n\n\n\n<p>We get four plots for the linear model while training process. The following four are those plots.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"432\" height=\"288\" src=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/08\/linear-epoch-0.png\" alt=\"Image for epoch 0 linear model\" class=\"wp-image-2012\" srcset=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/08\/linear-epoch-0.png 432w, https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/08\/linear-epoch-0-300x200.png 300w\" sizes=\"auto, (max-width: 432px) 100vw, 432px\" \/><figcaption>Epoch 0<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"432\" height=\"288\" src=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/08\/linear-epoch-50.png\" alt=\"Image for epoch 50 linear model\" class=\"wp-image-2013\" srcset=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/08\/linear-epoch-50.png 432w, https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/08\/linear-epoch-50-300x200.png 300w\" sizes=\"auto, (max-width: 432px) 100vw, 432px\" \/><figcaption>Epoch 50<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"432\" height=\"288\" src=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/08\/linear-epoch-100.png\" alt=\"Image for epoch 100 linear model\" class=\"wp-image-2014\" srcset=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/08\/linear-epoch-100.png 432w, https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/08\/linear-epoch-100-300x200.png 300w\" sizes=\"auto, (max-width: 432px) 100vw, 432px\" \/><figcaption>Epoch 100<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"432\" height=\"288\" src=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/08\/linear-epoch-150.png\" alt=\"Image for epoch 150 linear model\" class=\"wp-image-2015\" srcset=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/08\/linear-epoch-150.png 432w, https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/08\/linear-epoch-150-300x200.png 300w\" sizes=\"auto, (max-width: 432px) 100vw, 432px\" \/><figcaption>Epoch 150<\/figcaption><\/figure><\/div>\n\n\n\n<p>By the end of 200 epochs our model seems to do pretty well. You can always try to play around with different learning rates and epochs. This will give you better insights on how these affect the linear regression model.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary and Conclusion<\/h2>\n\n\n\n<p>In this article, you learned how to implement simple linear regression from scratch. Obviously, you do not need to write such code while doing practical industry-oriented machine learning. There are amazing libraries like <strong><a rel=\"noreferrer noopener\" aria-label=\"Scikit-Learn (opens in a new tab)\" href=\"https:\/\/scikit-learn.org\/stable\/\" target=\"_blank\">Scikit-Learn<\/a><\/strong> which help us to carry these out with much ease.<\/p>\n\n\n\n<p>Subscribe to the website to get timely article information in your inbox. You can also follow me <strong><a rel=\"noreferrer noopener\" aria-label=\"Twitter (opens in a new tab)\" href=\"https:\/\/twitter.com\/SovitRath5\" target=\"_blank\">Twitter<\/a><\/strong> and <strong><a rel=\"noreferrer noopener\" aria-label=\"LinkedIn (opens in a new tab)\" href=\"https:\/\/www.linkedin.com\/in\/sovit-rath\/\" target=\"_blank\">LinkedIn<\/a><\/strong> to get regular updates about my articles.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to implement Linear Regression for Machine Learning from scratch using Python.<\/p>\n","protected":false},"author":1,"featured_media":2025,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47,17],"tags":[50,24,78],"class_list":["post-1996","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","category-machine-learning","tag-data-science","tag-machine-learning","tag-statistical-methods-and-analysis"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Implement Simple Linear Regression from Scratch<\/title>\n<meta name=\"description\" content=\"Learn how to implement Linear Regression for Machine Learning from scratch using Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Implement Simple Linear Regression from Scratch\" \/>\n<meta property=\"og:description\" content=\"Learn how to implement Linear Regression for Machine Learning from scratch using Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/\" \/>\n<meta property=\"og:site_name\" content=\"DebuggerCafe\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/profile.php?id=100013731104496\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-02T00:30:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-08T10:59:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/09\/Implement-Simple-Linear-Regression-from-Scratch-using-Python-e1567307619886.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Sovit Ranjan Rath\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SovitRath5\" \/>\n<meta name=\"twitter:site\" content=\"@SovitRath5\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sovit Ranjan Rath\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/\"},\"author\":{\"name\":\"Sovit Ranjan Rath\",\"@id\":\"https:\/\/debuggercafe.com\/#\/schema\/person\/27719b14d930bd4a88ade40d18b0a752\"},\"headline\":\"Implement Simple Linear Regression from Scratch\",\"datePublished\":\"2019-09-02T00:30:05+00:00\",\"dateModified\":\"2023-01-08T10:59:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/\"},\"wordCount\":1102,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/09\/Implement-Simple-Linear-Regression-from-Scratch-using-Python-e1567307619886.png\",\"keywords\":[\"Data Science\",\"Machine Learning\",\"Statistical Methods and Analysis\"],\"articleSection\":[\"Data Science\",\"Machine Learning\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/\",\"url\":\"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/\",\"name\":\"Implement Simple Linear Regression from Scratch\",\"isPartOf\":{\"@id\":\"https:\/\/debuggercafe.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/09\/Implement-Simple-Linear-Regression-from-Scratch-using-Python-e1567307619886.png\",\"datePublished\":\"2019-09-02T00:30:05+00:00\",\"dateModified\":\"2023-01-08T10:59:47+00:00\",\"author\":{\"@id\":\"https:\/\/debuggercafe.com\/#\/schema\/person\/27719b14d930bd4a88ade40d18b0a752\"},\"description\":\"Learn how to implement Linear Regression for Machine Learning from scratch using Python.\",\"breadcrumb\":{\"@id\":\"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/#primaryimage\",\"url\":\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/09\/Implement-Simple-Linear-Regression-from-Scratch-using-Python-e1567307619886.png\",\"contentUrl\":\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/09\/Implement-Simple-Linear-Regression-from-Scratch-using-Python-e1567307619886.png\",\"width\":1200,\"height\":675,\"caption\":\"Banner Image\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/debuggercafe.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Implement Simple Linear Regression from Scratch\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/debuggercafe.com\/#website\",\"url\":\"https:\/\/debuggercafe.com\/\",\"name\":\"DebuggerCafe\",\"description\":\"Machine Learning and Deep Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/debuggercafe.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/debuggercafe.com\/#\/schema\/person\/27719b14d930bd4a88ade40d18b0a752\",\"name\":\"Sovit Ranjan Rath\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/debuggercafe.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f71ca13ec56d630e7d8045e8b846396068791aa204936c3d74d721c6dd2b4d3c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f71ca13ec56d630e7d8045e8b846396068791aa204936c3d74d721c6dd2b4d3c?s=96&d=mm&r=g\",\"caption\":\"Sovit Ranjan Rath\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Implement Simple Linear Regression from Scratch","description":"Learn how to implement Linear Regression for Machine Learning from scratch using Python.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/","og_locale":"en_US","og_type":"article","og_title":"Implement Simple Linear Regression from Scratch","og_description":"Learn how to implement Linear Regression for Machine Learning from scratch using Python.","og_url":"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/","og_site_name":"DebuggerCafe","article_publisher":"https:\/\/www.facebook.com\/profile.php?id=100013731104496","article_published_time":"2019-09-02T00:30:05+00:00","article_modified_time":"2023-01-08T10:59:47+00:00","og_image":[{"width":1200,"height":675,"url":"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/09\/Implement-Simple-Linear-Regression-from-Scratch-using-Python-e1567307619886.png","type":"image\/png"}],"author":"Sovit Ranjan Rath","twitter_card":"summary_large_image","twitter_creator":"@SovitRath5","twitter_site":"@SovitRath5","twitter_misc":{"Written by":"Sovit Ranjan Rath","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/#article","isPartOf":{"@id":"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/"},"author":{"name":"Sovit Ranjan Rath","@id":"https:\/\/debuggercafe.com\/#\/schema\/person\/27719b14d930bd4a88ade40d18b0a752"},"headline":"Implement Simple Linear Regression from Scratch","datePublished":"2019-09-02T00:30:05+00:00","dateModified":"2023-01-08T10:59:47+00:00","mainEntityOfPage":{"@id":"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/"},"wordCount":1102,"commentCount":0,"image":{"@id":"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/#primaryimage"},"thumbnailUrl":"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/09\/Implement-Simple-Linear-Regression-from-Scratch-using-Python-e1567307619886.png","keywords":["Data Science","Machine Learning","Statistical Methods and Analysis"],"articleSection":["Data Science","Machine Learning"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/","url":"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/","name":"Implement Simple Linear Regression from Scratch","isPartOf":{"@id":"https:\/\/debuggercafe.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/#primaryimage"},"image":{"@id":"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/#primaryimage"},"thumbnailUrl":"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/09\/Implement-Simple-Linear-Regression-from-Scratch-using-Python-e1567307619886.png","datePublished":"2019-09-02T00:30:05+00:00","dateModified":"2023-01-08T10:59:47+00:00","author":{"@id":"https:\/\/debuggercafe.com\/#\/schema\/person\/27719b14d930bd4a88ade40d18b0a752"},"description":"Learn how to implement Linear Regression for Machine Learning from scratch using Python.","breadcrumb":{"@id":"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/#primaryimage","url":"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/09\/Implement-Simple-Linear-Regression-from-Scratch-using-Python-e1567307619886.png","contentUrl":"https:\/\/debuggercafe.com\/wp-content\/uploads\/2019\/09\/Implement-Simple-Linear-Regression-from-Scratch-using-Python-e1567307619886.png","width":1200,"height":675,"caption":"Banner Image"},{"@type":"BreadcrumbList","@id":"https:\/\/debuggercafe.com\/implement-simple-linear-regression-from-scratch\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/debuggercafe.com\/"},{"@type":"ListItem","position":2,"name":"Implement Simple Linear Regression from Scratch"}]},{"@type":"WebSite","@id":"https:\/\/debuggercafe.com\/#website","url":"https:\/\/debuggercafe.com\/","name":"DebuggerCafe","description":"Machine Learning and Deep Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/debuggercafe.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/debuggercafe.com\/#\/schema\/person\/27719b14d930bd4a88ade40d18b0a752","name":"Sovit Ranjan Rath","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/debuggercafe.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f71ca13ec56d630e7d8045e8b846396068791aa204936c3d74d721c6dd2b4d3c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f71ca13ec56d630e7d8045e8b846396068791aa204936c3d74d721c6dd2b4d3c?s=96&d=mm&r=g","caption":"Sovit Ranjan Rath"}}]}},"_links":{"self":[{"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/posts\/1996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/comments?post=1996"}],"version-history":[{"count":25,"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/posts\/1996\/revisions"}],"predecessor-version":[{"id":28544,"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/posts\/1996\/revisions\/28544"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/media\/2025"}],"wp:attachment":[{"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/media?parent=1996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/categories?post=1996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/tags?post=1996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}