{"id":53010,"date":"2025-08-11T22:44:44","date_gmt":"2025-08-11T14:44:44","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/53010.html"},"modified":"2025-08-11T22:44:44","modified_gmt":"2025-08-11T14:44:44","slug":"%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0-k-means%e8%81%9a%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/53010.html","title":{"rendered":"\u673a\u5668\u5b66\u4e60\u2014\u2014K-means\u805a\u7c7b"},"content":{"rendered":"<p>\u65e0\u76d1\u7763\u5b66\u4e60\u662f\u673a\u5668\u5b66\u4e60\u7684\u91cd\u8981\u5206\u652f&#xff0c;\u5b83\u4ece\u65e0\u6807\u7b7e\u6570\u636e\u4e2d\u53d1\u73b0\u9690\u85cf\u6a21\u5f0f\u548c\u7ed3\u6784\u3002\u4ee5\u4e0b\u662f\u5173\u4e8e\u65e0\u76d1\u7763\u5b66\u4e60\u7684\u6df1\u5ea6\u89e3\u6790&#xff1a;<\/p>\n<h3>\u4e00\u3001\u6838\u5fc3\u6982\u5ff5<\/h3>\n<h4>1. \u5b9a\u4e49\u4e0e\u7279\u70b9<\/h4>\n<p>\u65e0\u76d1\u7763\u5b66\u4e60\u5904\u7406\u200b\u200b\u6ca1\u6709\u9884\u5148\u6807\u8bb0\u200b\u200b\u7684\u6570\u636e\u96c6&#xff0c;\u76ee\u6807\u662f&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u53d1\u73b0\u6570\u636e\u4e2d\u7684\u200b\u200b\u9690\u85cf\u6a21\u5f0f\u200b\u200b<\/p>\n<\/li>\n<li>\n<p>\u8bc6\u522b\u200b\u200b\u5185\u5728\u7ed3\u6784\u200b\u200b<\/p>\n<\/li>\n<li>\n<p>\u63d0\u53d6\u200b\u200b\u6709\u610f\u4e49\u7279\u5f81\u200b\u200b<\/p>\n<\/li>\n<\/ul>\n<h4>2. \u4e0e\u76d1\u7763\u5b66\u4e60\u7684\u5bf9\u6bd4<\/h4>\n<table>\n<tr>\n<p>\u7279\u6027<\/p>\n<p>\u65e0\u76d1\u7763\u5b66\u4e60<\/p>\n<p>\u76d1\u7763\u5b66\u4e60<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td>\n<p>\u6570\u636e\u8981\u6c42<\/p>\n<\/td>\n<td>\n<p>\u65e0\u9700\u6807\u7b7e<\/p>\n<\/td>\n<td>\n<p>\u9700\u8981\u6807\u6ce8\u6570\u636e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u76ee\u6807<\/p>\n<\/td>\n<td>\n<p>\u53d1\u73b0\u7ed3\u6784<\/p>\n<\/td>\n<td>\n<p>\u9884\u6d4b\u8f93\u51fa<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u5e94\u7528\u573a\u666f<\/p>\n<\/td>\n<td>\n<p>\u63a2\u7d22\u6027\u5206\u6790<\/p>\n<\/td>\n<td>\n<p>\u9884\u6d4b\u6027\u4efb\u52a1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u5178\u578b\u7b97\u6cd5<\/p>\n<\/td>\n<td>\n<p>K-means, PCA<\/p>\n<\/td>\n<td>\n<p>\u56de\u5f52, \u5206\u7c7b<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u4e8c\u3001\u4e3b\u8981\u7b97\u6cd5\u5206\u7c7b<\/h3>\n<h4>1. \u805a\u7c7b\u5206\u6790<\/h4>\n<p>\u5c06\u76f8\u4f3c\u6570\u636e\u5206\u7ec4&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u200b\u200bK-means\u200b\u200b&#xff1a;\u57fa\u4e8e\u8ddd\u79bb\u7684\u5212\u5206\u805a\u7c7b<\/p>\n<\/li>\n<li>\n<p>\u200b\u200b\u5c42\u6b21\u805a\u7c7b\u200b\u200b&#xff1a;\u6784\u5efa\u6811\u72b6\u805a\u7c7b\u7ed3\u6784<\/p>\n<\/li>\n<li>\n<p>\u200b\u200bDBSCAN\u200b\u200b&#xff1a;\u57fa\u4e8e\u5bc6\u5ea6\u7684\u805a\u7c7b<\/p>\n<\/li>\n<li>\n<p>\u200b\u200b\u9ad8\u65af\u6df7\u5408\u6a21\u578b(GMM)\u200b\u200b&#xff1a;\u6982\u7387\u8f6f\u805a\u7c7b<\/p>\n<\/li>\n<\/ul>\n<h4>2. \u964d\u7ef4\u6280\u672f<\/h4>\n<p>\u51cf\u5c11\u7279\u5f81\u7ef4\u5ea6&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u200b\u200bPCA\u200b\u200b&#xff1a;\u4e3b\u6210\u5206\u5206\u6790&#xff08;\u7ebf\u6027&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u200b\u200bt-SNE\u200b\u200b&#xff1a;t\u5206\u5e03\u968f\u673a\u90bb\u57df\u5d4c\u5165&#xff08;\u975e\u7ebf\u6027&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u200b\u200bUMAP\u200b\u200b&#xff1a;\u7edf\u4e00\u6d41\u5f62\u903c\u8fd1\u4e0e\u6295\u5f71<\/p>\n<\/li>\n<li>\n<p>\u200b\u200b\u81ea\u52a8\u7f16\u7801\u5668\u200b\u200b&#xff1a;\u795e\u7ecf\u7f51\u7edc\u964d\u7ef4<\/p>\n<\/li>\n<\/ul>\n<h4>3. \u5173\u8054\u89c4\u5219\u5b66\u4e60<\/h4>\n<p>\u53d1\u73b0\u53d8\u91cf\u95f4\u5173\u7cfb&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u200b\u200bApriori\u200b\u200b&#xff1a;\u9891\u7e41\u9879\u96c6\u6316\u6398<\/p>\n<\/li>\n<li>\n<p>\u200b\u200bFP-Growth\u200b\u200b&#xff1a;\u9ad8\u6548\u9891\u7e41\u6a21\u5f0f\u53d1\u73b0<\/p>\n<\/li>\n<\/ul>\n<h4>4. \u5f02\u5e38\u68c0\u6d4b<\/h4>\n<p>\u8bc6\u522b\u5f02\u5e38\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u200b\u200b\u5b64\u7acb\u68ee\u6797\u200b\u200b<\/p>\n<\/li>\n<li>\n<p>\u200b\u200b\u5c40\u90e8\u79bb\u7fa4\u56e0\u5b50(LOF)\u200b\u200b<\/p>\n<\/li>\n<li>\n<p>\u200b\u200b\u4e00\u7c7bSVM\u200b<\/p>\n<\/li>\n<\/ul>\n<h2>\u4e09\u3001k-means\u805a\u7c7b<\/h2>\n<\/p>\n<p>K-means\u662f\u6700\u7ecf\u5178\u4e14\u5e7f\u6cdb\u4f7f\u7528\u7684\u805a\u7c7b\u7b97\u6cd5\u4e4b\u4e00&#xff0c;\u5c5e\u4e8e\u65e0\u76d1\u7763\u5b66\u4e60\u3002\u4e0b\u9762\u6211\u5c06\u5168\u9762\u4ecb\u7ecdK-means\u7684\u539f\u7406\u3001\u5b9e\u73b0\u548c\u5e94\u7528\u3002<\/p>\n<h3 style=\"background-color:transparent\">\u7b97\u6cd5\u539f\u7406<\/h3>\n<p>k-means \u805a\u7c7b\u7684\u6838\u5fc3\u76ee\u6807\u662f\u5c06 n \u4e2a\u6570\u636e\u70b9\u5212\u5206\u5230 k \u4e2a\u7c07\u4e2d&#xff0c;\u901a\u8fc7\u4f18\u5316\u4ee5\u4e0b\u4e24\u4e2a\u6761\u4ef6\u6765\u8fbe\u5230\u6700\u4f73\u7684\u805a\u7c7b\u6548\u679c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"453\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811144443-689a01db32057.png\" width=\"597\" \/><\/p>\n<li>\n<p>\u6570\u636e\u70b9\u5f52\u5c5e\u539f\u5219&#xff1a;<\/p>\n<ul>\n<li>\u6bcf\u4e2a\u6570\u636e\u70b9\u4f1a\u88ab\u5206\u914d\u5230\u8ddd\u79bb\u6700\u8fd1\u7684\u7c07\u4e2d\u5fc3\u6240\u5728\u7684\u7c07\u3002\u5177\u4f53\u6765\u8bf4&#xff0c;\u5bf9\u4e8e\u6570\u636e\u96c6\u4e2d\u7684\u6bcf\u4e00\u4e2a\u70b9&#xff0c;\u8ba1\u7b97\u5b83\u4e0e\u6240\u6709\u7c07\u4e2d\u5fc3&#xff08;\u8d28\u5fc3&#xff09;\u7684\u6b27\u6c0f\u8ddd\u79bb&#xff0c;\u7136\u540e\u5c06\u5176\u5f52\u5165\u8ddd\u79bb\u6700\u77ed\u7684\u90a3\u4e2a\u7c07\u3002<\/li>\n<li>\u516c\u5f0f\u5316\u8868\u793a&#xff1a;\u5bf9\u4e8e\u6570\u636e\u70b9 ( x_i )&#xff0c;\u5176\u6240\u5c5e\u7684\u7c07 ( C_j ) \u6ee1\u8db3 ( j &#061; \\\\text{argmin}_k ||x_i &#8211; \\\\mu_k||^2 )&#xff0c;\u5176\u4e2d ( \\\\mu_k ) \u662f\u7b2c k \u4e2a\u7c07\u7684\u4e2d\u5fc3\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4f18\u5316\u76ee\u6807&#xff1a;<\/p>\n<ul>\n<li>\u7b97\u6cd5\u901a\u8fc7\u8fed\u4ee3\u4f18\u5316&#xff0c;\u4f7f\u5f97\u6240\u6709\u7c07\u5185\u6570\u636e\u70b9\u5230\u5176\u7c07\u4e2d\u5fc3\u7684\u5e73\u65b9\u8ddd\u79bb\u548c&#xff08;\u4e5f\u79f0\u4e3a\u7c07\u5185\u5e73\u65b9\u548c&#xff0c;Within-Cluster Sum of Squares, WCSS&#xff09;\u6700\u5c0f\u5316\u3002<\/li>\n<li>\u6570\u5b66\u8868\u8fbe\u5f0f&#xff1a;\u6700\u5c0f\u5316\u76ee\u6807\u51fd\u6570<\/li>\n<\/ul>\n<\/li>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"86\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811144443-689a01db82b4c.png\" width=\"251\" \/><\/p>\n<p>\u5176\u4e2d&#xff1a;<\/p>\n<ul>\n<li>\n<p>Ci\u200b&#xff1a;\u7b2ci\u4e2a\u7c07<\/p>\n<\/li>\n<li>\n<p>\u03bci\u200b&#xff1a;\u7b2ci\u4e2a\u7c07\u7684\u8d28\u5fc3<\/p>\n<\/li>\n<li>\n<p>x&#xff1a;\u6570\u636e\u70b9<\/p>\n<\/li>\n<\/ul>\n<h5>\u7b97\u6cd5\u6b65\u9aa4\u7684\u8be6\u7ec6\u8bf4\u660e&#xff1a;<\/h5>\n<li>\n<p>\u521d\u59cb\u5316&#xff1a;<\/p>\n<ul>\n<li>\u968f\u673a\u9009\u62e9 k \u4e2a\u6570\u636e\u70b9\u4f5c\u4e3a\u521d\u59cb\u7684\u7c07\u4e2d\u5fc3&#xff08;\u8d28\u5fc3&#xff09;\u3002\u8fd9\u4e00\u6b65\u5bf9\u6700\u7ec8\u805a\u7c7b\u7ed3\u679c\u6709\u8f83\u5927\u5f71\u54cd&#xff0c;\u5e38\u89c1\u7684\u6539\u8fdb\u65b9\u6cd5\u5305\u62ec k-means&#043;&#043; \u521d\u59cb\u5316&#xff0c;\u4ee5\u51cf\u5c11\u5bf9\u521d\u59cb\u4e2d\u5fc3\u7684\u654f\u611f\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5206\u914d\u9636\u6bb5&#xff1a;<\/p>\n<ul>\n<li>\u904d\u5386\u6240\u6709\u6570\u636e\u70b9&#xff0c;\u8ba1\u7b97\u6bcf\u4e2a\u70b9\u5230\u5f53\u524d\u5404\u4e2a\u7c07\u4e2d\u5fc3\u7684\u8ddd\u79bb&#xff0c;\u5e76\u5c06\u5176\u5206\u914d\u5230\u6700\u8fd1\u7684\u7c07\u3002\u901a\u5e38\u4f7f\u7528\u6b27\u6c0f\u8ddd\u79bb&#xff0c;\u4f46\u4e5f\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u95ee\u9898\u9009\u62e9\u5176\u4ed6\u8ddd\u79bb\u5ea6\u91cf&#xff08;\u5982\u66fc\u54c8\u987f\u8ddd\u79bb&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u66f4\u65b0\u9636\u6bb5&#xff1a;<\/p>\n<ul>\n<li>\u91cd\u65b0\u8ba1\u7b97\u6bcf\u4e2a\u7c07\u7684\u4e2d\u5fc3&#xff0c;\u5373\u53d6\u7c07\u5185\u6240\u6709\u6570\u636e\u70b9\u7684\u5747\u503c\u4f5c\u4e3a\u65b0\u7684\u8d28\u5fc3\u3002\u4f8b\u5982&#xff0c;\u5bf9\u4e8e\u7c07 ( C_j )&#xff0c;\u5176\u65b0\u4e2d\u5fc3 ( \\\\mu_j ) \u66f4\u65b0\u4e3a ( \\\\mu_j &#061; \\\\frac{1}{|C_j|} \\\\sum_{x_i \\\\in C_j} x_i )\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8fed\u4ee3\u4f18\u5316&#xff1a;<\/p>\n<ul>\n<li>\u91cd\u590d\u5206\u914d\u548c\u66f4\u65b0\u6b65\u9aa4&#xff0c;\u76f4\u5230\u6ee1\u8db3\u505c\u6b62\u6761\u4ef6&#xff08;\u5982\u8d28\u5fc3\u53d8\u5316\u5c0f\u4e8e\u67d0\u4e2a\u9608\u503c&#xff0c;\u6216\u76ee\u6807\u51fd\u6570 ( J ) \u7684\u53d8\u5316\u4e0d\u518d\u663e\u8457&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<h5>\u5e94\u7528\u573a\u666f\u793a\u4f8b&#xff1a;<\/h5>\n<ul>\n<li>\u5ba2\u6237\u7ec6\u5206&#xff1a;\u5728\u5e02\u573a\u8425\u9500\u4e2d&#xff0c;\u6839\u636e\u5ba2\u6237\u7684\u8d2d\u4e70\u884c\u4e3a\u3001 demographics \u7b49\u7279\u5f81&#xff0c;\u5c06\u5ba2\u6237\u5206\u6210\u4e0d\u540c\u7684\u7fa4\u4f53&#xff0c;\u4ee5\u4fbf\u5236\u5b9a\u4e2a\u6027\u5316\u7b56\u7565\u3002<\/li>\n<li>\u56fe\u50cf\u538b\u7f29&#xff1a;\u901a\u8fc7\u5c06\u56fe\u50cf\u4e2d\u989c\u8272\u76f8\u8fd1\u7684\u50cf\u7d20\u805a\u7c7b&#xff0c;\u7528\u5c11\u91cf\u4ee3\u8868\u6027\u989c\u8272\u66ff\u4ee3\u539f\u59cb\u989c\u8272&#xff0c;\u51cf\u5c11\u5b58\u50a8\u7a7a\u95f4\u3002<\/li>\n<li>\u5f02\u5e38\u68c0\u6d4b&#xff1a;\u901a\u8fc7\u805a\u7c7b\u5206\u6790&#xff0c;\u5c06\u6b63\u5e38\u6570\u636e\u548c\u5f02\u5e38\u6570\u636e\u5206\u79bb&#xff0c;\u4f8b\u5982\u5728\u7f51\u7edc\u5b89\u5168\u4e2d\u8bc6\u522b\u5f02\u5e38\u6d41\u91cf\u3002<\/li>\n<\/ul>\n<h5>\u6ce8\u610f\u4e8b\u9879&#xff1a;<\/h5>\n<ul>\n<li>\u9700\u8981\u9884\u5148\u6307\u5b9a\u7c07\u7684\u6570\u91cf k&#xff0c;\u9009\u62e9\u4e0d\u5408\u9002\u7684 k \u503c\u53ef\u80fd\u5bfc\u81f4\u805a\u7c7b\u6548\u679c\u4e0d\u4f73\u3002\u53ef\u4ee5\u901a\u8fc7\u8098\u90e8\u6cd5\u5219&#xff08;Elbow Method&#xff09;\u6216\u8f6e\u5ed3\u7cfb\u6570&#xff08;Silhouette Score&#xff09;\u6765\u786e\u5b9a\u6700\u4f73 k \u503c\u3002<\/li>\n<li>\u5bf9\u566a\u58f0\u548c\u5f02\u5e38\u503c\u654f\u611f&#xff0c;\u56e0\u4e3a\u5e73\u65b9\u8ddd\u79bb\u4f1a\u653e\u5927\u79bb\u7fa4\u70b9\u7684\u5f71\u54cd\u3002\u6539\u8fdb\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528 k-medoids \u6216\u5176\u4ed6\u9c81\u68d2\u6027\u66f4\u5f3a\u7684\u7b97\u6cd5\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u4f18\u5316\u76ee\u6807&#xff0c;k-means \u80fd\u591f\u9ad8\u6548\u5730\u5c06\u6570\u636e\u5212\u5206\u4e3a\u5177\u6709\u9ad8\u5185\u805a\u6027\u7684\u7c07&#xff0c;\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u96c6\u7684\u805a\u7c7b\u4efb\u52a1\u3002<\/p>\n<h2>\u56db\u3001k-means\u805a\u7c7b\u7684\u4f7f\u7528<\/h2>\n<p>class sklearn.cluster.KMeans( n_clusters&#061;8, *, init&#061;&#039;k-means&#043;&#043;&#039;, n_init&#061;&#039;auto&#039;, max_iter&#061;300, tol&#061;1e-4, verbose&#061;0, random_state&#061;None, copy_x&#061;True, algorithm&#061;&#039;lloyd&#039; )<\/p>\n<h3>\u4e8c\u3001\u6838\u5fc3\u53c2\u6570\u8be6\u89e3<\/h3>\n<h4>1. \u805a\u7c7b\u914d\u7f6e\u53c2\u6570<\/h4>\n<table>\n<tr>\n<p>\u53c2\u6570<\/p>\n<p>\u7c7b\u578b<\/p>\n<p>\u8bf4\u660e<\/p>\n<p>\u63a8\u8350\u503c<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td>\n<p>n_clusters<\/p>\n<\/td>\n<td>\n<p>int<\/p>\n<\/td>\n<td>\n<p>\u8981\u5f62\u6210\u7684\u7c07\u6570&#xff08;k\u503c&#xff09;<\/p>\n<\/td>\n<td>\n<p>\u901a\u8fc7\u8098\u90e8\u6cd5\u5219\u786e\u5b9a<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>init<\/p>\n<\/td>\n<td>\n<p>str\/array<\/p>\n<\/td>\n<td>\n<p>\u521d\u59cb\u5316\u65b9\u6cd5<\/p>\n<\/td>\n<td>\n<p>&#039;k-means&#043;&#043;&#039;(\u9ed8\u8ba4) \u6216 &#039;random&#039;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>n_init<\/p>\n<\/td>\n<td>\n<p>int\/str<\/p>\n<\/td>\n<td>\n<p>\u4e0d\u540c\u521d\u59cb\u5316\u7684\u8fd0\u884c\u6b21\u6570<\/p>\n<\/td>\n<td>\n<p>&#039;auto&#039; \u6216 10(\u9ed8\u8ba4)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>algorithm<\/p>\n<\/td>\n<td>\n<p>str<\/p>\n<\/td>\n<td>\n<p>K-means\u7b97\u6cd5\u53d8\u4f53<\/p>\n<\/td>\n<td>\n<p>&#039;lloyd&#039;(\u9ed8\u8ba4), &#039;elkan&#039;<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>2. \u4f18\u5316\u63a7\u5236\u53c2\u6570<\/h4>\n<table>\n<tr>\n<p>\u53c2\u6570<\/p>\n<p>\u7c7b\u578b<\/p>\n<p>\u8bf4\u660e<\/p>\n<p>\u63a8\u8350\u503c<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td>\n<p>max_iter<\/p>\n<\/td>\n<td>\n<p>int<\/p>\n<\/td>\n<td>\n<p>\u5355\u6b21\u8fd0\u884c\u7684\u6700\u5927\u8fed\u4ee3\u6b21\u6570<\/p>\n<\/td>\n<td>\n<p>300(\u9ed8\u8ba4)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>tol<\/p>\n<\/td>\n<td>\n<p>float<\/p>\n<\/td>\n<td>\n<p>\u6536\u655b\u9608\u503c<\/p>\n<\/td>\n<td>\n<p>1e-4(\u9ed8\u8ba4)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>random_state<\/p>\n<\/td>\n<td>\n<p>int<\/p>\n<\/td>\n<td>\n<p>\u968f\u673a\u79cd\u5b50<\/p>\n<\/td>\n<td>\n<p>\u4efb\u610f\u6574\u6570(\u4fdd\u8bc1\u53ef\u590d\u73b0)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u4e09\u3001\u4e3b\u8981\u5c5e\u6027<\/h3>\n<p>\u8bad\u7ec3\u540e\u6a21\u578b\u5c06\u83b7\u5f97\u4ee5\u4e0b\u91cd\u8981\u5c5e\u6027&#xff1a;<\/p>\n<table>\n<tr>\n<p>\u5c5e\u6027<\/p>\n<p>\u8bf4\u660e<\/p>\n<p>\u5f62\u72b6<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td>\n<p>cluster_centers_<\/p>\n<\/td>\n<td>\n<p>\u7c07\u4e2d\u5fc3\u5750\u6807<\/p>\n<\/td>\n<td>\n<p>(n_clusters, n_features)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>labels_<\/p>\n<\/td>\n<td>\n<p>\u6bcf\u4e2a\u6837\u672c\u7684\u7c07\u6807\u7b7e<\/p>\n<\/td>\n<td>\n<p>(n_samples,)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>inertia_<\/p>\n<\/td>\n<td>\n<p>\u6837\u672c\u5230\u6700\u8fd1\u7c07\u4e2d\u5fc3\u7684\u5e73\u65b9\u8ddd\u79bb\u548c<\/p>\n<\/td>\n<td>\n<p>\u6807\u91cf<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>n_iter_<\/p>\n<\/td>\n<td>\n<p>\u5b9e\u9645\u8fed\u4ee3\u6b21\u6570<\/p>\n<\/td>\n<td>\n<p>\u6807\u91cf<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u56db\u3001\u6838\u5fc3\u65b9\u6cd5<\/h3>\n<h4>1. \u8bad\u7ec3\u6a21\u578b<\/h4>\n<p>fit(X, y&#061;None, sample_weight&#061;None)<\/p>\n<ul>\n<li>\n<p>X&#xff1a;\u8bad\u7ec3\u6570\u636e&#xff0c;\u5f62\u72b6 (n_samples, n_features)<\/p>\n<\/li>\n<li>\n<p>sample_weight&#xff1a;\u6837\u672c\u6743\u91cd&#xff0c;\u5f62\u72b6 (n_samples,)<\/p>\n<\/li>\n<\/ul>\n<h4>2. \u9884\u6d4b\u7c07\u6807\u7b7e<\/h4>\n<p>predict(X, sample_weight&#061;None)<\/p>\n<p>\u8fd4\u56de\u6bcf\u4e2a\u6837\u672c\u6240\u5c5e\u7684\u7c07<\/p>\n<h4>3. \u8bad\u7ec3\u5e76\u9884\u6d4b<\/h4>\n<p>fit_predict(X, y&#061;None, sample_weight&#061;None)<\/p>\n<p>\u4e00\u6b65\u5b8c\u6210\u8bad\u7ec3\u548c\u9884\u6d4b<\/p>\n<h4>4. \u6a21\u578b\u8bc4\u5206<\/h4>\n<p>score(X, y&#061;None, sample_weight&#061;None)<\/p>\n<p>\u8fd4\u56de\u8d1f\u7684inertia\u503c&#xff08;\u8d8a\u5927\u8d8a\u597d&#xff09;<\/p>\n<p>from sklearn.cluster import KMeans<br \/>\nfrom sklearn.datasets import make_blobs<br \/>\nimport matplotlib.pyplot as plt<\/p>\n<p># \u751f\u6210\u6837\u672c\u6570\u636e<br \/>\nX, _ &#061; make_blobs(n_samples&#061;300, centers&#061;4, random_state&#061;42)<\/p>\n<p># \u521b\u5efaKMeans\u5b9e\u4f8b<br \/>\nkmeans &#061; KMeans(<br \/>\n    n_clusters&#061;4,<br \/>\n    init&#061;&#039;k-means&#043;&#043;&#039;,<br \/>\n    n_init&#061;10,<br \/>\n    max_iter&#061;300,<br \/>\n    random_state&#061;42<br \/>\n)<\/p>\n<p># \u8bad\u7ec3\u6a21\u578b<br \/>\nkmeans.fit(X)<\/p>\n<p># \u53ef\u89c6\u5316\u7ed3\u679c<br \/>\nplt.scatter(X[:, 0], X[:, 1], c&#061;kmeans.labels_, cmap&#061;&#039;viridis&#039;)<br \/>\nplt.scatter(kmeans.cluster_centers_[:, 0],<br \/>\n            kmeans.cluster_centers_[:, 1],<br \/>\n            s&#061;200, marker&#061;&#039;*&#039;, c&#061;&#039;red&#039;)<br \/>\nplt.title(&#039;K-means Clustering&#039;)<br \/>\nplt.show()<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb125\u6b21\u3002\u65e0\u76d1\u7763\u5b66\u4e60\u5904\u7406\u200b\u200b\u6ca1\u6709\u9884\u5148\u6807\u8bb0\u200b\u200b\u7684\u6570\u636e\u96c6\uff0c\u76ee\u6807\u662f\uff1a\u53d1\u73b0\u6570\u636e\u4e2d\u7684\u200b\u200b\u9690\u85cf\u6a21\u5f0f\u200b\u200b\u8bc6\u522b\u200b\u200b\u5185\u5728\u7ed3\u6784\u200b\u200b\u63d0\u53d6\u200b\u200b\u6709\u610f\u4e49\u7279\u5f81\u200b\u200b\u3002<\/p>\n","protected":false},"author":2,"featured_media":53008,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[371,2792,207],"topic":[],"class_list":["post-53010","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-371","tag-2792","tag-207"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u673a\u5668\u5b66\u4e60\u2014\u2014K-means\u805a\u7c7b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/53010.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u673a\u5668\u5b66\u4e60\u2014\u2014K-means\u805a\u7c7b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb125\u6b21\u3002\u65e0\u76d1\u7763\u5b66\u4e60\u5904\u7406\u200b\u200b\u6ca1\u6709\u9884\u5148\u6807\u8bb0\u200b\u200b\u7684\u6570\u636e\u96c6\uff0c\u76ee\u6807\u662f\uff1a\u53d1\u73b0\u6570\u636e\u4e2d\u7684\u200b\u200b\u9690\u85cf\u6a21\u5f0f\u200b\u200b\u8bc6\u522b\u200b\u200b\u5185\u5728\u7ed3\u6784\u200b\u200b\u63d0\u53d6\u200b\u200b\u6709\u610f\u4e49\u7279\u5f81\u200b\u200b\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/53010.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-11T14:44:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811144443-689a01db32057.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/53010.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/53010.html\",\"name\":\"\u673a\u5668\u5b66\u4e60\u2014\u2014K-means\u805a\u7c7b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-11T14:44:44+00:00\",\"dateModified\":\"2025-08-11T14:44:44+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/53010.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/53010.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/53010.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u673a\u5668\u5b66\u4e60\u2014\u2014K-means\u805a\u7c7b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u673a\u5668\u5b66\u4e60\u2014\u2014K-means\u805a\u7c7b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/53010.html","og_locale":"zh_CN","og_type":"article","og_title":"\u673a\u5668\u5b66\u4e60\u2014\u2014K-means\u805a\u7c7b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb125\u6b21\u3002\u65e0\u76d1\u7763\u5b66\u4e60\u5904\u7406\u200b\u200b\u6ca1\u6709\u9884\u5148\u6807\u8bb0\u200b\u200b\u7684\u6570\u636e\u96c6\uff0c\u76ee\u6807\u662f\uff1a\u53d1\u73b0\u6570\u636e\u4e2d\u7684\u200b\u200b\u9690\u85cf\u6a21\u5f0f\u200b\u200b\u8bc6\u522b\u200b\u200b\u5185\u5728\u7ed3\u6784\u200b\u200b\u63d0\u53d6\u200b\u200b\u6709\u610f\u4e49\u7279\u5f81\u200b\u200b\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/53010.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-11T14:44:44+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811144443-689a01db32057.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/53010.html","url":"https:\/\/www.wsisp.com\/helps\/53010.html","name":"\u673a\u5668\u5b66\u4e60\u2014\u2014K-means\u805a\u7c7b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-11T14:44:44+00:00","dateModified":"2025-08-11T14:44:44+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/53010.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/53010.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/53010.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u673a\u5668\u5b66\u4e60\u2014\u2014K-means\u805a\u7c7b"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/53010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=53010"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/53010\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/53008"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=53010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=53010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=53010"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=53010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}