Python实现K-Means聚类实例
时间: 2023-06-14 11:08:04 浏览: 245
以下是Python实现K-Means聚类的代码示例:
```python
import numpy as np
from matplotlib import pyplot as plt
# 生成数据点
X = np.array([[1, 2], [1.5, 1.8], [5, 8], [8, 8], [1, 0.6], [9, 11]])
# 设定初始聚类中心
centroids = np.array([[1, 0.6], [1.5, 1.8], [5, 8]])
# 定义距离函数
def euclidean_distance(x1, x2):
return np.sqrt(np.sum((x1 - x2) ** 2))
# 定义K-Means函数
def k_means(X, k=3, max_iters=100):
# 随机初始化聚类中心
idx = np.random.choice(len(X), k, replace=False)
centroids = X[idx]
# 迭代更新聚类中心
for i in range(max_iters):
# 分配数据点到最近的聚类中心
clusters = [[] for _ in range(k)]
for x in X:
distances = [euclidean_distance(x, c) for c in centroids]
cluster_idx = np.argmin(distances)
clusters[cluster_idx].append(x)
# 计算新的聚类中心
new_centroids = []
for c in clusters:
mean = np.mean(c, axis=0)
new_centroids.append(mean)
new_centroids = np.array(new_centroids)
# 判断是否收敛
if np.all(centroids == new_centroids):
break
centroids = new_centroids
return centroids, clusters
# 调用K-Means函数
centroids, clusters = k_means(X, k=3)
# 可视化聚类结果
colors = ['r', 'g', 'b']
for i, c in enumerate(clusters):
for x in c:
plt.scatter(x[0], x[1], color=colors[i])
plt.scatter(centroids[:,0], centroids[:,1], marker='*', s=200, color='black')
plt.show()
```
运行上述代码,可以得到类似于以下图像的聚类结果:
![K-Means聚类结果](https://img-blog.csdn.net/20180721090548206?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2JqMjM4NzA1MjYwNjg=//font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/80)
阅读全文