{"id":1,"date":"2011-01-19T18:25:27","date_gmt":"2011-01-19T18:25:27","guid":{"rendered":"http:\/\/www.netquote.it\/nqmain\/?p=1"},"modified":"2011-01-20T17:34:02","modified_gmt":"2011-01-20T16:34:02","slug":"curve_fitting","status":"publish","type":"post","link":"https:\/\/www.netquote.it\/nqmain\/2011\/01\/curve_fitting\/","title":{"rendered":"Curve fitting with Python"},"content":{"rendered":"<p>First we need a <a href=\"http:\/\/sourceforge.net\/projects\/numpy\/files\/NumPy\/\">NumPy<\/a> and <a href=\"http:\/\/sourceforge.net\/projects\/matplotlib\/files\/matplotlib\/\">MatPlotLib<\/a> modules.<\/p>\n<p>Look at curve fitting explanation from <a href=\"http:\/\/en.wikipedia.org\/wiki\/Curve_fitting\">wiki<\/a>.<\/p>\n<p>Second degree polynomial equation would help us: y=ax^2 + bx + c<br \/>\nThis will exactly fit a simple curve to three points.<br \/>\nUsing more points we get a nearest possible curve.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\n# Import modules\r\nfrom numpy import *\r\nfrom matplotlib.pyplot import *\r\n\r\n# Input data (change it)\r\n# X and Y points must have same length\r\nx = &#x5B;1,  2,  4,   6]\r\ny = &#x5B;10, 3,  7,  -6]\r\n\r\n# (a, b, c) coefficients computation\r\ncoefficients = polyfit(x, y, 2)\r\n\r\n# With coefficients we make a polynomial object\r\npoly = poly1d(coefficients)\r\n\r\n# Create point series\r\nxs = arange(min(x)-1, max(x)+1, 0.05)\r\n\r\n# Calculate resultant series\r\nys = poly(xs)\r\n\r\n# Finally draw everything\r\nplot(xs, ys)\r\nplot(x, y, 'o')\r\n\r\n# X and Y axis label\r\nylabel('y')\r\nxlabel('x')\r\n\r\n# And show\r\nshow()\r\n\r\n<\/pre>\n<p>Using 3 points:<br \/>\nx = [1,  2,  4]<br \/>\ny = [10, 3,  7]<br \/>\n<a href=\"http:\/\/www.netquote.it\/nqmain\/2011\/01\/curve_fitting\/curve_fitting_3\/\" rel=\"attachment wp-att-12\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/curve_fitting_3.png\" alt=\"\" title=\"curve_fitting_3\" width=\"600\" height=\"407\" class=\"alignnone size-full wp-image-12\" srcset=\"https:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/curve_fitting_3.png 600w, https:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/curve_fitting_3-150x101.png 150w, https:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/curve_fitting_3-300x203.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>Using 4 points:<br \/>\nx = [1,  2,  4,   6]<br \/>\ny = [10, 3,  7,  -6]<br \/>\n<a href=\"http:\/\/www.netquote.it\/nqmain\/2011\/01\/curve_fitting\/curve_fitting_4\/\" rel=\"attachment wp-att-13\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/curve_fitting_4.png\" alt=\"\" title=\"curve_fitting_4\" width=\"600\" height=\"407\" class=\"alignnone size-full wp-image-13\" srcset=\"https:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/curve_fitting_4.png 600w, https:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/curve_fitting_4-150x101.png 150w, https:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/curve_fitting_4-300x203.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>First we need a NumPy and MatPlotLib modules. Look at curve fitting explanation from wiki. Second degree polynomial equation would<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/posts\/1","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/comments?post=1"}],"version-history":[{"count":20,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/posts\/1\/revisions"}],"predecessor-version":[{"id":1281,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/posts\/1\/revisions\/1281"}],"wp:attachment":[{"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/media?parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/categories?post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/tags?post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}