{"id":391,"date":"2020-04-11T19:54:21","date_gmt":"2020-04-11T14:24:21","guid":{"rendered":"https:\/\/dphi.tech\/?p=391"},"modified":"2022-11-15T15:42:52","modified_gmt":"2022-11-15T10:12:52","slug":"tutorial-on-linear-regression-using-least-squares","status":"publish","type":"post","link":"https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/","title":{"rendered":"Tutorial on Linear Regression Using Least Squares"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Linear Regression is the simplest form of machine learning out there. In this tutorial, we will see how linear regression works and implement it in Python from scratch. This article is a written version of the video tutorial embedded below. If you prefer a video tutorial, you can jump to it directly, else you can continue to read the article.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Linear Regression using Least Squares in Python - Machine Learning basics\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/kR6tBAq16ng?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><figcaption>Video tutorial on regression using Least Squares<\/figcaption><\/figure>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"864\" height=\"648\" src=\"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/example-regression-simulation.gif\" alt=\"\" class=\"wp-image-400\"\/><\/figure><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"8ae0\">Linear Regression<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">In statistics, linear regression is a linear approach to modelling the relationship between a dependent variable and one or more independent variables (To learn more about dependent and independent variables, read <a href=\"https:\/\/www.khanacademy.org\/math\/pre-algebra\/pre-algebra-equations-expressions\/pre-algebra-dependent-independent\/a\/dependent-and-independent-variables-review\">this article<\/a>). In the case of one independent variable, it is called simple linear regression. For more than one independent variable, the process is called multiple linear regression. We will be dealing with simple linear regression in this tutorial.<br>Let&nbsp;<strong>X<\/strong>&nbsp;be the independent variable and&nbsp;<strong>Y<\/strong>&nbsp;be the dependent variable. We will define a linear relationship between these two variables as follows:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"300\" height=\"100\" src=\"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/image.png\" alt=\"\" class=\"wp-image-392\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"356\" height=\"236\" src=\"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/graph-regression.gif\" alt=\"\" class=\"wp-image-393\"\/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This is the equation for a line that you might have studied in high school.&nbsp;<strong>m<\/strong>&nbsp;is the slope of the line and&nbsp;<strong>c<\/strong>&nbsp;is the y-intercept. Today we will use this equation to train our model with a given dataset and predict the value of&nbsp;<strong>Y<\/strong>&nbsp;for any given value of&nbsp;<strong>X<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now the challenge here is to determine the optimal values for&nbsp;<strong>m<\/strong>&nbsp;and&nbsp;<strong>c<\/strong>, that would give a minimum error for the given dataset. We will be doing this by using the&nbsp;<strong>Least Squares<\/strong>&nbsp;method.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"7053\">Finding the Error<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">So to minimize the error we need a way to calculate the error in the first place. A&nbsp;<strong>loss function<\/strong>&nbsp;in machine learning is simply a measure of how different the predicted value is from the actual value.<br>In this tutorial, we will be using the&nbsp;<strong>Quadratic Loss Function<\/strong>&nbsp;to calculate the loss or error in our regression model. It is defined as mentioned below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"100\" src=\"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/loss-function-least-squares-regression.png\" alt=\"\" class=\"wp-image-394\"\/><\/figure><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"864\" height=\"648\" src=\"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/plot-loss-function-showing-difference.jpg\" alt=\"\" class=\"wp-image-395\" srcset=\"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/plot-loss-function-showing-difference.jpg 864w, https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/plot-loss-function-showing-difference-300x225.jpg 300w, https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/plot-loss-function-showing-difference-768x576.jpg 768w\" sizes=\"(max-width: 864px) 100vw, 864px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We are squaring it because, for the points below the regression line&nbsp;<strong>y \u2014 p<\/strong>&nbsp;(red line in the above graph), the difference of y and p will be negative whereas the difference will be positive for the values above the regression line&#8230; Summation of these differences (negative values and positives values) might nullify the error or not give a true picture of the total error of the model. Hence, we sum the square of the difference of actual value (y) and predicted value (p) while calculating the loss function.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"1a8b\">Least Squares method<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Now that we have determined the loss function, the only thing left to do is minimize it. This is done by finding the partial derivative of&nbsp;<strong>L<\/strong>, equating it to 0 and then finding an expression for&nbsp;<strong>m<\/strong>&nbsp;and&nbsp;<strong>c<\/strong>. After we do the math, we are left with these equations:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"330\" height=\"172\" src=\"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/least-square-estimate-function.png\" alt=\"\" class=\"wp-image-396\" srcset=\"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/least-square-estimate-function.png 330w, https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/least-square-estimate-function-300x156.png 300w\" sizes=\"(max-width: 330px) 100vw, 330px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Here x\u0305 is the mean of all the values in the input&nbsp;<strong>X<\/strong>&nbsp;and \u0233 is the mean of all the values in the desired output&nbsp;<strong>Y<\/strong>. This is the Least Squares method. Now we will implement this in python and make predictions.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"3a91\">Implementing the Model<\/h1>\n\n\n\n<script src=\"https:\/\/gist.github.com\/chasinginfinity\/df9acae4991f334871a641745395bc9e.js\"><\/script>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"713\" height=\"524\" src=\"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/graph-ls.png\" alt=\"\" class=\"wp-image-397\" srcset=\"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/graph-ls.png 713w, https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/graph-ls-300x220.png 300w\" sizes=\"(max-width: 713px) 100vw, 713px\" \/><\/figure><\/div>\n\n\n\n<script src=\"https:\/\/gist.github.com\/chasinginfinity\/8316d0e7f8e3e16bbaffa2eb2b8f66a6.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\">1.287357370010931 9.908606190326509<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/chasinginfinity\/f5e5831c852821108a22ea75aa729ac1.js\"><\/script>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"713\" height=\"524\" src=\"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/graph-ls-2.png\" alt=\"\" class=\"wp-image-398\" srcset=\"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/graph-ls-2.png 713w, https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/graph-ls-2-300x220.png 300w\" sizes=\"(max-width: 713px) 100vw, 713px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">There won&#8217;t be much accuracy because we are simply taking a straight line and forcing it to fit into the given data in the best possible way. But you can use this to make simple predictions or get an idea about the magnitude\/range of the real value. Also, this is a good first step for beginners in Machine Learning.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Find the dataset and the code used in the tutorial here:&nbsp;<\/em><a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/chasinginfinity\/ml-from-scratch\/tree\/master\/01%20Linear%20Regression%20using%20Least%20Squares\" target=\"_blank\"><em>https:\/\/github.com\/chasinginfinity\/ml-from-scratch\/tree\/master\/01%20Linear%20Regression%20using%20Least%20Squares<\/em><\/a><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Got questions ? Need help ? Contact me!<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Email: adarsh1021@gmail.com<br>LinkedIn: https:\/\/www.linkedin.com\/in\/adarsh-menon-739573146\/<br>Twitter: https:\/\/twitter.com\/adarsh_menon_<br>Instagram: https:\/\/www.instagram.com\/adarsh_menon_\/<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This article was originally published on <a href=\"https:\/\/towardsdatascience.com\/linear-regression-using-least-squares-a4c3456e8570\">towardsdatascience.com<\/a> and kindly contributed to AI Planet (formerly DPhi) to spread the knowledge.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em>Become a guide. Become a mentor.<\/em><\/strong><br><em>We at AI Planet (formerly DPhi), welcome you to share your experience in data science \u2013 be it your learning journey, experience while participating in Data Science Challenges, data science\u00a0projects, tutorials and anything that is related to Data Science. Your learnings could help a large number of aspiring data scientists!\u00a0Interested? Submit\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/docs.google.com\/forms\/d\/e\/1FAIpQLSdOMt4yfkgkWxWIkvQMuuJiwsc21mCV8IxPqGLhj1uZ6oLJ9Q\/viewform\" target=\"_blank\">here<\/a>.\u00a0<\/em><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Linear Regression is the simplest form of machine learning out there. In this tutorial, we will see how linear regression works and implement it in Python from scratch. This article is a written version of the video tutorial embedded below. If you prefer a video tutorial, you can jump to it directly, else you can [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":408,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-391","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-datascience"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Tutorial on Linear Regression Using Least Squares - AI Planet<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tutorial on Linear Regression Using Least Squares - AI Planet\" \/>\n<meta property=\"og:description\" content=\"Linear Regression is the simplest form of machine learning out there. In this tutorial, we will see how linear regression works and implement it in Python from scratch. This article is a written version of the video tutorial embedded below. If you prefer a video tutorial, you can jump to it directly, else you can [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/\" \/>\n<meta property=\"og:site_name\" content=\"AI Planet\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/aiplanet\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-11T14:24:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-15T10:12:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/least-squares.png\" \/>\n\t<meta property=\"og:image:width\" content=\"531\" \/>\n\t<meta property=\"og:image:height\" content=\"216\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Adarsh Menon\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Adarsh Menon\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-linear-regression-using-least-squares\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-linear-regression-using-least-squares\\\/\"},\"author\":{\"name\":\"Adarsh Menon\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#\\\/schema\\\/person\\\/b121996cdbe68ef54fbc4683ff368a27\"},\"headline\":\"Tutorial on Linear Regression Using Least Squares\",\"datePublished\":\"2020-04-11T14:24:21+00:00\",\"dateModified\":\"2022-11-15T10:12:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-linear-regression-using-least-squares\\\/\"},\"wordCount\":744,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-linear-regression-using-least-squares\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/least-squares.png\",\"articleSection\":[\"datascience\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-linear-regression-using-least-squares\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-linear-regression-using-least-squares\\\/\",\"url\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-linear-regression-using-least-squares\\\/\",\"name\":\"Tutorial on Linear Regression Using Least Squares - AI Planet\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-linear-regression-using-least-squares\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-linear-regression-using-least-squares\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/least-squares.png\",\"datePublished\":\"2020-04-11T14:24:21+00:00\",\"dateModified\":\"2022-11-15T10:12:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-linear-regression-using-least-squares\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-linear-regression-using-least-squares\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-linear-regression-using-least-squares\\\/#primaryimage\",\"url\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/least-squares.png\",\"contentUrl\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/least-squares.png\",\"width\":531,\"height\":216},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-linear-regression-using-least-squares\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorial on Linear Regression Using Least Squares\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/\",\"name\":\"AI Planet, Formerly DPhi\",\"description\":\"AI Planet (formerly DPhi) is a global ecosystem that aims to educate and empower individuals and organizations to build and utilize artificial intelligence (AI). With a community of over 300,000 AI specialists and enthusiasts, we offer a range of products and have helped drive innovative solutions for various industries. Our ultimate goal is to make AI education accessible to everyone and use AI to tackle important business and social challenges.\",\"publisher\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#organization\",\"name\":\"AI Planet, Formerly DPhi\",\"url\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/Size48-Large-1.png\",\"contentUrl\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/Size48-Large-1.png\",\"width\":48,\"height\":48,\"caption\":\"AI Planet, Formerly DPhi\"},\"image\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/aiplanet\\\/\",\"https:\\\/\\\/www.instagram.com\\\/aiplanet_community\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/aiplanet\\\/\",\"https:\\\/\\\/www.youtube.com\\\/@aiplanet\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#\\\/schema\\\/person\\\/b121996cdbe68ef54fbc4683ff368a27\",\"name\":\"Adarsh Menon\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c60cd7eab96e2afd47ba36714163d1f75f4f470b5a5da376a761e1a0f74b12bf?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c60cd7eab96e2afd47ba36714163d1f75f4f470b5a5da376a761e1a0f74b12bf?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c60cd7eab96e2afd47ba36714163d1f75f4f470b5a5da376a761e1a0f74b12bf?s=96&d=mm&r=g\",\"caption\":\"Adarsh Menon\"},\"description\":\"Adarsh is a tech &amp; data science enthusiast. In his own words, \u201cI make websites and teach machines to predict stuff. I also make YouTube videos \u2014 https:\\\/\\\/www.youtube.com\\\/adarshmenon\\\"\",\"url\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/author\\\/adarsh_menon\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tutorial on Linear Regression Using Least Squares - AI Planet","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:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/","og_locale":"en_US","og_type":"article","og_title":"Tutorial on Linear Regression Using Least Squares - AI Planet","og_description":"Linear Regression is the simplest form of machine learning out there. In this tutorial, we will see how linear regression works and implement it in Python from scratch. This article is a written version of the video tutorial embedded below. If you prefer a video tutorial, you can jump to it directly, else you can [&hellip;]","og_url":"https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/","og_site_name":"AI Planet","article_publisher":"https:\/\/www.facebook.com\/aiplanet\/","article_published_time":"2020-04-11T14:24:21+00:00","article_modified_time":"2022-11-15T10:12:52+00:00","og_image":[{"width":531,"height":216,"url":"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/least-squares.png","type":"image\/png"}],"author":"Adarsh Menon","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Adarsh Menon","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/#article","isPartOf":{"@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/"},"author":{"name":"Adarsh Menon","@id":"https:\/\/aiplanet.com\/blog\/#\/schema\/person\/b121996cdbe68ef54fbc4683ff368a27"},"headline":"Tutorial on Linear Regression Using Least Squares","datePublished":"2020-04-11T14:24:21+00:00","dateModified":"2022-11-15T10:12:52+00:00","mainEntityOfPage":{"@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/"},"wordCount":744,"commentCount":2,"publisher":{"@id":"https:\/\/aiplanet.com\/blog\/#organization"},"image":{"@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/#primaryimage"},"thumbnailUrl":"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/least-squares.png","articleSection":["datascience"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/","url":"https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/","name":"Tutorial on Linear Regression Using Least Squares - AI Planet","isPartOf":{"@id":"https:\/\/aiplanet.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/#primaryimage"},"image":{"@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/#primaryimage"},"thumbnailUrl":"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/least-squares.png","datePublished":"2020-04-11T14:24:21+00:00","dateModified":"2022-11-15T10:12:52+00:00","breadcrumb":{"@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/#primaryimage","url":"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/least-squares.png","contentUrl":"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/least-squares.png","width":531,"height":216},{"@type":"BreadcrumbList","@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-linear-regression-using-least-squares\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/aiplanet.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Tutorial on Linear Regression Using Least Squares"}]},{"@type":"WebSite","@id":"https:\/\/aiplanet.com\/blog\/#website","url":"https:\/\/aiplanet.com\/blog\/","name":"AI Planet, Formerly DPhi","description":"AI Planet (formerly DPhi) is a global ecosystem that aims to educate and empower individuals and organizations to build and utilize artificial intelligence (AI). With a community of over 300,000 AI specialists and enthusiasts, we offer a range of products and have helped drive innovative solutions for various industries. Our ultimate goal is to make AI education accessible to everyone and use AI to tackle important business and social challenges.","publisher":{"@id":"https:\/\/aiplanet.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/aiplanet.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/aiplanet.com\/blog\/#organization","name":"AI Planet, Formerly DPhi","url":"https:\/\/aiplanet.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/aiplanet.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2022\/11\/Size48-Large-1.png","contentUrl":"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2022\/11\/Size48-Large-1.png","width":48,"height":48,"caption":"AI Planet, Formerly DPhi"},"image":{"@id":"https:\/\/aiplanet.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/aiplanet\/","https:\/\/www.instagram.com\/aiplanet_community\/","https:\/\/www.linkedin.com\/company\/aiplanet\/","https:\/\/www.youtube.com\/@aiplanet"]},{"@type":"Person","@id":"https:\/\/aiplanet.com\/blog\/#\/schema\/person\/b121996cdbe68ef54fbc4683ff368a27","name":"Adarsh Menon","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c60cd7eab96e2afd47ba36714163d1f75f4f470b5a5da376a761e1a0f74b12bf?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c60cd7eab96e2afd47ba36714163d1f75f4f470b5a5da376a761e1a0f74b12bf?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c60cd7eab96e2afd47ba36714163d1f75f4f470b5a5da376a761e1a0f74b12bf?s=96&d=mm&r=g","caption":"Adarsh Menon"},"description":"Adarsh is a tech &amp; data science enthusiast. In his own words, \u201cI make websites and teach machines to predict stuff. I also make YouTube videos \u2014 https:\/\/www.youtube.com\/adarshmenon\"","url":"https:\/\/aiplanet.com\/blog\/author\/adarsh_menon\/"}]}},"_links":{"self":[{"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/posts\/391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/comments?post=391"}],"version-history":[{"count":6,"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/posts\/391\/revisions"}],"predecessor-version":[{"id":4967,"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/posts\/391\/revisions\/4967"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/media\/408"}],"wp:attachment":[{"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/media?parent=391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/categories?post=391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/tags?post=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}