赞
踩
- import numpy as np
-
- class KMeans:
- def __init__(self, n_clusters=2, max_iter=300):
- self.n_clusters = n_clusters # 聚类数目
- self.max_iter = max_iter # 最大迭代次数
-
- def fit(self, X):
- # 初始化聚类中心
- self.centroids = X[np.random.choice(X.shape[0], self.n_clusters, replace=False)]
-
- for _ in range(self.max_iter):
- # 计算每个样本到聚类中心的距离
- distances = np.sqrt(((X - self.centroids[:, np.newaxis])**2).sum(axis=2))
-
- # 将样本分配到最近的聚类中心
- labels = np.argmin(distances, axis=0)
-
- # 更新聚类中心
- new_centroids = np.array([X[labels == i].mean(axis=0) for i in range(self.n_clusters)])
-
- # 判断聚类中心是否变化小于阈值
- if np.allclose(self.centroids, new_centroids):
- break
- else:
- self.centroids = new_centroids
-
- return labels
-
- # 示例数据
- X = np.array([[1, 2], [5, 8], [1.5, 1.8], [8, 8], [1, 0.6], [9, 11]])
-
- # 聚类
- kmeans = KMeans(n_clusters=2)
- labels = kmeans.fit(X)
-
- print("聚类结果:", labels)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。