简体   繁体   English

Python中的Pairwise Kullback Leibler(或Jensen-Shannon)发散距离矩阵

[英]Pairwise Kullback Leibler (or Jensen-Shannon) divergence distance matrix in Python

I have two matrices X and Y (in most of my cases they are similar) Now I want to calculate the pairwise KL divergence between all rows and output them in a matrix. 我有两个矩阵X和Y(在我的大多数情况下它们是相似的)现在我想计算所有行之间的成对KL分歧并将它们输出到矩阵中。 Eg: 例如:

X = [[0.1, 0.9], [0.8, 0.2]]

The function should then take kl_divergence(X, X) and compute the pairwise Kl divergence distance for each pair of rows of both X matrices. 然后该函数应采用kl_divergence(X, X)并计算两个X矩阵的每对行的成对K1发散距离。 The output would be a 2x2 matrix. 输出将是2x2矩阵。

Is already some implementation for this in Python? 在Python中已经有了一些实现吗? If not, this should be quite simple to calculate. 如果没有,这应该很容易计算。 I'd like some kind of matrix implementation for this, because I have a lot of data and need to keep the runtime as low as possible. 我想要一些矩阵实现,因为我有很多数据,需要尽可能地保持运行时间。 Alternatively the Jensen-Shannon entropy is also fine. 或者,Jensen-Shannon熵也很好。 Eventually this would even be a better solution for me. 最终这对我来说甚至是更好的解决方案。

Note that KL divergence is essentially a dot product of P(i) and log(P(i)/Q(i)). 注意, KL散度基本上是P(i)和log(P(i)/ Q(i))的点积。 So, one option is to form a list of numpy arrays for P(i) and another for log(P(i)/Q(i)), one row for each KL divergence you want to calculate), then perform dot-products. 因此,一个选项是为P(i)形成一个numpy数组列表,为log(P(i)/ Q(i))形成另一个数组,每个KL分歧需要计算一行,然后执行点积。

There is a new(ish) library called dit which has JSD implemented, as well as mutual information and many other distance metrics: 有一个名为dit的新(ish)库,它实现了JSD,以及互信息和许多其他距离度量:

import dit
foo = dit.Distribution(['A','B','C'],[0.5,0.5,0.0])
bar = dit.Distribution(['A','B','C'],[0.1,0.0,0.9])
dit.divergences.jensen_shannon_divergence([foo,bar])
0.80499327350549388

The docs could use a bit of work, but it looks promising. 文档可以使用一些工作,但看起来很有希望。

http://docs.dit.io/en/latest/generalinfo.html#quickstart http://docs.dit.io/en/latest/generalinfo.html#quickstart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM