机器学习-Kmeans算法的sklearn实现

from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt  # 可视化数据

# 生成数据
n_samples = 200
n_clusters = 3
random_state = 42
X, y = make_blobs(n_samples=n_samples, centers=n_clusters, random_state=random_state)

# 使用KMeans算法
kmeans = KMeans(n_clusters=n_clusters, random_state=random_state)
kmeans.fit(X)

# 输出聚类结果
print("Cluster labels:", kmeans.labels_)
print("Cluster centers:", kmeans.cluster_centers_)

plt.scatter(X[:, 0], X[:, 1], c=y, cmap='viridis', label='Points')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], c='red', marker='x', label='Centers')
plt.xlabel('X1')
plt.ylabel('X2')
plt.legend()
plt.show()

本文作者:碧水云天4

本文链接:https://www.cnblogs.com/smalldong/p/17945121

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   碧水云天4  阅读(35)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 love songs Audio artist
  2. 2 love songs Audio artist
  3. 3 love songs Audio artist
  4. 4 love songs Audio artist
love songs - Audio artist
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.