scipy中binned_statistic的范围

Gre*_*reg 2 python scipy

嗨,我正在尝试使用以下方法在频率分档中存储一些数据:

import scipy as sc
sc.stats.binned_statistic([0, 1, 0, 0 ,1], py.arange(1), 
                          statistic="count", bins=2, range=(0, 2.0) )
Run Code Online (Sandbox Code Playgroud)

这会产生一个错误(如下),如果没有range参数,则不会发生错误.这个函数的文档建议range=(float, float)应该做的伎俩.

谁能告诉我这里缺少什么?

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-136-1cf57b135132> in <module>()
  1 
 ----> 2 sc.stats.binned_statistic([0, 1, 0, 0 ,1], py.arange(1), statistic="count",    bins=2, range=(0,2))

/usr/lib/python2.7/dist-packages/scipy/stats/_binned_statistic.pyc in binned_statistic(x, values, statistic, bins, range)
      90 
      91     medians, edges, xy = binned_statistic_dd([x], values, statistic,
 ---> 92                                              bins, range)
      93 
      94     return medians, edges[0], xy

/usr/lib/python2.7/dist-packages/scipy/stats/_binned_statistic.pyc in binned_statistic_dd(sample, values, statistic, bins, range)
    281         smax = np.zeros(D)
    282         for i in np.arange(D):
--> 283             smin[i], smax[i] = range[i]
    284 
    285     # Make sure the bins have a finite width.

    TypeError: 'int' object is not iterable
Run Code Online (Sandbox Code Playgroud)

Jai*_*ime 5

我认为问题在于第二个参数,而不是range关键字参数.x根据文档,第二个参数必须是"与形状相同" .试试这个:

sc.stats.binned_statistic([0, 1, 0, 0, 1], np.arange(5), 
                          statistic="count", bins=2, range=(0, 2.0))
Run Code Online (Sandbox Code Playgroud)

编辑正如@DSM指出的那样,我的修正是针对另一个尚未出现的错误,因此这不起作用.binned_statistic调用binned_statistic_dd,期望"每个维度一个下部和上部边框边缘的序列".看起来像SciPy上的一个错误你可以通过以下方式解决:

sc.stats.binned_statistic([0, 1, 0, 0, 1], np.arange(5), 
                          statistic="count", bins=2, range=[(0, 2.0)])
Run Code Online (Sandbox Code Playgroud)

  • 这仍然是我的错误.无论文档似乎在说什么,它在我看来,代码期望`range`(如果不是`None`)是一对可迭代的对,而不是一对自身. (2认同)