python - 聚类之间的距离 kmeans sklearn python

标签 python scikit-learn distance k-means

我正在使用 sklearn 的 k-means 聚类来聚类我的数据。现在我想知道我的集群之间的距离,但找不到。我可以计算每个质心之间的距离,但想知道是否有获取它的函数以及是否有办法获取每个簇之间的最小/最大/平均链接距离。我的代码很简单:

km = KMeans(n_clusters = 5, random_state = 1)
km.fit(X_tfidf )

clusterkm = km.cluster_centers_

clusters = km.labels_.tolist()

谢谢!

最佳答案

不幸的是,您将不得不自己计算集群中心的这些距离。 Scikit 没有提供开箱即用的方法。这是一个类似的问题设置:

from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
from sklearn.metrics.pairwise import euclidean_distances

X, y = load_iris(return_X_y=True)
km = KMeans(n_clusters = 5, random_state = 1).fit(X)

以及如何计算距离:

dists = euclidean_distances(km.cluster_centers_)

然后为了获得您感兴趣的统计数据,您只需计算距离矩阵的上(或下)三角角:

import numpy as np
tri_dists = dists[np.triu_indices(5, 1)]
max_dist, avg_dist, min_dist = tri_dists.max(), tri_dists.mean(), tri_dists.min()

关于python - 聚类之间的距离 kmeans sklearn python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51729851/

相关文章:

python-3.x - 导入错误 : cannot import name '_deprecate_positional_args' from 'sklearn.utils.validation'

python - 当我有高度不平衡的数据时,我应该平衡测试集吗?

algorithm - 算法的距离度量

python - 将方法添加到python中加载的类/模块

Python 数组组计数

python - heapq.nlargest 的时间复杂度是多少?

google-api - Google places api关闭超市

python - 你能在 kivy 文件中换行吗?

python - 在多个程序中正确使用 Scikit 的 LabelEncoder

没有三角函数的 SQL 距离查询