{"id":394,"date":"2019-02-13T00:25:55","date_gmt":"2019-02-13T08:25:55","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=394"},"modified":"2021-11-12T14:54:20","modified_gmt":"2021-11-12T22:54:20","slug":"polynomial-basis-function","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2019\/02\/polynomial-basis-function\/","title":{"rendered":"#58 Polynomial basis function"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\nimport seaborn as sns\r\nfrom sklearn.preprocessing import PolynomialFeatures\r\nfrom sklearn.linear_model import LinearRegression\r\nfrom sklearn.pipeline import make_pipeline\r\nsns.set()\r\nos.chdir(r'D:\\projects\\wordpress\\ex58')\r\nos.getcwd()\r\nx = np.array(&#x5B;2, 3, 4])\r\npoly = PolynomialFeatures(3, include_bias=False)\r\npoly.fit_transform(x&#x5B;:, None])\r\npoly_model = make_pipeline(PolynomialFeatures(7),LinearRegression())\r\nrng = np.random.RandomState(1)\r\nx = 10 * rng.rand(50)\r\ny = np.sin(x) + 0.1 * rng.randn(50)\r\npoly_model.fit(x&#x5B;:, np.newaxis], y)\r\nxfit = np.linspace(0, 10, 1000)\r\nyfit = poly_model.predict(xfit&#x5B;:, np.newaxis])\r\nplt.scatter(x,y,color='blue')\r\nplt.plot(xfit,yfit,color='red')\r\nplt.savefig(&quot;example58.png&quot;, dpi=100)\r\nplt.show()\r\nplt.close()\r\n<\/pre>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example67.png?resize=600%2C400&#038;ssl=1\" alt=\"example67\" width=\"600\" height=\"400\" class=\"alignnone size-full wp-image-395\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example67.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example67.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>import os import matplotlib.pyplot as plt import numpy as np import seaborn as sns from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression from sklearn.pipeline import make_pipeline sns.set() os.chdir(r&#8217;D:\\projects\\wordpress\\ex58&#8242;) os.getcwd() x = np.array(&#x5B;2, 3, 4]) poly = PolynomialFeatures(3, include_bias=False) poly.fit_transform(x&#x5B;:, None]) poly_model = make_pipeline(PolynomialFeatures(7),LinearRegression()) rng = np.random.RandomState(1) x = 10 * rng.rand(50) y = np.sin(x) + [&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-394","post","type-post","status-publish","format-standard","hentry","category-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-6m","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1002,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/180-linear-regression-using-python\/","url_meta":{"origin":394,"position":0},"title":"#180 Linear regression using python","author":"gantovnik","date":"2021-10-26","format":false,"excerpt":"[code language=\"python\"] import os import matplotlib.pyplot as plt import numpy as np import seaborn as sns from sklearn.linear_model import LinearRegression sns.set() os.chdir(r'D:\\projects\\wordpress\\ex66') os.getcwd() rng = np.random.RandomState(1) x = 10 * rng.rand(50) y = 2 * x - 5 + rng.randn(50) plt.scatter(x, y) model = LinearRegression(fit_intercept=True) model.fit(x[:, np.newaxis], y) xfit =\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\/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":391,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/02\/linear-regression-2\/","url_meta":{"origin":394,"position":1},"title":"#57 Linear Regression","author":"gantovnik","date":"2019-02-13","format":false,"excerpt":"[code language=\"python\"] import os import matplotlib.pyplot as plt import numpy as np import seaborn as sns from sklearn.linear_model import LinearRegression sns.set() os.chdir(r'D:\\projects\\wordpress\\ex57') os.getcwd() rng = np.random.RandomState(1) x = 10 * rng.rand(50) y = 2 * x - 5 + rng.randn(50) plt.scatter(x, y) model = LinearRegression(fit_intercept=True) model.fit(x[:, np.newaxis], y) xfit =\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\/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":370,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/roots-of-equation\/","url_meta":{"origin":394,"position":2},"title":"#52 Roots of equation using python","author":"gantovnik","date":"2019-01-29","format":false,"excerpt":"[code language=\"python\"] import os import numpy as np import matplotlib.pyplot as plt from scipy.optimize import brentq os.chdir(r'D:\\projects\\wordpress\\ex52') os.getcwd() f=lambda x: 0.2+x * np.cos(3\/x) x=np.linspace(-1,1,1000) plt.plot(x,f(x)) plt.axhline(0,color='k') xstar=brentq(f,-0.7,-0.5) print(xstar,f(xstar)) plt.plot(xstar,f(xstar), 'ro') plt.text(-1.0, -0.5, 'x=%6.4f and f(x)=%6.4f' % (xstar, f(xstar))) plt.tight_layout() plt.savefig(\"example52.png\", dpi=300) plt.show() plt.close() [\/code]","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\/01\/example62.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example62.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example62.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":82,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/plot-with-an-inset\/","url_meta":{"origin":394,"position":3},"title":"#12: Plot with an inset in python","author":"gantovnik","date":"2018-12-29","format":false,"excerpt":"[code language=\"python\"] import os import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np os.chdir('\/home\/vg\/Downloads\/projects\/ex12') os.getcwd() fig = plt.figure(figsize=(10,8)) def f(x): return 1\/(1+x**2) + 0.1\/(1+((3-x)\/0.1)**2) def plot_and_format_axes(ax,x,f,fontsize): ax.plot(x,f(x),linewidth=2) ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(5)) ax.yaxis.set_major_locator(mpl.ticker.MaxNLocator(4)) ax.set_xlabel(r\"$x$\",fontsize=fontsize) ax.set_ylabel(r\"$f(x)$\",fontsize=fontsize) ax=fig.add_axes([0.1,0.15,0.8,0.8],facecolor=\"#f5f5f5\") x = np.linspace(-4,14,1000) plot_and_format_axes(ax,x,f,18) plt.title('Plot with inset') x0,x1=2.5,3.5 ax.axvline(x0,ymax=0.3,color=\"grey\",linestyle=\":\") ax.axvline(x1,ymax=0.3,color=\"grey\",linestyle=\":\") ax_insert=fig.add_axes([0.5,0.5,0.38,0.42],facecolor='none') x=np.linspace(x0,x1,1000) plot_and_format_axes(ax_insert,x,f,14) plt.savefig(\"example12.png\", dpi=100)\u2026","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"example12","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example12.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example12.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example12.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2059,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/407-multiple-axes-animation-using-python\/","url_meta":{"origin":394,"position":4},"title":"#407 Multiple axes animation using python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"y=sin(x) [code language=\"python\"] import matplotlib.pyplot as plt import numpy as np import matplotlib.animation as animation from matplotlib.patches import ConnectionPatch fig, (axl, axr) = plt.subplots( ncols=2, sharey=True, figsize=(6, 2), gridspec_kw=dict(width_ratios=[1, 3], wspace=0), ) axl.set_aspect(1) axr.set_box_aspect(1 \/ 3) axr.yaxis.set_visible(False) axr.xaxis.set_ticks([0, np.pi, 2 * np.pi], [\"0\", r\"$\\pi$\", r\"$2\\pi$\"]) # draw circle with initial\u2026","rel":"","context":"In &quot;animation&quot;","block_context":{"text":"animation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/animation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex407.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex407.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex407.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1718,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/12\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2\/","url_meta":{"origin":394,"position":5},"title":"#325 Finding the intersection points between two functions using python","author":"gantovnik","date":"2022-12-13","format":false,"excerpt":"[code language=\"python\"] from scipy.optimize import fsolve import numpy as np import matplotlib.pyplot as plt # Defining function to simplify intersection solution def findIntersection(func1, func2, x0): return fsolve(lambda x : func1(x) - func2(x), x0) # Defining functions that will intersect funky = lambda x : np.cos(x \/ 5) * np.sin(x \/\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\/12\/ex325.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex325.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex325.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/394","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=394"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/394\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}