kmeans 平均质心距离代码
时间: 2023-04-08 18:01:37 浏览: 100
matlab的Kmeans算法实现kmeans聚类算法matlab代码
以下是一个简单的 kmeans 平均质心距离的代码示例:
```python
from sklearn.cluster import KMeans
import numpy as np
# 生成一些随机数据
X = np.random.rand(100, 2)
# 使用 kmeans 进行聚类
kmeans = KMeans(n_clusters=3, random_state=0).fit(X)
# 计算平均质心距离
distances = []
for i in range(3):
cluster_points = X[kmeans.labels_ == i]
centroid = kmeans.cluster_centers_[i]
distances.append(np.mean(np.linalg.norm(cluster_points - centroid, axis=1)))
mean_distance = np.mean(distances)
print("平均质心距离为:", mean_distance)
```
希望这个代码能够帮到你!
阅读全文