numpy - 在 Scipy 中计算 KL 散度时出错

标签 numpy matplotlib machine-learning scipy signal-processing

我正在尝试使用 entropy 计算 KL 散度scipy的功能。

我的p是:

array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.],
       [ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

q是:

array([[ 0.05242718,  0.04436347,  0.04130855,  0.04878344,  0.04310538,
         0.02856853,  0.03303122,  0.02517992,  0.08525434,  0.03450324,
         0.14580068,  0.1286993 ,  0.28897473],
       [ 0.65421444,  0.11592199,  0.0642645 ,  0.02989768,  0.01385762,
         0.01756484,  0.01024294,  0.00891479,  0.01140301,  0.00718939,
         0.00938009,  0.01070139,  0.04644726],
       [ 0.65984136,  0.13251236,  0.06345234,  0.02891162,  0.02429709,
         0.02025307,  0.01073064,  0.01170066,  0.00678652,  0.00703361,
         0.00560414,  0.00651137,  0.02236522],
       [ 0.32315928,  0.23900077,  0.05460232,  0.03953635,  0.02901102,
         0.01294443,  0.02372061,  0.02092882,  0.01188251,  0.01377188,
         0.02976672,  0.05854314,  0.14313218],
       [ 0.7717858 ,  0.09692616,  0.03415596,  0.01713088,  0.01108141,
         0.0128005 ,  0.00847301,  0.01049734,  0.0052889 ,  0.00514799,
         0.00442508,  0.00485477,  0.01743218]], dtype=float32)

当我这样做时:

entropy(p[0],q[0])

我收到以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-201-563ea7d4decf> in <module>()
      4 print('p0:',p[0])
      5 print('q0:',q[0])
----> 6 entropy(p[0],q[0])

/Users/freelancer/anaconda/envs/py35/lib/python3.5/site-packages/matplotlib/mlab.py in entropy(y, bins)
   1570     y = np.zeros((len(x)+2,), x.dtype)
   1571     y[1:-1] = x
-> 1572     dif = np.diff(y)
   1573     up = (dif == 1).nonzero()[0]
   1574     dn = (dif == -1).nonzero()[0]

/Users/freelancer/anaconda/envs/py35/lib/python3.5/site-packages/numpy/lib/function_base.py in histogram(a, bins, range, normed, weights, density)
    781         if (np.diff(bins) < 0).any():
    782             raise ValueError(
--> 783                 'bins must increase monotonically.')
    784 
    785         # Initialize empty histogram

ValueError: bins must increase monotonically.

为什么会这样?

最佳答案

这适用于示例数组:

import scipy as sp
sp.stats.entropy(p[0], q[0])

查看错误消息中的堆栈跟踪,很明显您没有调用 scipy's entropy功能但是 matplotlib's entropy ,其工作原理不同。 这是相关部分:

/Users/freelancer/anaconda/envs/py35/lib/python3.5/site-packages/matplotlib/mlab.pyin entropy(y, bins)

关于numpy - 在 Scipy 中计算 KL 散度时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43797480/

相关文章:

windows-7 - cython:mingw 的内存 View 构建错误

python - 如何突出显示数据帧的两个不同列中的唯一数据值?

Python numpy按条件过滤二维数组

python - 使用 matplotlib 绘制 semilogx,然后将其转换为 Bokeh

python - matplotlib,savefig : DPI setting is ignored

machine-learning - 我如何在 Giza++ 中进行训练和测试步骤?

python - 获取 Tensorflow 中线性回归的系数

Python:根据具有第 3 维索引的相应数组将 2D 数组拉伸(stretch)为 3D

python - 在同一角图中绘制多个数据集

r - 自定义 SHAPforxgboost 图中的标签