{"id":5215,"date":"2013-12-06T13:21:03","date_gmt":"2013-12-06T12:21:03","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=5215"},"modified":"2013-12-06T16:13:56","modified_gmt":"2013-12-06T15:13:56","slug":"simple-nonlinear-least-squares-curve-fitting-in-python","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=5215","title":{"rendered":"Simple nonlinear least squares curve fitting in Python"},"content":{"rendered":"<p>A question I get asked a lot is &#8216;How can I do nonlinear least squares curve fitting in X?&#8217; where X might be MATLAB, Mathematica or a whole host of alternatives. \u00a0Since this is such a common query, I thought I&#8217;d write up how to do it for a very simple problem in several systems that I&#8217;m interested in<\/p>\n<p>This is the Python version. For other versions,see the list below<\/p>\n<ul>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=5181\">Simple nonlinear least squares curve fitting in Julia<\/a><\/li>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=5218\">Simple nonlinear least squares curve fitting in Maple<\/a><\/li>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=5180\">Simple nonlinear least squares curve fitting in Mathematica<\/a><\/li>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=5196\">Simple nonlinear least squares curve fitting in MATLAB<\/a><\/li>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=5254\">Simple nonlinear least squares curve fitting in R<\/a><\/li>\n<\/ul>\n<p><strong>The problem<\/strong><\/p>\n<pre>xdata = -2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9\r\nydata = 0.699369,0.700462,0.695354,1.03905,1.97389,2.41143,1.91091,0.919576,-0.730975,-1.42001<\/pre>\n<p>and you&#8217;d like to fit the function<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/walkingrandomly.com\/wp-content\/ql-cache\/quicklatex.com-17f46b025dd91e024cf2dc04211e19ba_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#32;&#70;&#40;&#112;&#95;&#49;&#44;&#112;&#95;&#50;&#44;&#120;&#41;&#32;&#61;&#32;&#112;&#95;&#49;&#32;&#92;&#99;&#111;&#115;&#40;&#112;&#95;&#50;&#32;&#120;&#41;&#43;&#112;&#95;&#50;&#32;&#92;&#115;&#105;&#110;&#40;&#112;&#95;&#49;&#32;&#120;&#41;&#32;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"297\" style=\"vertical-align: -4px;\"\/><\/p>\n<p>using nonlinear least squares. \u00a0You&#8217;re starting guesses for the parameters are p1=1 and P2=0.2<\/p>\n<p>For now, we are primarily interested in the following results:<\/p>\n<ul>\n<li><span style=\"line-height: 13px;\">The fit parameters<\/span><\/li>\n<li>Sum of squared residuals<\/li>\n<\/ul>\n<p>Future updates of these posts will show how to get other results such as confidence intervals. Let me know what you are most interested in.<\/p>\n<p><strong>Python solution using scipy<\/strong><\/p>\n<p>Here, I use the curve_fit function from scipy<\/p>\n<pre>import numpy as np\r\nfrom scipy.optimize import curve_fit\r\n\r\nxdata = np.array([-2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9])\r\nydata = np.array([0.699369,0.700462,0.695354,1.03905,1.97389,2.41143,1.91091,0.919576,-0.730975,-1.42001])\r\n\r\ndef func(x, p1,p2):\r\n  return p1*np.cos(p2*x) + p2*np.sin(p1*x)\r\n\r\npopt, pcov = curve_fit(func, xdata, ydata,p0=(1.0,0.2))<\/pre>\n<p>The variable popt contains the fit parameters<\/p>\n<pre>array([ 1.88184732,  0.70022901])<\/pre>\n<p>We need to do a little more work to get the sum of squared residuals<\/p>\n<pre>p1 = popt[0]\r\np2 = popt[1]\r\nresiduals = ydata - func(xdata,p1,p2)\r\nfres = sum(residuals**2)<\/pre>\n<p>which gives<\/p>\n<pre>0.053812696547933969<\/pre>\n<ul>\n<li>I&#8217;ve put this in an Ipython notebook which can be <a href=\"https:\/\/www.walkingrandomly.com\/images\/python\/least_squares\/Python_nonlinear_least_squares.ipynb\">downloaded here<\/a>. \u00a0There is also a <a href=\"https:\/\/www.walkingrandomly.com\/images\/python\/least_squares\/Python_nonlinear_least_squares.pdf\">pdf version<\/a> of the notebook.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A question I get asked a lot is &#8216;How can I do nonlinear least squares curve fitting in X?&#8217; where X might be MATLAB, Mathematica or a whole host of alternatives. \u00a0Since this is such a common query, I thought I&#8217;d write up how to do it for a very simple problem in several systems [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,7,31],"tags":[],"class_list":["post-5215","post","type-post","status-publish","format-standard","hentry","category-math-software","category-programming","category-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-1m7","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5215"}],"version-history":[{"count":8,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5215\/revisions"}],"predecessor-version":[{"id":5301,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5215\/revisions\/5301"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}