{"id":304,"date":"2019-01-22T00:37:10","date_gmt":"2019-01-22T08:37:10","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=304"},"modified":"2024-07-21T05:27:55","modified_gmt":"2024-07-21T12:27:55","slug":"training-a-perceptron-via-scikit-learn","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/training-a-perceptron-via-scikit-learn\/","title":{"rendered":"#53 Training a perceptron via scikit-learn"},"content":{"rendered":"\r\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\nfrom sklearn import datasets\r\nos.chdir(r&#039;D:\\projects\\wordpress\\ex53&#039;)\r\nos.getcwd()\r\niris = datasets.load_iris()\r\nX = iris.data&#x5B;:, &#x5B;2, 3]]\r\ny = iris.target\r\nfrom sklearn.model_selection import train_test_split\r\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)\r\n\r\nfrom sklearn.preprocessing import StandardScaler\r\nsc = StandardScaler()\r\nsc.fit(X_train)\r\nX_train_std = sc.transform(X_train)\r\nX_test_std = sc.transform(X_test)\r\n\r\nfrom sklearn.linear_model import Perceptron\r\nppn = Perceptron(max_iter=40, eta0=0.1, random_state=0)\r\nppn.fit(X_train_std, y_train)\r\n\r\ny_pred = ppn.predict(X_test_std)\r\nprint(&#039;Misclassified samples: %d&#039; % (y_test != y_pred).sum())\r\n\r\nfrom sklearn.metrics import accuracy_score\r\nprint(&#039;Accuracy: %.2f&#039; % accuracy_score(y_test, y_pred))\r\n\r\nfrom matplotlib.colors import ListedColormap\r\ndef plot_decision_regions(X, y, classifier,test_idx=None, resolution=0.02):\r\n    # setup marker generator and color map\r\n    markers = (&#039;s&#039;, &#039;x&#039;, &#039;o&#039;, &#039;^&#039;, &#039;v&#039;)\r\n    colors = (&#039;red&#039;, &#039;blue&#039;, &#039;lightgreen&#039;, &#039;gray&#039;, &#039;cyan&#039;)\r\n    cmap = ListedColormap(colors&#x5B;:len(np.unique(y))])\r\n    # plot the decision surface\r\n    x1_min, x1_max = X&#x5B;:, 0].min() - 1, X&#x5B;:, 0].max() + 1\r\n    x2_min, x2_max = X&#x5B;:, 1].min() - 1, X&#x5B;:, 1].max() + 1\r\n    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),\r\n                           np.arange(x2_min, x2_max, resolution))\r\n    Z = classifier.predict(np.array(&#x5B;xx1.ravel(), xx2.ravel()]).T)\r\n    Z = Z.reshape(xx1.shape)\r\n    plt.contourf(xx1, xx2, Z, alpha=0.2, cmap=cmap)\r\n    plt.xlim(xx1.min(), xx1.max())\r\n    plt.ylim(xx2.min(), xx2.max())\r\n\r\n    # plot all samples\r\n    for idx, cl in enumerate(np.unique(y)):\r\n        plt.scatter(x=X&#x5B;y == cl, 0], y=X&#x5B;y == cl, 1],\r\n                    alpha=0.8, color=cmap(idx),\r\n                    marker=markers&#x5B;idx], label=cl)\r\n\r\n    # highlight test samples\r\n    if test_idx:\r\n        X_test, y_test = X&#x5B;test_idx, :], y&#x5B;test_idx]\r\n        plt.scatter(X_test&#x5B;:, 0], X_test&#x5B;:, 1], facecolors=&#039;none&#039;, edgecolors=&#039;black&#039;,\r\n                    alpha=1.0, linewidths=1, marker=&#039;o&#039;,\r\n                    s=55, label=&#039;test set&#039;)\r\n\r\nX_combined_std = np.vstack((X_train_std, X_test_std))\r\ny_combined = np.hstack((y_train, y_test))\r\nplot_decision_regions(X=X_combined_std,\r\n                      y=y_combined,\r\n                      classifier=ppn,\r\n                      test_idx=range(105,150))\r\nplt.xlabel(&#039;petal length &#x5B;standardized]&#039;)\r\nplt.ylabel(&#039;petal width &#x5B;standardized]&#039;)\r\nplt.legend(loc=&#039;upper left&#039;)\r\nplt.tight_layout()\r\nplt.savefig(&quot;example53.png&quot;, dpi=100)\r\nplt.show()\r\nplt.close()\r\n<\/pre>\r\n\r\n\r\n\r\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><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\/example53.png?resize=600%2C400\" alt=\"\" class=\"wp-image-305\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example53.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example53.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure><\/div>\r\n","protected":false},"excerpt":{"rendered":"<p>import os import matplotlib.pyplot as plt import numpy as np from sklearn import datasets os.chdir(r&#039;D:\\projects\\wordpress\\ex53&#039;) os.getcwd() iris = datasets.load_iris() X = iris.data&#x5B;:, &#x5B;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) X_train_std = sc.transform(X_train) X_test_std = sc.transform(X_test) from [&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-304","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-4U","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":243,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/classification\/","url_meta":{"origin":304,"position":0},"title":"#48 Classification","author":"gantovnik","date":"2019-01-13","format":false,"excerpt":"[code language=\"python\"] import os import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn import linear_model from sklearn import metrics from sklearn import tree from sklearn import neighbors from sklearn import svm from sklearn import ensemble from sklearn import cluster import seaborn\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example48","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example48.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example48.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example48.png?resize=525%2C300 1.5x"},"classes":[]},{"id":232,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/regression\/","url_meta":{"origin":304,"position":1},"title":"#47 Regression","author":"gantovnik","date":"2019-01-13","format":false,"excerpt":"[code language=\"python\"] import os import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn import linear_model from sklearn import metrics from sklearn import tree from sklearn import neighbors from sklearn import svm from sklearn import ensemble from sklearn import cluster import seaborn\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\/example47_2.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example47_2.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example47_2.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example47_2.png?resize=700%2C400 2x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example47_2.png?resize=1050%2C600 3x"},"classes":[]},{"id":401,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/02\/neural-network-prediction\/","url_meta":{"origin":304,"position":2},"title":"#60 Neural network prediction using python neurolab","author":"gantovnik","date":"2019-02-19","format":false,"excerpt":"[code language=\"python\"] import os import matplotlib.pyplot as plt import numpy as np import neurolab as nl os.chdir(r'D:\\projects\\wordpress\\ex60') os.getcwd() #create train sets x=np.linspace(-10,10,60) y=np.cos(x)*0.9 size=len(x) x_train=x.reshape(size,1) y_train=y.reshape(size,1) #create network with 4 layers and randomly initiate d=[[1,1],[45,1],[45,45,1],[45,45,45,1]] for i in range(4): net=nl.net.newff([[-10,10]],d[i]) train_net=nl.train.train_gd(net,x_train,y_train,epochs=1000,show=100) outp=net.sim(x_train) plt.subplot(2,1,1) plt.grid(True) plt.plot(train_net) plt.title('Hidden Layers: ' + str(i))\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\/example69_3.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example69_3.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example69_3.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":283,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/perceptron-model\/","url_meta":{"origin":304,"position":3},"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":[]},{"id":248,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/clustering\/","url_meta":{"origin":304,"position":4},"title":"#49 Clustering","author":"gantovnik","date":"2019-01-13","format":false,"excerpt":"[code language=\"python\"] import os import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn import metrics from sklearn import cluster os.chdir(r'D:\\projects\\wordpress\\ex49') os.getcwd() iris = datasets.load_iris() X, y = iris.data, iris.target np.random.seed(123) n_clusters = 3 c = cluster.KMeans(n_clusters=n_clusters) c.fit(X) y_pred = c.predict(X) print(y_pred[::8]) print(y[::8]) idx_0, idx_1, idx_2\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example49","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example49.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example49.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example49.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1002,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/180-linear-regression-using-python\/","url_meta":{"origin":304,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/304","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=304"}],"version-history":[{"count":1,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/304\/revisions"}],"predecessor-version":[{"id":2909,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/304\/revisions\/2909"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}