{"id":1002,"date":"2021-10-26T12:22:06","date_gmt":"2021-10-26T19:22:06","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1002"},"modified":"2021-10-26T12:22:06","modified_gmt":"2021-10-26T19:22:06","slug":"180-linear-regression-using-python","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/180-linear-regression-using-python\/","title":{"rendered":"#180 Linear regression using python"},"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.linear_model import LinearRegression\r\nsns.set()\r\nos.chdir(r'D:\\projects\\wordpress\\ex66')\r\nos.getcwd()\r\nrng = np.random.RandomState(1)\r\nx = 10 * rng.rand(50)\r\ny = 2 * x - 5 + rng.randn(50)\r\nplt.scatter(x, y)\r\nmodel = LinearRegression(fit_intercept=True)\r\nmodel.fit(x&#x5B;:, np.newaxis], y)\r\nxfit = np.linspace(0, 10, 1000)\r\nyfit = model.predict(xfit&#x5B;:, np.newaxis])\r\nplt.scatter(x,y,color='blue')\r\nplt.plot(xfit,yfit,color='red')\r\nprint(&quot;Model slope: &quot;, model.coef_&#x5B;0])\r\nprint(&quot;Model intercept:&quot;, model.intercept_)\r\nplt.savefig(&quot;example66.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\/2021\/10\/ex180.png?resize=640%2C480&#038;ssl=1\" alt=\"\" width=\"640\" height=\"480\" class=\"alignnone size-full wp-image-1003\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex180.png 640w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex180-480x360.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 640px, 100vw\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;D:\\projects\\wordpress\\ex66&#8242;) os.getcwd() rng = np.random.RandomState(1) x = 10 * rng.rand(50) y = 2 * x &#8211; 5 + rng.randn(50) plt.scatter(x, y) model = LinearRegression(fit_intercept=True) model.fit(x&#x5B;:, np.newaxis], y) xfit = np.linspace(0, 10, 1000) yfit = model.predict(xfit&#x5B;:, np.newaxis]) [&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-1002","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-ga","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":391,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/02\/linear-regression-2\/","url_meta":{"origin":1002,"position":0},"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":394,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/02\/polynomial-basis-function\/","url_meta":{"origin":1002,"position":1},"title":"#58 Polynomial basis function","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.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression from sklearn.pipeline import make_pipeline sns.set() os.chdir(r'D:\\projects\\wordpress\\ex58') os.getcwd() x = np.array([2, 3, 4]) poly = PolynomialFeatures(3, include_bias=False) poly.fit_transform(x[:, None]) poly_model = make_pipeline(PolynomialFeatures(7),LinearRegression()) rng = np.random.RandomState(1) 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\/2019\/02\/example67.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example67.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example67.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":387,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/02\/pca-principal-component-analysis\/","url_meta":{"origin":1002,"position":2},"title":"#56 PCA &#8211; Principal Component Analysis","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 sns.set() #from mpl_toolkits.mplot3d import Axes3D os.chdir(r'D:\\projects\\wordpress\\ex56') os.getcwd() rng = np.random.RandomState() X = np.dot(rng.rand(2, 2), rng.randn(2, 200)).T plt.scatter(X[:, 0], X[:, 1]) plt.axis('equal') from sklearn.decomposition import PCA pca = PCA(n_components=2) pca.fit(X) print(pca.components_) print(pca.explained_variance_) def draw_vector(v0, v1,\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\/example65_1.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example65_1.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example65_1.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1758,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/335-solution-of-nonlinear-equation-using-brent-method-in-python\/","url_meta":{"origin":1002,"position":3},"title":"#335 Solution of nonlinear equation by Brent&#8217;s method in python","author":"gantovnik","date":"2023-01-06","format":false,"excerpt":"Brent's method is a hybrid root-finding algorithm combining the bisection method, the secant method, and inverse quadratic interpolation. ex335.py [code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import scipy.optimize as optimize def f(x): return 2*x - 1 + 2*np.cos(np.pi*x) x = np.linspace(0.0,2.0,201) y=f(x) plt.plot(x,y,label='$2x - 1 + 2\\\\cos(\\\\pi\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex335.png?resize=350%2C200&ssl=1","width":350,"height":200},"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":1002,"position":4},"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":[]},{"id":283,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/perceptron-model\/","url_meta":{"origin":1002,"position":5},"title":"Perceptron model","author":"gantovnik","date":"2019-01-21","format":false,"excerpt":"[code language=\"python\"] import os import pandas as pd import matplotlib.pyplot as plt import numpy as np from matplotlib.colors import ListedColormap os.chdir(r'D:\\projects\\wordpress\\ex52') os.getcwd() class Perceptron(object): #Rosenblatt's perceptron def __init__(self,eta=0.01,n_iter=10): self.eta=eta # learning rate, [0,1] self.n_iter=n_iter # passes over the training dataset def fit(self,X,y): # fitting training data # X = training\u2026","rel":"","context":"In &quot;machine learning&quot;","block_context":{"text":"machine learning","link":"https:\/\/gantovnik.com\/bio-tips\/category\/machine-learning\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example52.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example52.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example52.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1002","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=1002"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1002\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}