在Scipy中计算KL散度时出现错误

我试图使用scipy的entropy函数来计算KL散度。

我的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 histogramValueError: bins must increase monotonically.

这是为什么呢?


回答:

这个例子中的数组是可以正常工作的:

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

从错误信息中的堆栈跟踪来看,很明显你调用的不是scipy的entropy函数,而是matplotlib的entropy函数,它们的工作方式不同。以下是相关部分:

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

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注