我想找出矩阵的所有行对之间的 KL 散度。为了解释,让我们假设有一个V
shape矩阵N x K
。现在我想创建一个L
维度矩阵N x N
,其中每个元素L[i,j] = KL(V[i,:],V[j,:])
。到目前为止,我已经使用以下scipy.stats.entropy
来计算
upper_triangle = [entropy(V[i,:],V[j,:]) for (i,j) in itertools.combinations(range(N,2)]
lower_triangle = [entropy(V[j,:],V[i,:]) for (i,j) in itertools.combinations(range(N,2)]
L = np.zeroes((N,N))
L[np.triu_indices(N,k = 1)] = upper_triangle
L[np.tril_indices(N,k = -1)] = lower_triangle
有没有更聪明的方法?