{"id":283,"date":"2019-01-21T22:28:58","date_gmt":"2019-01-22T06:28:58","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=283"},"modified":"2019-01-21T22:33:23","modified_gmt":"2019-01-22T06:33:23","slug":"perceptron-model","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/perceptron-model\/","title":{"rendered":"Perceptron model"},"content":{"rendered":"\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib.colors import ListedColormap\nos.chdir(r'D:\\projects\\wordpress\\ex52')\nos.getcwd()\n\nclass Perceptron(object):\n    #Rosenblatt's perceptron\n    def __init__(self,eta=0.01,n_iter=10):\n        self.eta=eta # learning rate, &#x5B;0,1]\n        self.n_iter=n_iter # passes over the training dataset \n\n    def fit(self,X,y): # fitting training data\n        # X = training vector, shape = &#x5B;n_samples,n_features]\n        # n_samples = number of samples\n        # n_features = number of features\n        # y = target values, shape = &#x5B;n_samples]\n        self.w_=np.zeros(1+X.shape&#x5B;1]) # weights after fitting\n        self.errors_ = &#x5B;] # number of misclassification in every epoch\n        \n        for _ in range(self.n_iter):\n            errors = 0\n            for xi, target in zip(X,y):\n                update = self.eta * (target - self.predict(xi))\n                self.w_&#x5B;1:] = self.w_&#x5B;1:] + update * xi\n                self.w_&#x5B;0] = self.w_&#x5B;0] + update\n                errors = errors + int(update != 0.0)\n            \n            self.errors_.append(errors)\n        \n        return self\n\n    def net_input(self,X):\n        # calculate net input\n        return np.dot(X,self.w_&#x5B;1:]) + self.w_&#x5B;0]\n    \n    def predict(self,X):\n        # return class label after unit step \n        return np.where(self.net_input(X) &amp;amp;gt;= 0.0,1,-1)\n           \nurl='https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\ndf=pd.read_csv(url,header=None)\nprint(df.tail())\ny=df.iloc&#x5B;0:100,4].values\ny=np.where(y=='Iris-setosa',-1,1)\nX=df.iloc&#x5B;0:100,&#x5B;0,2]].values\nplt.scatter(X&#x5B;:50,0],X&#x5B;:50,1],color='red',marker='o',label='Setosa')\nplt.scatter(X&#x5B;50:100,0],X&#x5B;50:100,1],color='blue',marker='x',label='Versicolor')\nplt.xlabel('Sepal Length ')\nplt.ylabel('Petal Length')\nplt.legend(loc='upper left')\nplt.tight_layout()\nplt.savefig(&quot;example52.png&quot;, dpi=100)\nplt.show()\n\nppn=Perceptron(eta=0.1,n_iter=10)\nppn.fit(X,y)\nplt.plot(range(1,len(ppn.errors_)+1),ppn.errors_,marker='o')\nplt.xlabel('Epoch')\nplt.ylabel('Number of classification errors')\nplt.tight_layout()\nplt.savefig(&quot;example52_2.png&quot;, dpi=100)\nplt.show()\n\ndef plot_decision_regions(X,y,classifier,resolution=0.02):\n    markers=('s','x','o','^','v')\n    colors=('red','blue','lightgreen','gray','cyan')\n    cmap = ListedColormap(colors&#x5B;:len(np.unique(y))])\n    x1_min,x1_max=X&#x5B;:,0].min()-1,X&#x5B;:,0].max()+1\n    x2_min,x2_max=X&#x5B;:,1].min()-1,X&#x5B;:,1].max()+1\n    xx1,xx2=np.meshgrid(np.arange(x1_min,x1_max,resolution),\n                        np.arange(x2_min,x2_max,resolution))\n    Z=classifier.predict(np.array(&#x5B;xx1.ravel(),xx2.ravel()]).T)\n    Z=Z.reshape(xx1.shape)\n    plt.contourf(xx1,xx2,Z,alpha=0.2,cmap=cmap)\n    plt.xlim(xx1.min(),xx1.max())\n    plt.ylim(xx2.min(),xx2.max())\n    \n    for idx,cl in enumerate(np.unique(y)):\n        plt.scatter(x=X&#x5B;y==cl,0],y=X&#x5B;y==cl,1],alpha=0.9,color=cmap(idx),marker=markers&#x5B;idx],label=cl)\n\nplot_decision_regions(X, y, classifier=ppn)\nplt.xlabel('sepal length &#x5B;cm]')\nplt.ylabel('petal length &#x5B;cm]')\nplt.legend(loc='upper left')    \nplt.tight_layout()\nplt.savefig(&quot;example52_3.png&quot;, dpi=100)\nplt.show()\nplt.close()\n<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"600\" height=\"400\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example52.png?resize=600%2C400\" alt=\"\" class=\"wp-image-287\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example52.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example52.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"600\" height=\"400\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example52_2.png?resize=600%2C400\" alt=\"\" class=\"wp-image-288\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example52_2.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example52_2.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"600\" height=\"400\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example52_3.png?resize=600%2C400\" alt=\"\" class=\"wp-image-289\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example52_3.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example52_3.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>import os import pandas as pd import matplotlib.pyplot as plt import numpy as np from matplotlib.colors import ListedColormap os.chdir(r&#8217;D:\\projects\\wordpress\\ex52&#8242;) os.getcwd() class Perceptron(object): #Rosenblatt&#8217;s perceptron def __init__(self,eta=0.01,n_iter=10): self.eta=eta # learning rate, &#x5B;0,1] self.n_iter=n_iter # passes over the training dataset def fit(self,X,y): # fitting training data # X = training vector, shape = &#x5B;n_samples,n_features] # n_samples = [&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":[11,2],"tags":[10],"class_list":["post-283","post","type-post","status-publish","format-standard","hentry","category-machine-learning","category-python","tag-machine-learning"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-4z","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":304,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/training-a-perceptron-via-scikit-learn\/","url_meta":{"origin":283,"position":0},"title":"#53 Training a perceptron via scikit-learn","author":"gantovnik","date":"2019-01-22","format":false,"excerpt":"[code language=\"python\"] import os import matplotlib.pyplot as plt import numpy as np from sklearn import datasets os.chdir(r'D:\\projects\\wordpress\\ex53') os.getcwd() iris = datasets.load_iris() X = iris.data[:, [2, 3]] y = iris.target from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) from sklearn.preprocessing import StandardScaler sc = StandardScaler() sc.fit(X_train)\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\/01\/example53.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example53.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example53.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":107,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/nonlinear-least-square-fit\/","url_meta":{"origin":283,"position":1},"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":318,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/weighted-and-unweighted-least-squares-fitting\/","url_meta":{"origin":283,"position":2},"title":"Weighted and unweighted least squares fitting","author":"gantovnik","date":"2019-01-22","format":false,"excerpt":"[code language=\"python\"] import os import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit os.chdir(r'D:\\data\\scripts\\wordpress\\ex55') os.getcwd() x0,A,gamma=12,3,5 n=200 x=np.linspace(1,20,n) yexact=A*gamma**2\/(gamma**2+(x-x0)**2) #Learning Scientific Programming with Python # Add some noise with a sigma of 0.5 apart from a particularly noisy region # near x0 where sigma is 3 sigma=np.ones(n)*0.5\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\/01\/example55.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example55.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example55.png?resize=525%2C300&ssl=1 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":283,"position":3},"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":[]},{"id":847,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/01\/150-optimization-with-a-module-in-python\/","url_meta":{"origin":283,"position":4},"title":"#150 Optimization with a module in python","author":"gantovnik","date":"2021-01-24","format":false,"excerpt":"#150 Optimization with a module in python ex150.py [code language=\"python\"] import os import numpy as np from scipy.optimize import minimize, Bounds from structure import structure os.chdir(r'D:\\projects\\wordpress\\ex150') os.getcwd() def runoptimization(params,stressmax): objhist=[] def objcon(x): mass,stress=structure(x,params) f=mass g=stressmax-stress objhist.append(mass) return f,g xlast=[] flast=[] glast=[] def obj(x): nonlocal xlast,flast,glast if not np.array_equal(x,xlast): flast,glast =\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\/2021\/01\/ex150-300x200.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2053,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/406-nonlinear-least-squares-fitting-using-leastsq-from-scipy-optimize-in-python\/","url_meta":{"origin":283,"position":5},"title":"#406 Nonlinear least squares fitting using leastsq from scipy.optimize in python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt from scipy.optimize import leastsq A, freq, tau = 5, 4, 0.5 def f(t, A, freq, tau): return A * np.exp(-t\/tau) * np.cos(3*np.pi * freq * t) tmax, dt = 1, 0.01 t = np.arange(0, tmax, dt) yexact = f(t, A,\u2026","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\/01\/ex406.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex406.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex406.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/283","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=283"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/283\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}