0

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?

Hirak Sarkar
  • 479
  • 1
  • 4
  • 18

1 Answers1

0

Ok, after massaging the equation for KL divergence a bit the following equation should work too and of course, it's magnitudes faster,

kl = np.dot(V, np.log(Vc).T)
right = kl + kl.T
left = np.tile(np.diag(kl),(kl.shape[0],1))
left = left + left.T
L = left - right
Hirak Sarkar
  • 479
  • 1
  • 4
  • 18