Matrix of matches from an array with cluster tasks

I have an array containing a cluster assigned to each point.

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

How to 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 am 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.]]

Edit (bonus): I am interested in the average pair clustering of the set $ n $ cluster_labels. Therefore, I would like to get the average value of parwise_clustering directly from an array from the set cluster_labels:

n_cluster_labels = np.array([[1,1,2,3,4],
                             [1,2,3,3,4],
                             [1,1,2,3,4]])
0
source share
1 answer

It is hard to say whether what you are doing is the best way to solve a problem without knowing more about the problem itself.

However, you can get the matrix you are looking for in much less code:

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

, . numpy python for-loops.

x x[None,:] 1. numpy . . ( int float, ).

+1

Source: https://habr.com/ru/post/1689789/


All Articles