{"id":1900,"date":"2019-04-08T12:00:00","date_gmt":"2019-04-08T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/machine-learning-101-polynomial-curve-fitting\/"},"modified":"2026-07-05T03:22:40","modified_gmt":"2026-07-05T01:22:40","slug":"machine-learning-101-polynomial-curve-fitting","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/machine-learning-101-polynomial-curve-fitting\/","title":{"rendered":"Machine Learning 101 \u2013 Polynomial Curve Fitting"},"content":{"rendered":"<p>This is Lecture 6 of Machine Learning 101. We would discuss Polynomial Curve Fitting.<\/p>\n<p>Now don&#8217;t bother if the name makes it appear tough. This is simply a follow up of <a href=\"https:\/\/kindsonthegenius.com\/tempsite\/machine-learning-101-equation-for-a-line-and-regression-line\/\">Lecture 5, where we discussed Regression Line.<\/a><\/p>\n<p>So as before, we have a set of inputs<\/p>\n<p><strong>x<\/strong> = {x<sub>1<\/sub>, x<sub>2<\/sub>, . . . , x<sub>n<\/sub>}<sup>T<\/sup> where N = 6<\/p>\n<p>corresponding to a set of target variables:<\/p>\n<p><strong>t<\/strong> = {t<sub>1<\/sub>, t<sub>2<\/sub>, . . . , t<sub>N<\/sub>}<sup>T<\/sup> where N = 6<\/p>\n<p>Our objective is to find a function that relates each of the input variables to each of the target values.<\/p>\n<p>If we assume that the relationship is a linear one, then we can use the equation of a straight line given as:<\/p>\n<p>y =\u00a0\u00df<sub>0<\/sub> +\u00a0\u00df<sub>1<\/sub>x<\/p>\n<p>Then we simply calculates the coefficients\u00a0 \u00df<sub>0<\/sub>\u00a0and\u00a0 \u00df<sub>1<\/sub><\/p>\n<p>However since we don&#8217;t know the nature of this relationship, we would extend this equation to cover more options.<\/p>\n<p>So we would have it as:<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-678 size-full aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Polynormial-Curve-Fitting-Equation.jpg\" alt=\"Polynomial Curve Fitting Equation\" width=\"390\" height=\"71\" \/><\/p>\n<p>which is the same as:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-679 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Polynormial-Curve-Fitting-Equation2-300x126.jpg\" alt=\"\" width=\"205\" height=\"86\" \/><\/p>\n<p>This is similar to what we already have.\u00a0 Just like y depends on <em>x<\/em> and <em>\u00df<\/em> in the linear model, also here, y depends on <em>x<\/em> and <strong>w<\/strong>.<\/p>\n<p>M is the order of the polynomial. So if M is 1, then we have the linear model. If M is 2, then we have a quadratic function and so on.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>So what is w?<\/strong><\/p>\n<p><strong>w<\/strong> is simply the polynomial coefficients. So <em>w<sub>0<\/sub>, w<sub>1<\/sub>, . . . , w<sub>M<\/sub><\/em> are denoted by the vector <strong>w<\/strong>.<\/p>\n<p>So the problem reduces to simply determining the polynomial coefficients. Once we have it, we simple plug it into the polynomial and solve for <em>x.<\/em><\/p>\n<p>&nbsp;<\/p>\n<p><strong>How do we determine w?<\/strong><\/p>\n<p>We determine w by fitting the polynomial to the training data set.\u00a0 This is achieved by minimizing the error function that measures the difference between the function y(x,w), for any given value of w and the corresponding point in the training data set.<\/p>\n<p>To perform minimization, we need the error function. A good choice is to use the sum of squares error between the predicted values<em> y(x<sub>n<\/sub>, <strong>w<\/strong>)<\/em>\u00a0 for each training data point and the corresponding target values <em>t<sub>n<\/sub>.<\/em><\/p>\n<p>This error function is given by:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-680 size-full aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Sum-of-Squares-Error-Function.jpg\" alt=\"Sum of Squares Error Function\" width=\"426\" height=\"94\" \/><\/p>\n<p>where 1\/2 is a factor added that would be explained later.<\/p>\n<p>The value of this function would always be non-negative.\u00a0 It can also be zero, but rarely. It is zero if and only if\u00a0 the function produces exactly same output as the training set.<\/p>\n<p>In summary, we need to choose the values of <strong>w<\/strong> that would produce the least value of E(<strong>w<\/strong>).<\/p>\n<p>But another question is: <strong>How to\u00a0 we choose M,<\/strong> that is the order of the polynomial?<\/p>\n<p>I would explain this in the next lecture 6<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"pythoncode\">Python Code for Polynomial Regression<\/strong><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">numpy<\/span> <span style=\"color: #008800; font-weight: bold;\">as<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">np<\/span>\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">pandas<\/span> <span style=\"color: #008800; font-weight: bold;\">as<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">pd<\/span>\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">matplotlib.pyplot<\/span> <span style=\"color: #008800; font-weight: bold;\">as<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">plt<\/span>\n<span style=\"color: #008800; font-weight: bold;\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">sklearn.linear_model<\/span> <span style=\"color: #008800; font-weight: bold;\">import<\/span> LinearRegression\n<span style=\"color: #008800; font-weight: bold;\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">sklearn.preprocessing<\/span> <span style=\"color: #008800; font-weight: bold;\">import<\/span> PolynomialFeatures\n\n<span style=\"color: #888888;\"># Read the dataset into a dataframe<\/span>\ndf <span style=\"color: #333333;\">=<\/span> pd<span style=\"color: #333333;\">.<\/span>read_excel(<span style=\"background-color: #fff0f0;\">r'C:\\ML101\\Curve.xlsx'<\/span>)\n\n<span style=\"color: #888888;\"># Place the ranges in a variable and preprocess<\/span>\nx <span style=\"color: #333333;\">=<\/span> df[<span style=\"background-color: #fff0f0;\">'x'<\/span>]<span style=\"color: #333333;\">.<\/span>values\ny <span style=\"color: #333333;\">=<\/span> df[<span style=\"background-color: #fff0f0;\">'y'<\/span>]<span style=\"color: #333333;\">.<\/span>values\nx <span style=\"color: #333333;\">=<\/span> x<span style=\"color: #333333;\">.<\/span>reshape(<span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>)\n\n<span style=\"color: #888888;\"># Change the order here. degree is same as M<\/span>\npoly <span style=\"color: #333333;\">=<\/span> PolynomialFeatures(degree<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">11<\/span>)\n\n<span style=\"color: #888888;\"># Fit a Polynomial Curve<\/span>\nX_poly <span style=\"color: #333333;\">=<\/span> poly<span style=\"color: #333333;\">.<\/span>fit_transform(x)\npoly<span style=\"color: #333333;\">.<\/span>fit(X_poly, y)\nlinreg <span style=\"color: #333333;\">=<\/span> LinearRegression()\nlinreg<span style=\"color: #333333;\">.<\/span>fit(X_poly, y)\ny_pred <span style=\"color: #333333;\">=<\/span> linreg<span style=\"color: #333333;\">.<\/span>predict(X_poly)\n\n\n<span style=\"color: #888888;\"># Plot the curves. The regression line is in red<\/span>\nplt<span style=\"color: #333333;\">.<\/span>scatter(x,y, color<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'blue'<\/span>)\nplt<span style=\"color: #333333;\">.<\/span>plot(x, y_pred, color<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'red'<\/span>)\nplt<span style=\"color: #333333;\">.<\/span>show()\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is Lecture 6 of Machine Learning 101. We would discuss Polynomial Curve Fitting. Now don&#8217;t bother if the name makes it appear tough. This &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-1900","post","type-post","status-publish","format-standard","hentry","category-machine-learning"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1900","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=1900"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1900\/revisions"}],"predecessor-version":[{"id":2068,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1900\/revisions\/2068"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}