{"id":65,"date":"2018-12-24T12:16:40","date_gmt":"2018-12-24T12:16:40","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=65"},"modified":"2018-12-24T12:16:40","modified_gmt":"2018-12-24T12:16:40","slug":"linear-regression","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/linear-regression\/","title":{"rendered":"Linear regression"},"content":{"rendered":"<pre>import os\nimport matplotlib.pyplot as plt\nimport numpy as np\nos.chdir('\/home\/vg\/Downloads\/projects\/ex9')\nos.getcwd()\nplt.figure(figsize=(10,8)) \nN = 100\nstart = 0\nend = 1\nA = np.random.rand() + 1\nB = np.random.rand()\nx = np.linspace(start,end,N)\ny = A * x + B\ny = y + np.random.randn(N)\/10\np = np.polyfit(x,y,1)\nplt.plot(x,y,'o',label='Given data: A=%.2f. B=%.2f' % (A,B))\nplt.plot(x,np.polyval(p,x),'-',label='Linear regression: A=%.2f. B=%.2f' % tuple(p) )\nplt.legend(loc='best')\nplt.grid()\nplt.title('Linear regression')\nplt.savefig(\"example9.png\", dpi=100)\nplt.show()\nplt.close()\n\n<img data-recalc-dims=\"1\" decoding=\"async\" class=\"  wp-image-66 aligncenter\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example9.png?resize=610%2C488\" alt=\"example9\" width=\"610\" height=\"488\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example9.png?w=1000&amp;ssl=1 1000w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example9.png?resize=300%2C240&amp;ssl=1 300w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example9.png?resize=768%2C614&amp;ssl=1 768w\" sizes=\"(max-width: 610px) 100vw, 610px\" \/><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import os import matplotlib.pyplot as plt import numpy as np os.chdir(&#8216;\/home\/vg\/Downloads\/projects\/ex9&#8242;) os.getcwd() plt.figure(figsize=(10,8)) N = 100 start = 0 end = 1 A = np.random.rand() + 1 B = np.random.rand() x = np.linspace(start,end,N) y = A * x + B y = y + np.random.randn(N)\/10 p = np.polyfit(x,y,1) plt.plot(x,y,&#8217;o&#8217;,label=&#8217;Given data: A=%.2f. B=%.2f&#8217; % (A,B)) plt.plot(x,np.polyval(p,x),&#8217;-&#8216;,label=&#8217;Linear [&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_post_was_ever_published":false,"_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":""},"categories":[2],"tags":[],"class_list":["post-65","post","type-post","status-publish","format-standard","hentry","category-python"],"modified_by":null,"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-13","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":68,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/linear-regression-of-nonlinear-function\/","url_meta":{"origin":65,"position":0},"title":"Linear regression of nonlinear function","author":"gantovnik","date":"2018-12-24","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np import math os.chdir('\/home\/vg\/Downloads\/projects\/ex10') os.getcwd() plt.figure(figsize=(10,8)) N = 100 start = 0 end = 2 A = np.random.rand() + 0.5 B = np.random.rand() x = np.linspace(start,end,N) y = B*np.exp(A*x) y = y + np.random.randn(N)\/5 p = np.polyfit(x,np.log(y),1) plt.plot(x,y,'o',label='Given data: A=%.2f. B=%.2f'\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example10","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example10.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example10.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example10.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2158,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/421-plot-a-pandas-data-frame-for-loops-on-columns\/","url_meta":{"origin":65,"position":1},"title":"#421 Plot a pandas data frame for loops on columns","author":"gantovnik","date":"2024-05-04","format":false,"excerpt":"","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex421.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex421.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex421.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":391,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/02\/linear-regression-2\/","url_meta":{"origin":65,"position":2},"title":"#57 Linear Regression","author":"gantovnik","date":"2019-02-13","format":false,"excerpt":"","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\/2019\/02\/example66.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example66.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example66.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1002,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/180-linear-regression-using-python\/","url_meta":{"origin":65,"position":3},"title":"#180 Linear regression using python","author":"gantovnik","date":"2021-10-26","format":false,"excerpt":"","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\/2021\/10\/ex180.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex180.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex180.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":88,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/linear-least-square-fit\/","url_meta":{"origin":65,"position":4},"title":"#14 Linear least square fit using python","author":"gantovnik","date":"2018-12-31","format":false,"excerpt":"","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"example13","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13-1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13-1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13-1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":62,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/piecewise-linear-interpolation\/","url_meta":{"origin":65,"position":5},"title":"Piecewise linear interpolation","author":"gantovnik","date":"2018-12-24","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np os.chdir('\/home\/vg\/Downloads\/projects\/ex8') os.getcwd() plt.figure(figsize=(10,8)) x = np.linspace(0,1,500) y = np.sqrt(1-x**2) xp = np.linspace(0,1,6) yp = np.sqrt(1-xp**2) xi = np.arange(0.1,1.0,0.2) yi = np.interp(xi,xp,yp) plt.plot(x,y,'b',label='ideal') plt.plot(xp,yp,'or',label='interpolation points') plt.plot(xp,yp,'--r',label='piecewise linear function') plt.plot(xi,yi,'sg',label='interpolated values') plt.legend(loc='best') plt.grid() plt.axis('scaled') plt.axis([0,1.1,0,1.1]) plt.title('Piecewise linear interpolation') plt.savefig(\"example8.png\", dpi=100) plt.show() plt.close()","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example8","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example8.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example8.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example8.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/65","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=65"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}