{"id":126,"date":"2021-02-11T08:39:22","date_gmt":"2021-02-11T08:39:22","guid":{"rendered":"https:\/\/datasciencebasic.com\/?p=126"},"modified":"2021-04-29T00:11:57","modified_gmt":"2021-04-29T00:11:57","slug":"thuat-toan-phan-cum-k-means-k-means-clustering","status":"publish","type":"post","link":"https:\/\/datasciencebasic.com\/?p=126","title":{"rendered":"Thu\u1eadt to\u00e1n ph\u00e2n c\u1ee5m K-means (K-means Clustering)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">K-means clustering l\u00e0 m\u1ed9t trong nh\u1eefng thu\u1eadt to\u00e1n h\u1ecdc m\u00e1y kh\u00f4ng gi\u00e1m s\u00e1t (unsupervised learning) \u0111\u01a1n gi\u1ea3n v\u00e0 ph\u1ed5 bi\u1ebfn.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Th\u00f4ng th\u01b0\u1eddng, c\u00e1c thu\u1eadt to\u00e1n kh\u00f4ng gi\u00e1m s\u00e1t \u0111\u01b0a ra c\u00e1c k\u1ebft lu\u1eadn t\u1eeb c\u00e1c t\u1eadp d\u1eef li\u1ec7u ch\u1ec9 s\u1eed d\u1ee5ng c\u00e1c vect\u01a1 \u0111\u1ea7u v\u00e0o m\u00e0 kh\u00f4ng g\u1eafn nh\u00e3n d\u1eef li\u1ec7u ho\u1eb7c gi\u1ea3 \u0111\u1ecbnh tr\u01b0\u1edbc k\u1ebft qu\u1ea3.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">M\u1ee5c ti\u00eau c\u1ee7a K-means r\u1ea5t \u0111\u01a1n gi\u1ea3n: nh\u00f3m c\u00e1c \u0111i\u1ec3m d\u1eef li\u1ec7u t\u01b0\u01a1ng t\u1ef1 l\u1ea1i v\u1edbi nhau v\u00e0 kh\u00e1m ph\u00e1 c\u00e1c c\u1ea5u tr\u00fac (pattern) c\u01a1 b\u1ea3n. \u0110\u1ec3 \u0111\u1ea1t \u0111\u01b0\u1ee3c m\u1ee5c ti\u00eau n\u00e0y, K-means t\u00ecm ki\u1ebfm m\u1ed9t s\u1ed1 l\u01b0\u1ee3ng c\u1ed1 \u0111\u1ecbnh (k) c\u00e1c c\u1ee5m trong m\u1ed9t t\u1eadp d\u1eef li\u1ec7u.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">C\u1ee5m l\u00e0 m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c \u0111i\u1ec3m d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c t\u1eadp h\u1ee3p l\u1ea1i v\u1edbi nhau v\u00ec c\u00f3 nh\u1eefng \u0111i\u1ec3m t\u01b0\u01a1ng \u0111\u1ed3ng nh\u1ea5t \u0111\u1ecbnh.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">B\u1ea1n s\u1ebd x\u00e1c \u0111\u1ecbnh s\u1ed1 m\u1ee5c ti\u00eau k, l\u00e0 s\u1ed1 l\u01b0\u1ee3ng \u0111i\u1ec3m trung t\u00e2m (centroid) b\u1ea1n c\u1ea7n trong t\u1eadp d\u1eef li\u1ec7u. Centroid trong to\u00e1n h\u1ecdc l\u00e0 tr\u1ecdng t\u00e2m c\u1ee7a m\u1ed9t h\u00ecnh, v\u00ed d\u1ee5 tr\u1ecdng t\u00e2m c\u1ee7a tam gi\u00e1c. Trong thu\u1eadt to\u00e1n K-means, Centroid l\u00e0 v\u1ecb tr\u00ed th\u1ef1c ho\u1eb7c \u1ea3o \u0111\u1ea1i di\u1ec7n cho trung t\u00e2m c\u1ee7a c\u1ee5m.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"695\" height=\"283\" src=\"https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/centroid.png?resize=695%2C283&#038;ssl=1\" alt=\"\" class=\"wp-image-127\" srcset=\"https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/centroid.png?w=695&amp;ssl=1 695w, https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/centroid.png?resize=300%2C122&amp;ssl=1 300w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Ngu\u1ed3n: <a href=\"https:\/\/towardsdatascience.com\/k-means-a-complete-introduction-1702af9cd8c\">https:\/\/towardsdatascience.com\/K-means-a-complete-introduction-1702af9cd8c<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">M\u1ecdi \u0111i\u1ec3m d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c ph\u00e2n b\u1ed5 cho t\u1eebng c\u1ee5m sao cho t\u1ed5ng b\u00ecnh ph\u01b0\u01a1ng kho\u1ea3ng c\u00e1ch t\u1eeb c\u00e1c \u0111i\u1ec3m d\u1eef li\u1ec7u trong c\u1ee5m t\u1edbi c\u00e1c \u0111i\u1ec3m trung t\u00e2m Centroid l\u00e0 nh\u1ecf nh\u1ea5t.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">N\u00f3i c\u00e1ch kh\u00e1c, thu\u1eadt to\u00e1n K-means x\u00e1c \u0111\u1ecbnh k s\u1ed1 Centroid, v\u00e0 sau \u0111\u00f3 ph\u00e2n b\u1ed5 m\u1ecdi \u0111i\u1ec3m d\u1eef li\u1ec7u v\u00e0o c\u00e1c c\u1ee5m g\u1ea7n nh\u1ea5t, m\u1ed7i c\u1ee5m t\u01b0\u01a1ng \u1ee9ng v\u1edbi 1 \u0111i\u1ec3m trung t\u00e2m Centroid.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&#8216;Means&#8217; trong K-means \u0111\u1ec1 c\u1eadp \u0111\u1ebfn \u0111i\u1ec3m trung b\u00ecnh c\u1ee7a d\u1eef li\u1ec7u; hay n\u00f3i c\u00e1ch kh\u00e1c l\u00e0 t\u00ecm ra \u0111i\u1ec3m trung t\u00e2m.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>C\u00e1ch ho\u1ea1t \u0111\u1ed9ng c\u1ee7a thu\u1eadt to\u00e1n K-means<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u0110\u1ec3 x\u1eed l\u00fd d\u1eef li\u1ec7u \u0111\u1ea7u v\u00e0o, thu\u1eadt to\u00e1n K-means trong khai th\u00e1c d\u1eef li\u1ec7u b\u1eaft \u0111\u1ea7u v\u1edbi m\u1ed9t nh\u00f3m \u0111\u1ea7u ti\u00ean g\u1ed3m c\u00e1c \u0111i\u1ec3m trung t\u00e2m Centroid \u0111\u01b0\u1ee3c l\u1ef1a ch\u1ecdn ng\u1eabu nhi\u00ean, sau \u0111\u00f3 th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p t\u00ednh l\u1eb7p \u0111i l\u1eb7p l\u1ea1i v\u00e0 so s\u00e1nh c\u00e1c gi\u00e1 tr\u1ecb t\u1ed5ng b\u00ecnh ph\u01b0\u01a1ng kho\u1ea3ng c\u00e1ch \u0111\u1ec3 t\u1ed1i \u01b0u h\u00f3a v\u1ecb tr\u00ed c\u1ee7a c\u00e1c \u0111i\u1ec3m trung t\u00e2m n\u00e0y.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thu\u1eadt to\u00e1n t\u1ea1m d\u1eebng t\u1ea1o v\u00e0 t\u1ed1i \u01b0u h\u00f3a c\u00e1c c\u1ee5m khi:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>C\u00e1c centroid \u0111\u00e3 \u1ed5n \u0111\u1ecbnh &#8211; kh\u00f4ng c\u00f3 s\u1ef1 thay \u0111\u1ed5i v\u1ec1 gi\u00e1 tr\u1ecb c\u1ee7a ch\u00fang b\u1edfi v\u00ec vi\u1ec7c ph\u00e2n nh\u00f3m \u0111\u00e3 th\u00e0nh c\u00f4ng.<\/li><li>\u0110\u00e3 \u0111\u1ea1t \u0111\u01b0\u1ee3c s\u1ed1 l\u1ea7n l\u1eb7p (iterations) x\u00e1c \u0111\u1ecbnh.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Th\u1ef1c h\u00e0nh thu\u1eadt to\u00e1n K-means<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">H\u00e3y xem c\u00e1c b\u01b0\u1edbc v\u1ec1 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng c\u1ee7a thu\u1eadt to\u00e1n h\u1ecdc m\u00e1y K-means b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng Kaggle Notebook.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">T\u00f4i s\u1ebd s\u1eed d\u1ee5ng th\u01b0 vi\u1ec7n Scikit-learning v\u00e0 c\u00e1c d\u1eef li\u1ec7u ng\u1eabu nhi\u00ean \u0111\u1ec3 minh h\u1ecda gi\u1ea3i th\u00edch \u0111\u01a1n gi\u1ea3n v\u1ec1 ph\u00e2n c\u1ee5m K-means.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>B\u01b0\u1edbc 1:<\/strong> Nh\u1eadp c\u00e1c th\u01b0 th\u01b0 vi\u1ec7n Panda, Numpy v\u00e0 Matplotlib:<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">import pandas as pd<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">import matplotlib.pyplot as plt<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">from sklearn.cluster import KMeans<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">%matplotlib inline<\/pre>\n<\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Nh\u01b0 b\u1ea1n c\u00f3 th\u1ec3 th\u1ea5y t\u1eeb \u0111o\u1ea1n code tr\u00ean, ch\u00fang ta s\u1ebd nh\u1eadp c\u00e1c th\u01b0 vi\u1ec7n sau v\u00e0o d\u1ef1 \u00e1n c\u1ee7a m\u00ecnh:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Panda \u0111\u1ec3 \u0111\u1ecdc v\u00e0 vi\u1ebft b\u1ea3ng t\u00ednh<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Numpy \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p t\u00ednh hi\u1ec7u qu\u1ea3<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Matplotlib \u0111\u1ec3 tr\u1ef1c quan h\u00f3a d\u1eef li\u1ec7u<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>B\u01b0\u1edbc 2:<\/strong> t\u1ea1o d\u1eef li\u1ec7u ng\u1eabu nhi\u00ean<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ch\u00fang ta s\u1ebd nh\u1eadp \u0111o\u1ea1n code sau \u0111\u1ec3 t\u1ea1o m\u1ed9t s\u1ed1 d\u1eef li\u1ec7u ng\u1eabu nhi\u00ean trong kh\u00f4ng gian 2 chi\u1ec1u v\u00e0 th\u1ec3 hi\u1ec7n c\u00e1c d\u1eef li\u1ec7u \u0111\u00f3 tr\u00ean bi\u1ec3u \u0111\u1ed3 scatter plot:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">X= -2 * np.random.rand(200,2)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">X1 = 1 + 2 * np.random.rand(100,2)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">X[100:200, :] = X1<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">plt.scatter(X[ : , 0], X[ :, 1], s = 50, c = 'b')<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">plt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">T\u1ed5ng c\u1ed9ng 200 \u0111i\u1ec3m d\u1eef li\u1ec7u \u0111\u00e3 \u0111\u01b0\u1ee3c t\u1ea1o v\u00e0 chia th\u00e0nh hai nh\u00f3m, m\u1ed7i nh\u00f3m 100 \u0111i\u1ec3m.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">C\u00e1c d\u1eef li\u1ec7u n\u00e0y \u0111\u01b0\u1ee3c hi\u1ec3n th\u1ecb tr\u00ean kh\u00f4ng gian hai chi\u1ec1u nh\u01b0 sau:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"384\" height=\"262\" src=\"https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/scatter-plot.png?resize=384%2C262&#038;ssl=1\" alt=\"\" class=\"wp-image-128\" srcset=\"https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/scatter-plot.png?w=384&amp;ssl=1 384w, https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/scatter-plot.png?resize=300%2C205&amp;ssl=1 300w\" sizes=\"auto, (max-width: 384px) 100vw, 384px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>B\u01b0\u1edbc 3: <\/strong>S\u1eed d\u1ee5ng Scikit-Learn<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ch\u00fang ta s\u1ebd s\u1eed d\u1ee5ng ch\u1ee9c n\u0103ng K-means c\u00f3 s\u1eb5n trong th\u01b0 vi\u1ec7n Scikit-learning \u0111\u1ec3 x\u1eed l\u00fd d\u1eef li\u1ec7u ng\u1eabu nhi\u00ean v\u1eeba \u0111\u01b0\u1ee3c t\u1ea1o \u1edf tr\u00ean.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Nh\u1eadp code nh\u01b0 sau:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sklearn.cluster import KMeans<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Kmean = KMeans(n_clusters=2)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Kmean.fit(X)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Trong tr\u01b0\u1eddng h\u1ee3p n\u00e0y, ch\u00fang ta \u0111\u00e3 gi\u1ea3 \u0111\u1ecbnh tr\u01b0\u1edbc s\u1ed1 c\u1ee5m hay k (n_clusters) l\u00e0 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>B\u01b0\u1edbc 4:<\/strong> T\u00ecm centroid<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u0110\u00e2y l\u00e0 code \u0111\u1ec3 t\u00ecm \u0111i\u1ec3m trung t\u00e2m (centroid) c\u1ee7a c\u00e1c c\u1ee5m:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Kmean.cluster_centers_<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">K\u1ebft qu\u1ea3 c\u1ee7a gi\u00e1 tr\u1ecb c\u1ee7a centroid trong tr\u01b0\u1eddng h\u1ee3p n\u00e0y l\u00e0:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">array([[ 1.87977152,&nbsp; 1.98393029],\n&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;  [-1.03261149, -0.92568723]])<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Ch\u00fang ta c\u00f3 th\u1ec3 th\u1ec3 hi\u1ec7n c\u00e1c centroid n\u00e0y tr\u00ean s\u01a1 \u0111\u1ed3 scatter plot c\u00f9ng v\u1edbi c\u00e1c \u0111i\u1ec3m d\u1eef li\u1ec7u ng\u1eabu nhi\u00ean, s\u1eed d\u1ee5ng code nh\u01b0 sau:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">plt.scatter(X[ : , 0], X[ : , 1], s =50, c='b')<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">plt.scatter( 1.87977152,1.98393029, s=100, c='g', marker='s')<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">plt.scatter(-1.03261149, -0.92568723, s=100, c='r', marker='s')<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">plt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">K\u1ebft qu\u1ea3 thu \u0111\u01b0\u1ee3c:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"426\" height=\"253\" src=\"https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/scatter-plot-2.png?resize=426%2C253&#038;ssl=1\" alt=\"\" class=\"wp-image-129\" srcset=\"https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/scatter-plot-2.png?w=426&amp;ssl=1 426w, https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/scatter-plot-2.png?resize=300%2C178&amp;ssl=1 300w\" sizes=\"auto, (max-width: 426px) 100vw, 426px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>B\u01b0\u1edbc 5:<\/strong> Ki\u1ec3m tra k\u1ebft qu\u1ea3 thu\u1eadt to\u00e1n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">S\u1eed d\u1ee5ng code nh\u01b0 sau \u0111\u1ec3 l\u1ea5y thu\u1ed9c t\u00ednh nh\u00e3n c\u1ee7a t\u1eadp d\u1eef li\u1ec7u v\u1eeba \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 ph\u00e2n c\u1ee5m K-means; ngh\u0129a l\u00e0, c\u00e1ch c\u00e1c \u0111i\u1ec3m d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c ph\u00e2n lo\u1ea1i th\u00e0nh hai c\u1ee5m.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Kmean.labels_<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">K\u1ebft qu\u1ea3:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0, 0], dtype=int32)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Nh\u01b0 b\u1ea1n c\u00f3 th\u1ec3 th\u1ea5y \u1edf tr\u00ean, 100 \u0111i\u1ec3m d\u1eef li\u1ec7u thu\u1ed9c v\u1ec1 0 cluster trong khi ph\u1ea7n c\u00f2n l\u1ea1i thu\u1ed9c v\u1ec1 1 cluster.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ti\u1ebfp theo, ch\u1eb3ng h\u1ea1n ch\u00fang ta c\u00f3 m\u1ed9t \u0111i\u1ec3m d\u1eef li\u1ec7u b\u1ea5t k\u1ef3, ta c\u1ea7n ki\u1ec3m tra xem \u0111i\u1ec3m d\u1eef li\u1ec7u n\u00e0y thu\u1ed9c c\u1ee5m n\u00e0o. H\u00e3y s\u1eed d\u1ee5ng code d\u01b0\u1edbi \u0111\u00e2y:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sample_test=np.array([-3.0,-3.0])<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">second_test=sample_test.reshape(1, -1)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Kmean.predict(second_test)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0110\u00e2y l\u00e0 k\u1ebft qu\u1ea3:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">array([0], dtype=int32)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Nh\u01b0 v\u1eady d\u1eef li\u1ec7u ch\u00fang ta v\u1eeba ki\u1ec3m tra thu\u1ed9c v\u1ec1 c\u1ee5m 0 (t\u00e2m m\u00e0u xanh l\u00e1 c\u00e2y).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>K\u1ebft lu\u1eadn<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">K-means clustering l\u00e0 m\u1ed9t k\u1ef9 thu\u1eadt \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng r\u1ed9ng r\u00e3i \u0111\u1ec3 ph\u00e2n t\u00edch c\u1ee5m d\u1eef li\u1ec7u.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ph\u01b0\u01a1ng ph\u00e1p n\u00e0y r\u1ea5t d\u1ec5 hi\u1ec3u v\u00e0 d\u1ec5 th\u1ef1c hi\u1ec7n. H\u01a1n n\u1eefa, s\u1eed d\u1ee5ng ph\u01b0\u01a1ng ph\u00e1p ph\u00e2n c\u1ee5m K-means cho k\u1ebft qu\u1ea3 \u0111\u00e0o t\u1ea1o nhanh. Tuy nhi\u00ean, hi\u1ec7u su\u1ea5t c\u1ee7a n\u00f3 th\u01b0\u1eddng kh\u00f4ng t\u1ed1t b\u1eb1ng c\u00e1c k\u1ef9 thu\u1eadt ph\u00e2n c\u1ee5m ph\u1ee9c t\u1ea1p kh\u00e1c b\u1edfi v\u00ec c\u00e1c bi\u1ebfn th\u1ec3 nh\u1ecf trong d\u1eef li\u1ec7u c\u00f3 th\u1ec3 d\u1eabn \u0111\u1ebfn sai s\u1ed1 l\u1edbn.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">H\u01a1n n\u1eefa, c\u00e1c c\u1ee5m \u0111\u01b0\u1ee3c gi\u1ea3 \u0111\u1ecbnh l\u00e0 h\u00ecnh c\u1ea7u v\u00e0 c\u00f3 k\u00edch th\u01b0\u1edbc \u0111\u1ed3ng \u0111\u1ec1u, \u0111i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 l\u00e0m gi\u1ea3m \u0111\u1ed9 ch\u00ednh x\u00e1c c\u1ee7a c\u00e1c k\u1ebft qu\u1ea3 ph\u00e2n c\u1ee5m.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Trong c\u00e1c b\u00e0i vi\u1ebft ti\u1ebfp theo, t\u00f4i s\u1ebd gi\u1edbi thi\u1ec7u c\u00e1c ph\u01b0\u01a1ng ph\u00e1p ph\u00e2n c\u1ee5m kh\u00e1c nh\u1eb1m kh\u1eafc ph\u1ee5c nh\u1eefng \u0111i\u1ec3m y\u1ebfu c\u1ee7a ph\u01b0\u01a1ng ph\u00e1p ph\u00e2n c\u1ee5m K-means.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>K-means clustering l\u00e0 m\u1ed9t trong nh\u1eefng thu\u1eadt to\u00e1n h\u1ecdc m\u00e1y kh\u00f4ng gi\u00e1m s\u00e1t (unsupervised learning) \u0111\u01a1n gi\u1ea3n v\u00e0 ph\u1ed5 bi\u1ebfn. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":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":[3],"tags":[],"class_list":["post-126","post","type-post","status-publish","format-standard","hentry","category-kien-thuc"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=\/wp\/v2\/posts\/126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=126"}],"version-history":[{"count":2,"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=\/wp\/v2\/posts\/126\/revisions"}],"predecessor-version":[{"id":147,"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=\/wp\/v2\/posts\/126\/revisions\/147"}],"wp:attachment":[{"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}