Python集群“纯净”metri

2024-05-16 23:36:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用sklearn.mixture中的Gaussian Mixture Model (GMM)对我的数据集执行集群。

我可以使用函数score()来计算模型下的对数概率。

然而,我正在寻找一个称为“纯度”的度量,它在this article中定义。

如何在Python中实现它?我当前的实现如下所示:

from sklearn.mixture import GMM

# X is a 1000 x 2 array (1000 samples of 2 coordinates).
# It is actually a 2 dimensional PCA projection of data
# extracted from the MNIST dataset, but this random array
# is equivalent as far as the code is concerned.
X = np.random.rand(1000, 2)

clusterer = GMM(3, 'diag')
clusterer.fit(X)
cluster_labels = clusterer.predict(X)

# Now I can count the labels for each cluster..
count0 = list(cluster_labels).count(0)
count1 = list(cluster_labels).count(1)
count2 = list(cluster_labels).count(2)

但是我不能通过循环遍历每个集群来计算混淆矩阵(根据这个question


Tags: ofthefromlabelsiscount集群sklearn
3条回答

sklearn未实现群集纯度度量。你有两个选择:

  1. 自己使用sklearn数据结构实现度量。Thisthis有一些python源代码可用于测量纯度,但您的数据或函数体都需要进行调整,以便彼此兼容。

  2. 使用(不太成熟的)PML库,它确实实现了集群纯度。

大卫的回答很有效,但这里有另一种方法。

import numpy as np
from sklearn import metrics

def purity_score(y_true, y_pred):
    # compute contingency matrix (also called confusion matrix)
    contingency_matrix = metrics.cluster.contingency_matrix(y_true, y_pred)
    # return purity
    return np.sum(np.amax(contingency_matrix, axis=0)) / np.sum(contingency_matrix) 

另外,如果需要计算反纯度,只需将“axis=0”替换为“axis=1”

迟交的稿件。

您可以尝试像这样实现它,就像在这个gist

def purity_score(y_true, y_pred):
    """Purity score
        Args:
            y_true(np.ndarray): n*1 matrix Ground truth labels
            y_pred(np.ndarray): n*1 matrix Predicted clusters

        Returns:
            float: Purity score
    """
    # matrix which will hold the majority-voted labels
    y_voted_labels = np.zeros(y_true.shape)
    # Ordering labels
    ## Labels might be missing e.g with set like 0,2 where 1 is missing
    ## First find the unique labels, then map the labels to an ordered set
    ## 0,2 should become 0,1
    labels = np.unique(y_true)
    ordered_labels = np.arange(labels.shape[0])
    for k in range(labels.shape[0]):
        y_true[y_true==labels[k]] = ordered_labels[k]
    # Update unique labels
    labels = np.unique(y_true)
    # We set the number of bins to be n_classes+2 so that 
    # we count the actual occurence of classes between two consecutive bins
    # the bigger being excluded [bin_i, bin_i+1[
    bins = np.concatenate((labels, [np.max(labels)+1]), axis=0)

    for cluster in np.unique(y_pred):
        hist, _ = np.histogram(y_true[y_pred==cluster], bins=bins)
        # Find the most present label in the cluster
        winner = np.argmax(hist)
        y_voted_labels[y_pred==cluster] = winner

    return accuracy_score(y_true, y_voted_labels)

相关问题 更多 >