来自具有簇分配的数组的重合矩阵 [英] Coincidence matrix from array with clusters assignments

查看:76
本文介绍了来自具有簇分配的数组的重合矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,其中包含分配给每个点的聚类.

I have an array containing the cluster assigned to every point.

import numpy as np
cluster_labels = np.array([1,1,2,3,4])

我如何获得像这样的矩阵:

How can I get a matrix like:

1 1 0 0 0 
1 1 0 0 0 
0 0 1 0 0 
0 0 0 1 0 
0 0 0 0 1

我敢肯定,还有比这更聪明的东西

I'm sure there is something clever than:

import numpy as np

cluster_labels = np.array([1,1,2,3,4])
n = cluster_labels.shape[0]
pairwise_clustering = np.zeros((n, n))

for i in xrange(n):
    for j in xrange(n):
        if cluster_labels[i] == cluster_labels[j]:
            pairwise_clustering[i,j] = 1

print pairwise_clustering


[[ 1.  1.  0.  0.  0.]
 [ 1.  1.  0.  0.  0.]
 [ 0.  0.  1.  0.  0.]
 [ 0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  1.]]

编辑(奖励): 我对一组$ n $ cluster_labels的均值成对聚类感兴趣.因此,我想直接从许多cluster_labels的数组中获取pairwise_clustering的平均值:

Edit (bonus): I'm interested in the mean pairwise clustering of a set of $n$ cluster_labels. So I would like to get the mean of the pairwise_clustering directly from an array of many cluster_labels:

n_cluster_labels = np.array([[1,1,2,3,4],
                             [1,2,3,3,4],
                             [1,1,2,3,4]])

推荐答案

很难说,在不了解问题本身的情况下,做什么是解决问题的最佳方法.

It's difficult to say whether what your doing is the best way to tackle the problem without knowing more about the problem itself.

但是,可以用更少的代码获得您想要的矩阵:

However, it is possible to get the matrix your looking for in far less code:

x = np.array([1,1,2,3,4])
(x[None,:] == x[:,None]).astype(int)

从概念上讲,它与您的代码相同.它只是使用了numpy的更多功能,而不是python for循环.

Conceptually it does the same as your code. It just uses some more of numpy's features instead of python for-loops.

x索引为x[None,:]将添加长度为1的虚拟轴.然后,我们利用numpy的广播功能,并将相等的运算符逐个元素应用于广播数组.最后,我们将布尔结果转换为整数. (将int替换为float以获得浮点数).

Indexing x as x[None,:] adds a dummy axis of length 1. We then exploit numpy's broadcasting feature and apply the equal operator element-wise on the broadcasted arrays. In the end we convert the boolean result to integers. (replace int with float to get floating point numbers instead).

这篇关于来自具有簇分配的数组的重合矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆