Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python集群&x27;纯度';米制的_Python_Scikit Learn_Cluster Analysis - Fatal编程技术网

Python集群&x27;纯度';米制的

Python集群&x27;纯度';米制的,python,scikit-learn,cluster-analysis,Python,Scikit Learn,Cluster Analysis,我正在使用fromsklearn.mixed对数据集进行聚类 我可以使用函数score()计算模型下的对数概率 然而,我正在寻找一个称为“纯度”的指标,它在中定义 如何在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 d

我正在使用from
sklearn.mixed
对数据集进行聚类

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

然而,我正在寻找一个称为“纯度”的指标,它在中定义

如何在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)

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

sklearn
没有实现集群纯度度量。您有两种选择:

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

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


  • 迟交的稿件

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


    大卫的答案是可行的,但这里有另一种方法

    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”。

    正确实现纯度度量,但在所有情况下可能不是最合适的度量,因为它不能确保每个预测的簇标签仅分配一次给真实标签

    例如,考虑一个非常不平衡的数据集,其中有一个标签的99个实例和另一个标签的1个示例。然后,任何聚类(例如:具有两个大小为50的相等聚类)都将达到至少0.99的纯度,使其成为一个无用的度量

    相反,在聚类数与标签数相同的情况下,聚类精度可能更合适。这具有在无监督环境下镜像分类精度的优势。为了计算聚类精度,我们需要使用来找到聚类标签和真实标签之间的最佳匹配。SciPy函数
    线性求和赋值
    执行以下操作:

    import numpy as np
    from sklearn import metrics
    from scipy.optimize import linear_sum_assignment
    
    def cluster_accuracy(y_true, y_pred):
        # compute contingency matrix (also called confusion matrix)
        contingency_matrix = metrics.cluster.contingency_matrix(y_true, y_pred)
    
        # Find optimal one-to-one mapping between cluster labels and true labels
        row_ind, col_ind = linear_sum_assignment(-contingency_matrix)
    
        # Return cluster accuracy
        return contingency_matrix[row_ind, col_ind].sum() / np.sum(contingency_matrix)
    

    那张纸很不透明。在crossvalidated上简化了一点过程。请发布到目前为止的代码,并告诉我们涉及的数据结构。目前,我的代码是:
    from sklearn.mixed import GMM cluster=GMM(5,'diag')cluster.fit(X)cluster\u labels=cluster.predict(X)
    为了计算纯度,我需要混淆矩阵。现在,我的问题是,我无法循环遍历每个集群,并计算每个类中有多少对象被分类。什么是
    X
    ?是numpy阵列吗?如果是,它的维度是什么?它包含哪些数据?(注意我是如何在你的问题主体中编辑代码的。从现在开始,当你有其他东西要分享时,请这样做):)是的,这是一个NumPy数组(1000L,2L)。数据是从MNIST数据集(5个类200个示例)中提取的,我将它们作为浮点类型读取。然后,为了降低维数,我计算了PCA,现在我的任务是使用GMM对X进行聚类,改变聚类数,并计算每个聚类数选择的纯度。Hi@Hadij,它并不总是给出零,但确实存在重大缺陷。我接到通知(请注意,当真正的标签无序或/或不是从零开始时,它不起作用。我已更新了该函数,并赞赏反馈这是一个非常简洁的答案,使用scikit学习自己的函数更好!我感谢您的关注。但是,纯度不是基于一对一的映射,因为通常情况下,集群和类是不同的。确实,获得纯度分数1的一个简单方法是将每个数据点放在自己的集群中。有关更多信息,请参阅或抱歉——仔细阅读,您的代码确实正确实现了链接中描述的纯度度量
    import numpy as np
    from sklearn import metrics
    from scipy.optimize import linear_sum_assignment
    
    def cluster_accuracy(y_true, y_pred):
        # compute contingency matrix (also called confusion matrix)
        contingency_matrix = metrics.cluster.contingency_matrix(y_true, y_pred)
    
        # Find optimal one-to-one mapping between cluster labels and true labels
        row_ind, col_ind = linear_sum_assignment(-contingency_matrix)
    
        # Return cluster accuracy
        return contingency_matrix[row_ind, col_ind].sum() / np.sum(contingency_matrix)