在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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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