不调包实现kmeans聚类,并画出聚类结果图 python
时间: 2024-06-19 18:03:02 浏览: 116
K-Means是一种经典的聚类算法,可以将数据集划分为预定数量的簇。下面是不调用Python库实现K-Means聚类的代码(假设数据集为X,聚类数为k):
```
import numpy as np
# 计算两个向量之间的欧式距离
def dist(x1, x2):
return np.sqrt(np.sum((x1-x2)**2))
# 初始化聚类中心
def init_centers(X, k):
centers = X[np.random.choice(range(len(X)), k, replace=False)]
return centers
# 进行K-Means聚类
def kmeans(X, k):
centers = init_centers(X, k)
while True:
# 计算每个样本距离最近的聚类中心,并将样本归入对应簇
clusters = [[] for _ in range(k)]
for i, x in enumerate(X):
distances = [dist(x, center) for center in centers]
cluster_index = np.argmin(distances)
clusters[cluster_index].append(i)
# 计算新的聚类中心
new_centers = np.zeros_like(centers)
for i, cluster in enumerate(clusters):
cluster_mean = np.mean(X[cluster], axis=0)
new_centers[i] = cluster_mean
# 如果聚类中心不再改变,则停止迭代
if np.allclose(new_centers, centers):
break
else:
centers = new_centers
return clusters
# 生成数据集
X = np.concatenate([np.random.randn(100, 2) + [-5, -5],
np.random.randn(100, 2) + [5, 5],
np.random.randn(100, 2) + [-5, 5],
np.random.randn(100, 2) + [5, -5]])
# 进行K-Means聚类
clusters = kmeans(X, k=4)
# 绘制聚类结果图
import matplotlib.pyplot as plt
colors = ['r', 'g', 'b', 'y']
for i, cluster in enumerate(clusters):
for j in cluster:
plt.scatter(X[j, 0], X[j, 1], c=colors[i])
plt.show()
```
阅读全文