【问题标题】:Computing probability of a class from cluster labels in hierarchical clustering?从层次聚类中的聚类标签计算类的概率?
【发布时间】:2019-01-04 12:16:24
【问题描述】:

我有一个包含两个类“是”和“否”的数据框。使用 scipy Hiererchical clustering 我发现了 2 个集群。这是我的代码

from scipy.cluster.hierarchy import linkage, dendrogram
from scipy.spatial.distance import pdist
from scipy.cluster.hierarchy import fcluster
Mdist_matrix = pdist(x_Minmax, metric= 'cityblock')
MSlink = linkage (Mdist_matrix , method = 'single' , metric = 'cityblock')
crsm = fcluster(MClink, k , criterion='maxclust')
arr = np.unique(crsm, return_counts = True)
# print(arr)
dfcluster= dfcluster.copy()
dfcluster['Clabels'] = pd.Series(crsm, index=dfcluster.index)
No = dfcluster[df['status'] == 0]['Clabels'].value_counts()
print("CNO\n",No)
Yes= dfcluster[df['status'] == 1]['Clabels'].value_counts()
print("Cyes\n",Yes)

The output looks like this one

我想计算每个集群的熵和集群的纯度。如何计算每个集群中“是”和“否”的概率? 我试图以这种方式做到这一点Fastest way to compute entropy in python,但我不清楚。

【问题讨论】:

    标签: python scipy hierarchical-clustering entropy


    【解决方案1】:

    我回答纯洁。 您的应急矩阵(如果您不熟悉,请参阅this)如下:

          |   1  |   2 |
    ------|------|-----|
     CNO  | 7244 | 544 |
    ------|------|-----|
     CYES | 2136 |  76 |
    -------------------+
    

    那么,有一个公式可以从列联矩阵计算纯度:

    purity_score = np.sum(np.amax(contingency_matrix, axis=0)) / np.sum(contingency_matrix)
    

    【讨论】:

    • 明白了!谢谢。
    猜你喜欢
    • 2018-10-10
    • 1970-01-01
    • 2021-07-21
    • 2018-06-04
    • 2013-05-08
    • 2016-08-07
    • 2020-06-28
    • 2014-06-28
    • 2021-02-11
    相关资源
    最近更新 更多