为什么scipy.stats.熵(a,b)返回inf whilescipy.stats.熵(b,a)不是吗?

2024-06-16 09:55:24 发布

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

In [15]: a = np.array([0.5, 0.5, 0, 0, 0])

In [16]: b = np.array([1, 0, 0, 0, 0])

In [17]: entropy(a, b)
Out[17]: inf

In [18]: entropy(b, a)
Out[18]: 0.6931471805599453

从它们的documentation中,我期望两者都返回inf,因为给出的等式是S = sum(pk * log(pk / qk), axis=0)。第18行的非无限输出的原因是什么?在


Tags: inlogdocumentationnp原因outarrayinf
2条回答

entropy(b, a)函数计算第一对:

1 * np.log(1/0.5)

结果是0.6931471805599453。在

对于entropy(a, b),有一个被零除的情况,0.5/0,这将导致无穷解。在

其余的,entropy()假设0 * np.log(0/0)等于0。在

纵观Kullback-Leibler散度的定义,它似乎是由于它是如何定义的。在

来自维基百科:

Whenever P(x) is zero the contribution of the corresponding term is interpreted as zero because the limit goes to zero (click link for the equation).

相关问题 更多 >