{"id":614,"date":"2023-11-09T18:02:27","date_gmt":"2023-11-09T10:02:27","guid":{"rendered":"http:\/\/ai.gitpp.com\/?p=614"},"modified":"2023-11-09T18:02:27","modified_gmt":"2023-11-09T10:02:27","slug":"k-means-%e8%81%9a%e7%b1%bb%e7%ae%97%e6%b3%95%e5%9c%a8-python-%e4%b8%ad","status":"publish","type":"post","link":"http:\/\/ai.gitpp.com\/index.php\/2023\/11\/09\/k-means-%e8%81%9a%e7%b1%bb%e7%ae%97%e6%b3%95%e5%9c%a8-python-%e4%b8%ad\/","title":{"rendered":"K-Means \u805a\u7c7b\u7b97\u6cd5\u5728 Python \u4e2d"},"content":{"rendered":"\n<p>K-Means \u805a\u7c7b\u7b97\u6cd5\u5728 Python \u4e2d\u7684\u5b9e\u73b0\u53ef\u4ee5\u4f7f\u7528 scikit-learn \u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 K-Means \u805a\u7c7b\u793a\u4f8b\uff1a<br>&#8220;`python<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u5bfc\u5165\u6240\u9700\u5e93<\/h1>\n\n\n\n<p>from sklearn.cluster import KMeans<br>from sklearn.datasets import load_iris<br>from sklearn.model_selection import train_test_split<br>import matplotlib.pyplot as plt<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u52a0\u8f7d\u6570\u636e\u96c6<\/h1>\n\n\n\n<p>iris = load_iris()<br>X = iris.data<br>y = iris.target<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u5212\u5206\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6<\/h1>\n\n\n\n<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u521d\u59cb\u5316 K-Means \u7b97\u6cd5\u53c2\u6570<\/h1>\n\n\n\n<p>kmeans = KMeans(n_clusters=3, random_state=42)<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u8bad\u7ec3\u6a21\u578b<\/h1>\n\n\n\n<p>kmeans.fit(X_train)<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u9884\u6d4b\u6d4b\u8bd5\u96c6\u6570\u636e<\/h1>\n\n\n\n<p>y_pred = kmeans.predict(X_test)<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7ed8\u5236\u805a\u7c7b\u7ed3\u679c<\/h1>\n\n\n\n<p>plt.scatter(X_test[:, 0], X_test[:, 1], c=y_pred, cmap=&#8217;viridis&#8217;)<br>plt.xlabel(&#8216;Petal Length&#8217;)<br>plt.ylabel(&#8216;Petal Width&#8217;)<br>plt.title(&#8216;Iris Dataset Clustered by K-Means&#8217;)<br>plt.show()<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u8f93\u51fa\u6bcf\u4e2a\u7c07\u7684\u8d28\u5fc3<\/h1>\n\n\n\n<p>print(&#8220;Cluster centroids:&#8221;)<br>print(kmeans.cluster_centers_)<br><code>`` \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86\u6240\u9700\u7684\u5e93\u548c\u6570\u636e\u96c6\u3002\u7136\u540e\uff0c\u4f7f\u7528<\/code>train_test_split<code>\u5212\u5206\u6570\u636e\u96c6\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u3002\u63a5\u4e0b\u6765\uff0c\u521d\u59cb\u5316 K-Means \u7b97\u6cd5\u53c2\u6570\uff0c\u5305\u62ec\u805a\u7c7b\u6570\u91cf\uff083 \u4e2a\u7c07\uff09\u548c\u968f\u673a\u79cd\u5b50\uff0842\uff09\u3002 \u8bad\u7ec3\u6a21\u578b\u540e\uff0c\u4f7f\u7528<\/code>predict` \u65b9\u6cd5\u5bf9\u6d4b\u8bd5\u96c6\u6570\u636e\u8fdb\u884c\u9884\u6d4b\u3002\u6700\u540e\uff0c\u7ed8\u5236\u805a\u7c7b\u7ed3\u679c\u5e76\u8f93\u51fa\u6bcf\u4e2a\u7c07\u7684\u8d28\u5fc3\u3002<br>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cK-Means \u805a\u7c7b\u7b97\u6cd5\u7684\u7ed3\u679c\u53ef\u80fd\u53d7\u5230\u521d\u59cb\u8d28\u5fc3\u9009\u62e9\u7684\u5f71\u54cd\u3002\u4e3a\u4e86\u63d0\u9ad8\u805a\u7c7b\u6027\u80fd\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u591a\u6b21\u968f\u673a\u521d\u59cb\u5316\u8d28\u5fc3\uff0c\u5e76\u4ece\u4e2d\u9009\u53d6\u6700\u4f18\u7684\u805a\u7c7b\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>K-Means \u805a\u7c7b\u7b97\u6cd5\u5728 Python \u4e2d\u7684\u5b9e\u73b0\u53ef\u4ee5\u4f7f\u7528 scikit-learn \u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 K [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-614","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"blocksy_meta":"","_links":{"self":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=614"}],"version-history":[{"count":1,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/614\/revisions"}],"predecessor-version":[{"id":615,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/614\/revisions\/615"}],"wp:attachment":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=614"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}