{"id":248,"date":"2019-01-13T21:33:36","date_gmt":"2019-01-14T05:33:36","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=248"},"modified":"2024-07-21T05:25:31","modified_gmt":"2024-07-21T12:25:31","slug":"clustering","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/clustering\/","title":{"rendered":"#49 Clustering"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn import datasets\nfrom sklearn import metrics\nfrom sklearn import cluster\nos.chdir(r&amp;#039;D:\\projects\\wordpress\\ex49&amp;#039;)\nos.getcwd()\niris = datasets.load_iris()\nX, y = iris.data, iris.target\nnp.random.seed(123)\nn_clusters = 3\nc = cluster.KMeans(n_clusters=n_clusters)\nc.fit(X)\ny_pred = c.predict(X)\nprint(y_pred&#x5B;::8])\nprint(y&#x5B;::8])\nidx_0, idx_1, idx_2 = (np.where(y_pred == n) for n in range(3))\ny_pred&#x5B;idx_0], y_pred&#x5B;idx_1], y_pred&#x5B;idx_2] = 2, 0, 1\nprint(y_pred&#x5B;::8])\nprint(metrics.confusion_matrix(y, y_pred))\nN = X.shape&#x5B;1]\nfig, axes = plt.subplots(N, N, figsize=(12, 12), sharex=True, sharey=True)\ncolors = &#x5B;&amp;quot;coral&amp;quot;, &amp;quot;blue&amp;quot;, &amp;quot;green&amp;quot;]\nmarkers = &#x5B;&amp;quot;^&amp;quot;, &amp;quot;v&amp;quot;, &amp;quot;o&amp;quot;]\nfor m in range(N):\n    for n in range(N):\n        for p in range(n_clusters):\n            mask = y_pred == p\n            axes&#x5B;m, n].scatter(X&#x5B;:, m]&#x5B;mask], X&#x5B;:, n]&#x5B;mask],\n                               marker=markers&#x5B;p], s=30,\n                               color=colors&#x5B;p], alpha=0.25)\n\n&lt;pre&gt;&lt;code&gt;    for idx in np.where(y != y_pred):\n        axes&#x5B;m, n].scatter(X&#x5B;idx, m], X&#x5B;idx, n],\n                           marker=&amp;amp;quot;s&amp;amp;quot;, s=30,\n                           edgecolor=&amp;amp;quot;red&amp;amp;quot;,\n                           facecolor=(1,1,1,0))\n\naxes&#x5B;N-1, m].set_xlabel(iris.feature_names&#x5B;m], fontsize=16)\naxes&#x5B;m, 0].set_ylabel(iris.feature_names&#x5B;m], fontsize=16)\n&lt;\/code&gt;&lt;\/pre&gt;\n\nfig.tight_layout()\nplt.savefig(&amp;quot;example49.png&amp;quot;, dpi=100)\nplt.show()\nplt.close()\n<\/pre>\n<p>&nbsp;<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"  wp-image-249 aligncenter\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example49.png?resize=633%2C633\" alt=\"example49\" width=\"633\" height=\"633\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example49.png?w=1200&amp;ssl=1 1200w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example49.png?resize=150%2C150&amp;ssl=1 150w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example49.png?resize=300%2C300&amp;ssl=1 300w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example49.png?resize=768%2C768&amp;ssl=1 768w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example49.png?resize=1024%2C1024&amp;ssl=1 1024w\" sizes=\"(max-width: 633px) 100vw, 633px\" \/><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&amp;#039;D:\\projects\\wordpress\\ex49&amp;#039;) 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&#x5B;::8]) print(y&#x5B;::8]) idx_0, idx_1, idx_2 = (np.where(y_pred == n) for n in [&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-248","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-40","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":248,"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":243,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/classification\/","url_meta":{"origin":248,"position":1},"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":248,"position":2},"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":139,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/multidimensional-spline\/","url_meta":{"origin":248,"position":3},"title":"Multidimensional Spline","author":"gantovnik","date":"2019-01-04","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np from scipy import interpolate os.chdir(r'D:\\data\\scripts\\web1\\ex29') os.getcwd() np.random.seed(115925231) x = y = np.linspace(-1, 1, 100) X, Y = np.meshgrid(x, y) def f(x, y): return np.exp(-x**2 - y**2) * np.cos(4*x) * np.sin(6*y) Z = f(X, Y) N = 500 xdata = np.random.uniform(-1,\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example29","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example29.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example29.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example29.png?resize=525%2C300 1.5x"},"classes":[]},{"id":154,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/inexact-solutions-to-odes\/","url_meta":{"origin":248,"position":4},"title":"Inexact solutions to ODEs","author":"gantovnik","date":"2019-01-08","format":false,"excerpt":"\u00a0 import os import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl import sympy from IPython.display import display sympy.init_printing() mpl.rcParams['text.usetex'] = True import sympy os.chdir(r'D:\\projects\\wordpress\\ex33') os.getcwd() def plot_direction_field(x, y_x, f_xy, x_lim=(-5, 5), y_lim=(-5, 5), ax=None): f_np = sympy.lambdify((x, y_x), f_xy, 'numpy') x_vec = np.linspace(x_lim[0], x_lim[1], 20) y_vec\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example33","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example33.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example33.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example33.png?resize=525%2C300 1.5x"},"classes":[]},{"id":93,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/plots-of-nonlinear-functions\/","url_meta":{"origin":248,"position":5},"title":"Plots of nonlinear functions","author":"gantovnik","date":"2018-12-31","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np os.chdir('\/home\/vg\/Downloads\/projects\/ex15') os.getcwd() #define model parameters x=np.linspace(-2,2,1000) #examples of nonlinear functions f1=x**2-x-1 f2=x**3-3*np.sin(x) f3=np.exp(x)-2 f4=1-x**2+np.sin(50\/(1+x**2)) fig,axes=plt.subplots(1,4,figsize=(12,3),sharey=True) for n,f in enumerate([f1,f2,f3,f4]): axes[n].plot(x,f,lw=1.5) axes[n].axhline(0,ls=':',color='k') axes[n].set_ylim(-5,5) axes[n].set_xticks([-2,-1,0,1,2]) axes[n].set_xlabel(r'$x$',fontsize=18) axes[0].set_ylabel(r'$f(x)$',fontsize=18) titles=[r'$f(x)=x^2-x-1$',r'$f(x)=x^3-3\\sin(x)$',r'$f(x)=\\exp(x)-2$', r'$f(x)=\\sin\\left(50\/(1+x^2)\\right)+1-x^2$'] for n,title in enumerate(titles): axes[n].set_title(title) plt.savefig(\"example15.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":"example15","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example15.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example15.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example15.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/248","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=248"}],"version-history":[{"count":1,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/248\/revisions"}],"predecessor-version":[{"id":2907,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/248\/revisions\/2907"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}