{"id":6071,"date":"2024-05-29T15:10:50","date_gmt":"2024-05-29T07:10:50","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6071"},"modified":"2024-05-29T15:10:51","modified_gmt":"2024-05-29T07:10:51","slug":"kmeans-%e8%81%9a%e7%b1%bb%e7%ae%97%e6%b3%95%e5%85%b7%e4%bd%93%e4%be%8b%e5%ad%90","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6071","title":{"rendered":"KMeans \u805a\u7c7b\u7b97\u6cd5\u5177\u4f53\u4f8b\u5b50"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u975e\u539f\u521b\uff0cChatGPT \u95ee\u7b54\u751f\u6210<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u597d\u7684\uff0c\u8fd9\u91cc\u662f\u4e00\u4e2a\u4f7f\u7528 <code>KMeans<\/code> \u7b97\u6cd5\u7684\u5177\u4f53\u793a\u4f8b\uff0c\u4f7f\u7528 Python \u7684 <code>sklearn<\/code> \u5e93\u3002\u8fd9\u4e2a\u793a\u4f8b\u5c06\u751f\u6210\u4e00\u4e9b\u4e8c\u7ef4\u6570\u636e\u70b9\uff0c\u5e76\u4f7f\u7528 KMeans \u805a\u7c7b\u7b97\u6cd5\u5bf9\u5176\u8fdb\u884c\u805a\u7c7b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86 <code>numpy<\/code>\u3001<code>matplotlib<\/code> \u548c <code>scikit-learn<\/code> \u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install numpy matplotlib scikit-learn<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u751f\u6210\u793a\u4f8b\u6570\u636e<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u5c06\u4f7f\u7528 <code>numpy<\/code> \u751f\u6210\u4e00\u4e9b\u793a\u4f8b\u6570\u636e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4f7f\u7528 KMeans \u8fdb\u884c\u805a\u7c7b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u5c06\u4f7f\u7528 <code>KMeans<\/code> \u5bf9\u751f\u6210\u7684\u6570\u636e\u8fdb\u884c\u805a\u7c7b\uff0c\u5e76\u53ef\u89c6\u5316\u7ed3\u679c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.cluster import KMeans\n\n# \u751f\u6210\u793a\u4f8b\u6570\u636e\nnp.random.seed(42)\nX = np.vstack(&#91;\n    np.random.normal(loc=&#91;2, 2], scale=0.5, size=(100, 2)),\n    np.random.normal(loc=&#91;-2, -2], scale=0.5, size=(100, 2)),\n    np.random.normal(loc=&#91;2, -2], scale=0.5, size=(100, 2)),\n    np.random.normal(loc=&#91;-2, 2], scale=0.5, size=(100, 2))\n])\n\n# \u4f7f\u7528 KMeans \u8fdb\u884c\u805a\u7c7b\nkmeans = KMeans(n_clusters=4, random_state=42)\nkmeans.fit(X)\nlabels = kmeans.labels_\ncenters = kmeans.cluster_centers_\n\n# \u53ef\u89c6\u5316\u805a\u7c7b\u7ed3\u679c\nplt.figure(figsize=(8, 6))\nplt.scatter(X&#91;:, 0], X&#91;:, 1], c=labels, cmap='viridis', marker='o', alpha=0.5, label='Data Points')\nplt.scatter(centers&#91;:, 0], centers&#91;:, 1], c='red', marker='x', s=100, label='Cluster Centers')\nplt.title('KMeans Clustering Example')\nplt.xlabel('Feature 1')\nplt.ylabel('Feature 2')\nplt.legend()\nplt.show()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u8bf4\u660e<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong>\uff1a <code>np.random.seed(42) X = np.vstack([ np.random.normal(loc=[2, 2], scale=0.5, size=(100, 2)), np.random.normal(loc=[-2, -2], scale=0.5, size=(100, 2)), np.random.normal(loc=[2, -2], scale=0.5, size=(100, 2)), np.random.normal(loc=[-2, 2], scale=0.5, size=(100, 2)) ])<\/code> \u8fd9\u6bb5\u4ee3\u7801\u751f\u6210\u4e86\u56db\u7ec4\u4e8c\u7ef4\u6570\u636e\u70b9\uff0c\u6bcf\u7ec4\u6570\u636e\u70b9\u56f4\u7ed5\u4e00\u4e2a\u4e2d\u5fc3\u70b9\u5206\u5e03\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528 KMeans \u8fdb\u884c\u805a\u7c7b<\/strong>\uff1a <code>kmeans = KMeans(n_clusters=4, random_state=42) kmeans.fit(X) labels = kmeans.labels_ centers = kmeans.cluster_centers_<\/code> \u8fd9\u91cc\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a KMeans \u5bf9\u8c61\uff0c\u5e76\u5c06 <code>n_clusters<\/code> \u8bbe\u7f6e\u4e3a 4\uff0c\u5373\u6211\u4eec\u671f\u671b\u6570\u636e\u5206\u6210\u56db\u4e2a\u7c07\u3002<code>fit<\/code> \u65b9\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u805a\u7c7b\uff0c<code>labels<\/code> \u5b58\u50a8\u6bcf\u4e2a\u6570\u636e\u70b9\u6240\u5c5e\u7684\u7c07\u6807\u7b7e\uff0c<code>centers<\/code> \u5b58\u50a8\u6bcf\u4e2a\u7c07\u7684\u4e2d\u5fc3\u70b9\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u89c6\u5316\u805a\u7c7b\u7ed3\u679c<\/strong>\uff1a <code>plt.figure(figsize=(8, 6)) plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', marker='o', alpha=0.5, label='Data Points') plt.scatter(centers[:, 0], centers[:, 1], c='red', marker='x', s=100, label='Cluster Centers') plt.title('KMeans Clustering Example') plt.xlabel('Feature 1') plt.ylabel('Feature 2') plt.legend() plt.show()<\/code> \u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528 Matplotlib \u53ef\u89c6\u5316\u805a\u7c7b\u7ed3\u679c\u3002\u6570\u636e\u70b9\u7528\u4e0d\u540c\u989c\u8272\u8868\u793a\u4e0d\u540c\u7684\u7c07\uff0c\u7c07\u7684\u4e2d\u5fc3\u70b9\u7528\u7ea2\u8272 <code>x<\/code> \u6807\u8bb0\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u4ee5\u4e0a\u4ee3\u7801\uff0c\u5c06\u4f1a\u751f\u6210\u4e00\u4e2a\u5305\u542b\u56db\u4e2a\u7c07\u7684\u4e8c\u7ef4\u6563\u70b9\u56fe\uff0c\u6bcf\u4e2a\u7c07\u7528\u4e0d\u540c\u7684\u989c\u8272\u8868\u793a\uff0c\u7c07\u4e2d\u5fc3\u7528\u7ea2\u8272 <code>x<\/code> \u6807\u8bb0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\uff0cChatGPT \u95ee\u7b54\u751f\u6210 \u597d\u7684\uff0c\u8fd9\u91cc\u662f\u4e00\u4e2a\u4f7f\u7528 KMeans \u7b97\u6cd5\u7684\u5177\u4f53\u793a\u4f8b\uff0c\u4f7f\u7528 Python \u7684 &#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6071\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-6071","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6071"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6071\/revisions"}],"predecessor-version":[{"id":6072,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6071\/revisions\/6072"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}