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]])