使用scipy的两个不同长度数据集之间的KL差异

2024-05-16 08:42:17 发布

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

我试图用scipy的熵函数计算KL散度。 https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.entropy.html

from scipy.stats import entropy
data_a = np.array([176,489,7])
data_b = np.array([84,86,47,18,99,26,50,53,76,27])
entropy(data_a, data_b)

我犯了以下错误。我该怎么办

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-d8fef5634e56> in <module>
----> 1 en = entropy(data_real, data_crowd)

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/scipy/stats/_distn_infrastructure.py in entropy(pk, qk, base)
   2548         qk = asarray(qk)
   2549         if len(qk) != len(pk):
-> 2550             raise ValueError("qk and pk must have same length.")
   2551         qk = 1.0*qk / np.sum(qk, axis=0)
   2552         vec = rel_entr(pk, qk)

ValueError: qk and pk must have same length.

我使用MacOS 10.14.5、python 3.7.2和scipy 1.2.1


Tags: andindatalenhavestatsnpscipy