计算Kullback的有效方法&Python中的Leibler散度

2024-06-01 06:18:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我要计算成千上万离散概率向量之间的Kullback-Leibler Divergence(KLD)。目前我正在使用下面的代码,但对我来说太慢了。我想知道有没有更快的方法来计算KL散度?在

import numpy as np
import scipy.stats as sc

    #n is the number of data points
    kld = np.zeros(n, n)
        for i in range(0, n):
            for j in range(0, n):
                if(i != j):
                    kld[i, j] = sc.entropy(distributions[i, :], distributions[j, :])

Tags: inimportforasnprange概率向量
1条回答
网友
1楼 · 发布于 2024-06-01 06:18:35

Scipy的^{}在其默认意义上邀请输入作为1D数组,给我们一个标量,这是在列出的问题中完成的。在内部,这个函数还允许^{},我们可以在这里滥用{eem>来获得矢量化的解决方案。在

^{}-

scipy.stats.entropy(pk, qk=None, base=None)

If only probabilities pk are given, the entropy is calculated as S = -sum(pk * log(pk), axis=0).

If qk is not None, then compute the Kullback-Leibler divergence S = sum(pk * log(pk / qk), axis=0).

在我们的例子中,我们对每一行对所有行进行熵计算,执行和约化,以便在这两个嵌套循环的每次迭代中都有一个标量。因此,输出数组的形状为(M,M),其中M是输入数组中的行数。在

现在,这里要注意的是stats.entropy()将沿着axis=0求和,因此我们将向它提供distributions的两个版本,这两个版本的rowth维都将被带到axis=0以便沿着它进行缩减,而另外两个轴交错-(M,1)&;(1,M)给我们一个使用broadcasting(M,M)形状的输出数组。在

因此,一个矢量化和更有效的解决我们的案件的方法是-

from scipy import stats
kld = stats.entropy(distributions.T[:,:,None], distributions.T[:,None,:])

运行时测试和验证-

^{pr2}$

相关问题 更多 >