【发布时间】:2015-05-15 16:44:54
【问题描述】:
我有一个数组,其中包含分配给每个点的簇。
import numpy as np
cluster_labels = np.array([1,1,2,3,4])
我怎样才能得到这样的矩阵:
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
我敢肯定有什么比:
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的平均值:
n_cluster_labels = np.array([[1,1,2,3,4],
[1,2,3,3,4],
[1,1,2,3,4]])
【问题讨论】: