I would like to find out the KL divergence between all pairs of rows of a matrix. To explain, let's assume there is a matrix V
of shape N x K
. Now I want to create a matrix L
of dimension N x N
, where each element L[i,j] = KL(V[i,:],V[j,:])
. So far I have used the following scipy.stats.entropy
to compute
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
Is there a cleverer way?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…