{"id":88,"date":"2018-12-31T05:01:06","date_gmt":"2018-12-31T05:01:06","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=88"},"modified":"2024-06-26T04:04:36","modified_gmt":"2024-06-26T11:04:36","slug":"linear-least-square-fit","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/linear-least-square-fit\/","title":{"rendered":"#14 Linear least square fit using python"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"  wp-image-89 aligncenter\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13-1.png?resize=610%2C203\" alt=\"example13\" width=\"610\" height=\"203\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13-1.png?w=1200&amp;ssl=1 1200w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13-1.png?resize=300%2C100&amp;ssl=1 300w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13-1.png?resize=768%2C256&amp;ssl=1 768w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13-1.png?resize=1024%2C341&amp;ssl=1 1024w\" sizes=\"(max-width: 610px) 100vw, 610px\" \/><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\nimport scipy.linalg as la\r\nos.chdir(&#039;\/home\/vg\/Downloads\/projects\/ex14&#039;)\r\nos.getcwd()\r\n#define model parameters\r\nx=np.linspace(-1,1,200)\r\na,b,c=1,2,3\r\ny_exact=a+b*x+c*x**2\r\n#simulate noisy data\r\nm=200\r\nX=1-2*np.random.rand(m)\r\nY=a+b*X+c*X**2+np.random.randn(m)\r\n#linear least square fit\r\nA=np.vstack(&#x5B;X**0,X**1,X**2])\r\nsol,r,rank,sv=la.lstsq(A.T,Y)\r\ny_fit=sol&#x5B;0]+sol&#x5B;1]*x+sol&#x5B;2]*x**2\r\nfig,ax=plt.subplots(figsize=(12,4)) \r\nax.plot(X,Y,&#039;go&#039;,alpha=0.5,label=&#039;simulated data&#039;)\r\nax.plot(x,y_exact,&#039;k&#039;,lw=2,label=&#039;true value $y=1+2x+3x^2$&#039;)\r\nax.plot(x,y_fit,&#039;b&#039;,lw=2,label=&#039;least square fit&#039;)\r\n#1st order polynomial\r\nA=np.vstack(&#x5B;X**n for n in range(2)])\r\nsol,r,rank,sv=la.lstsq(A.T,Y)\r\ny_fit1=sum(&#x5B;s*x**n for n,s in enumerate(sol)])\r\n#15th order polynomial\r\nA=np.vstack(&#x5B;X**n for n in range(16)])\r\nsol,r,rank,sv=la.lstsq(A.T,Y)\r\ny_fit15=sum(&#x5B;s*x**n for n,s in enumerate(sol)])\r\nax.plot(x,y_fit1,&#039;r&#039;,lw=2,label=&#039;least square fit (1st order)&#039;)\r\nax.plot(x,y_fit15,&#039;m&#039;,lw=2,label=&#039;least square fit (15th order)&#039;)\r\nax.set_xlabel(r&quot;$x$&quot;,fontsize=18)\r\nax.set_ylabel(r&quot;$y$&quot;,fontsize=18)\r\nax. Legend(loc=2)\r\nplt.savefig(&quot;example14.png&quot;, dpi=100)\r\nplt.show()\r\nplt.close()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import os import matplotlib.pyplot as plt import numpy as np import scipy.linalg as la os.chdir(&#039;\/home\/vg\/Downloads\/projects\/ex14&#039;) os.getcwd() #define model parameters x=np.linspace(-1,1,200) a,b,c=1,2,3 y_exact=a+b*x+c*x**2 #simulate noisy data m=200 X=1-2*np.random.rand(m) Y=a+b*X+c*X**2+np.random.randn(m) #linear least square fit A=np.vstack(&#x5B;X**0,X**1,X**2]) sol,r,rank,sv=la.lstsq(A.T,Y) y_fit=sol&#x5B;0]+sol&#x5B;1]*x+sol&#x5B;2]*x**2 fig,ax=plt.subplots(figsize=(12,4)) ax.plot(X,Y,&#039;go&#039;,alpha=0.5,label=&#039;simulated data&#039;) ax.plot(x,y_exact,&#039;k&#039;,lw=2,label=&#039;true value $y=1+2x+3x^2$&#039;) ax.plot(x,y_fit,&#039;b&#039;,lw=2,label=&#039;least square fit&#039;) #1st order polynomial A=np.vstack(&#x5B;X**n for n in range(2)]) sol,r,rank,sv=la.lstsq(A.T,Y) y_fit1=sum(&#x5B;s*x**n for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[69,2],"tags":[117],"class_list":["post-88","post","type-post","status-publish","format-standard","hentry","category-matplotlib","category-python","tag-linear-least-square-fit"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-1q","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":124,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/polynomial-fit\/","url_meta":{"origin":88,"position":0},"title":"Polynomial fit","author":"gantovnik","date":"2019-01-03","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np from numpy import polynomial as P os.chdir(r'D:\\data\\scripts\\web1\\ex24') os.getcwd() x = np.array([1, 2, 3, 4]) y = np.array([1, 3, 5, 4]) f1 = P.Polynomial.fit(x, y, 1) f2 = P.Polynomial.fit(x, y, 2) f3 = P.Polynomial.fit(x, y, 3) xx = np.linspace(x.min(), x.max(), 100)\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example24","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example24.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example24.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example24.png?resize=525%2C300 1.5x"},"classes":[]},{"id":121,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/polynomial-interpolation\/","url_meta":{"origin":88,"position":1},"title":"Polynomial interpolation","author":"gantovnik","date":"2019-01-03","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np from numpy import polynomial as P from scipy import linalg os.chdir(r'D:\\data\\scripts\\web1\\ex23') os.getcwd() x = np.array([1, 2, 3, 4]) y = np.array([1, 3, 5, 4]) deg = len(x) - 1 A = P.polynomial.polyvander(x, deg) c = linalg.solve(A, y) f1 = P.Polynomial(c)\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example23","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example23.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example23.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example23.png?resize=525%2C300 1.5x"},"classes":[]},{"id":101,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/solution-of-system-of-nonlinear-equations\/","url_meta":{"origin":88,"position":2},"title":"Solution of system of nonlinear equations","author":"gantovnik","date":"2018-12-31","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np import scipy os.chdir('\/home\/vg\/Downloads\/projects\/ex17') os.getcwd() def f(x): return [x[1]-x[0]**3-2*x[0]**2+1,x[1]+x[0]**2-1] tol=0.1 a,b=-2,2 x=np.linspace(-3,2,5000) y1=x**3+2*x**2-1 y2=-x**2+1 fig,ax=plt.subplots(figsize=(8,4)) ax.plot(x,y1,'k',lw=1.5) ax.plot(x,y2,'k',lw=1.5) sol1=scipy.optimize.fsolve(f,[-2,2]) sol2=scipy.optimize.fsolve(f,[1,-1]) sol3=scipy.optimize.fsolve(f,[-2,-5]) sols=[sol1,sol2,sol3] colors=['r','b','g'] for idx,s in enumerate(sols): ax.plot(s[0],s[1],colors[idx]+'*',markersize=15) for m in np.linspace(-4,3,80): for n in np.linspace(-20,20,40): x_guess=[m,n] sol=scipy.optimize.fsolve(f,x_guess) idx = (abs(sols-sol)**2).sum(axis=1).argmin() ax.plot(x_guess[0],x_guess[1],colors[idx]+'.')\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example17","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example17.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example17.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example17.png?resize=525%2C300 1.5x"},"classes":[]},{"id":127,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/runge-problem\/","url_meta":{"origin":88,"position":3},"title":"Runge problem","author":"gantovnik","date":"2019-01-03","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np from numpy import polynomial as P os.chdir(r'D:\\data\\scripts\\web1\\ex25') os.getcwd() # In the mathematical field of numerical analysis, Runge's phenomenon # is a problem of oscillation at the edges of an interval that occurs # when using polynomial interpolation with polynomials of\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example25","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example25.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example25.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example25.png?resize=525%2C300 1.5x"},"classes":[]},{"id":107,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/nonlinear-least-square-fit\/","url_meta":{"origin":88,"position":4},"title":"Nonlinear least square fit","author":"gantovnik","date":"2018-12-31","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np import scipy os.chdir('\/home\/vg\/Downloads\/projects\/ex19') os.getcwd() beta=(0.25,0.75,0.5) def f(x,b0,b1,b2): return b0+b1*np.exp(-b2*x**2) xdata=np.linspace(0,5,50) y=f(xdata,*beta) ydata=y+0.05*np.random.randn(len(xdata)) def g(beta): return ydata-f(xdata,*beta) beta_start=(1,1,1) beta_opt,beta_cov=scipy.optimize.leastsq(g,beta_start) print(beta_opt) fig,ax=plt.subplots() ax.scatter(xdata,ydata,label='samples') ax.plot(xdata,y,'r',lw=2,label='true model') ax.plot(xdata,f(xdata,*beta_opt),'b',lw=2,label='fitted model') ax.set_xlim(0,5) ax.set_xlabel(r\"$x$\",fontsize=18) ax.set_ylabel(r\"$f(x,\\beta)$\",fontsize=18) ax.legend() ax.set_title(\"Nonlinear least square fitting\") plt.savefig(\"example19.png\", dpi=100) plt.show() plt.close() beta_opt,beta_cov=scipy.optimize.curve_fit(f,xdata,ydata) print(beta_opt) \u00a0","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example19","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example19.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example19.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example19.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1407,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/02\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":88,"position":5},"title":"#269 Fitting noisy data with a linear equation using python","author":"gantovnik","date":"2022-02-22","format":false,"excerpt":"#269 Fitting noisy data with a linear equation using python [code language=\"python\"] import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit def func(x,a,b): return a*x+b x=np.linspace(0,10,100) y=func(x,1,2) # Adding noise to the data yn=y+0.9*np.random.normal(size=len(x)) popt,pcov=curve_fit(func,x,yn) fig,ax = plt.subplots() ax.plot(x,y,'b-',label='data') ax.plot(x,yn,'.',c='red',label='data with noise') ax.plot(x, func(x, *popt),'g--',label='fit: a=%5.3f, b=%5.3f'\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/02\/ex269.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/02\/ex269.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/02\/ex269.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/88","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":2226,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/88\/revisions\/2226"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}