使用scikit-learn剪影得分计算scipy的fcluster的Silhouette Score

时间:2016-05-11 19:04:02

标签: scipy scikit-learn cluster-analysis silhouette

我正在使用scipy.cluster进行层次结构聚类,然后在不同的截止值下使用fcluster。 我也想使用scikit的silhouette_score。 我看到帖子How to calculate Silhouette Score of the scipy's fcluster using scikit-learn silhouette score? 但是,我得到了错误"太多的布尔索引" ??

我的代码如下:

import fastcluster
from sklearn import metrics
from scipy.cluster import hierarchy as hac


Temps=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
Distance=[]
#read the Distance obtained as a list then
Distances=np.array(Distances)
Z=fastcluster.linkage(Distances, "complete", "euclidean")
for Cutoff in Temps:
    results=hac.fcluster(Z,Cutoff,'distance')
    metrics.silhouette_score(Distances, results, metric="euclidean")

错误报告是:

Traceback (most recent call last):
  File "Clustering_2.py", line 93, in <module>
    main(argv)
  File "Clustering_2.py", line 69, in main
    silscore=metrics.silhouette_score(Distances, results,metric='euclidean')
  File "/home/wangz18/site-packages2/sklearn/metrics/cluster/unsupervised.py", line 93, in silhouette_score
    return np.mean(silhouette_samples(X, labels, metric=metric, **kwds))
  File "/home/wangz18/site-packages2/sklearn/metrics/cluster/unsupervised.py", line 157, in silhouette_samples
    for i in range(n)])
  File "/home/wangz18/site-packages2/sklearn/metrics/cluster/unsupervised.py", line 187, in _intra_cluster_distance
    a = np.mean(distances_row[mask])
ValueError: too many boolean indices

问题是什么?请指教。感谢

1 个答案:

答案 0 :(得分:0)

我有相同的问题,请检查:

  1. 距离为N * N,N为样本数

  2. 结果为N,值是簇的类

  3. 群集数应大于1

如果#1和#2是正确的,则它们应该是正确的。