{"id":1901,"date":"2019-04-11T12:00:00","date_gmt":"2019-04-11T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/machine-learning-101-overfitting-and-underfitting\/"},"modified":"2026-07-05T03:22:43","modified_gmt":"2026-07-05T01:22:43","slug":"machine-learning-101-overfitting-and-underfitting","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/machine-learning-101-overfitting-and-underfitting\/","title":{"rendered":"Machine Learning 101 \u2013 Overfitting and Underfitting"},"content":{"rendered":"<p>Remember that in the <a href=\"https:\/\/kindsonthegenius.com\/tempsite\/machine-learning-101-polynomial-curve-fitting\/\">previous lecture (Lecture 6),<\/a> we\u00a0 discuss polynomial curve fitting. We kind of saw that the relationship in a dataset can be represented using a polynomial of this form:<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-688 \" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Polynomial-Curve-Fitting-Equation3.jpg\" alt=\"Polynomial Curve Fitting Equation\" width=\"487\" height=\"56\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Just assume that this is any polynomial of order, M. We want to understand want happens when the order gradually increases from 0 to say 13.<\/p>\n<p>Also assume that the w terms are constant.<\/p>\n<p>Remember our goal is to ensure that this polynomial fits the dataset. That is, after plotting the dataset as a scatter plot, then we fit this polynomial through it. We&#8217;ll then cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">How M Relates to E(Error)<\/a><\/li>\n<li><a href=\"#t2\">If M is too Low<\/a><\/li>\n<li><a href=\"#t3\">If M is too High<\/a><\/li>\n<li><a href=\"#t4\">The Trade-off<\/a><\/li>\n<li><a href=\"#t5\">Hands-on in Python<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. How M relates to E<\/strong><\/h4>\n<p>Also recall that we would like to minimize the error term E(x, w*). This is so that the difference between predicted values and the actual values is very small.<\/p>\n<p>Now as M (the order of the polynomial) increases, the error, E decreases. This also means that the model complexity increases since we have higher order polynomial.<\/p>\n<p>Similarly, when M is low, the complexity of the model reduces, meaning that the model becomes kind of simple. (I recommend you take some time to understand this relationship).<\/p>\n<p>So it then appear that to reduce the error E to the barest minimum, or even zero, we simply keep increasing M. But there a problem.<\/p>\n<p>Let&#8217;s now see what happens when M is too low and when M is too high.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. If M is very low (Underfitting)<\/strong><\/h4>\n<p>As mentioned, if M is very low, then\u00a0 polynomial is will not be able to properly model the relationship between the variable.\u00a0 So the following will be true:<\/p>\n<ul>\n<li>the model (polynomial) will not give a good fit to the dataset<\/li>\n<li>the model is simple and easy to manipulate<\/li>\n<li>the error is high<\/li>\n<\/ul>\n<p>You can see how the polynomial looks for values of M =1 and M = 3 in the figure below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-689 size-full aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Underfitting-and-Overfitting-1-3.jpg\" alt=\"Underfitting and Overfitting 1 3\" width=\"979\" height=\"372\" \/><\/p>\n<p>You can see that the model does not\u00a0 fit the given dataset properly. This problem is called <strong><em>underfitting<\/em><\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. If M is very high (Overfitting)<\/strong><\/h4>\n<p>On the other hand, if M is very high, then the following will be true:<\/p>\n<ul>\n<li>the model will fit the dataset very closely or even match every point in the dataset<\/li>\n<li>the model&#8217;s complexity will increase<\/li>\n<li>the error also becomes high (it decreases, then increases again)<\/li>\n<\/ul>\n<p>I have generated the curve for values of M = 11 and M = 12 as shown below.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-691 size-full aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Underfitting-and-Overfitting-11-12.jpg\" alt=\"Underfitting and Overfitting 11 12\" width=\"888\" height=\"371\" \/><\/p>\n<p>In this case of M = 11 and M =12, you can see that the model fits the data very closely. There are two problems with this:<\/p>\n<ul>\n<li>first, the model becomes too complex<\/li>\n<li>second, the model is not able to generalize (predict new values) properly<\/li>\n<\/ul>\n<p>So this is the problem of <strong><em>overfitting<\/em><\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. The Trade-off<\/strong><\/h4>\n<p>Therefore, we need to find a trade-off between the too extremes we just discussed. This trade-off has a special name in Machine Learning. It is called <a href=\"https:\/\/kindsonthegenius.com\/blog\/bias-variance-trade-off-in-classificationmachine-learning\"><strong>Bias-Variance Trade-off<\/strong>.<\/a> It would be discussed further is subsequent lectures.<a href=\"https:\/\/www.youtube.com\/watch?v=Vzw54MFIwFk\" target=\"_blank\" rel=\"noopener\"> Bias-Variance Trade-off Video.<\/a><\/p>\n<p>At the trade-off point, the error is minimum.<\/p>\n<p>I have generated the plot for values of M = 6 and M = 7 as shown below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-690 size-full\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Underfitting-and-Overfitting-6-7.jpg\" alt=\"Underfitting and Overfitting 6 7\" width=\"984\" height=\"374\" \/><\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Hands on in Python<\/strong><\/h4>\n<p>Now I would like to play around with this in Python. The complete code is given below. Try to adjust the values of M gradually from 0 to 5 and see how it affects the graph.<\/p>\n<p>The complete code is given below.<\/p>\n<p>You can adjust M by changing the degree.<\/p>\n<p><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/machine-learning-101-polynomial-curve-fitting#pythoncode\">Python Code here<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-692 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Polynomial-Regression-in-Python-and-Jupyter-Notebook.jpg\" alt=\"Polynomial Regression in Python and Jupyter Notebook\" width=\"1108\" height=\"890\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Remember that in the previous lecture (Lecture 6), we\u00a0 discuss polynomial curve fitting. We kind of saw that the relationship in a dataset can be &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-1901","post","type-post","status-publish","format-standard","hentry","category-machine-learning"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1901","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=1901"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1901\/revisions"}],"predecessor-version":[{"id":2069,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1901\/revisions\/2069"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}