scipy中binned_statistic的范围

时间:2013-07-13 23:22:42

标签: 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) )

这会产生一个错误(如下),如果没有range参数,则不会发生错误。该函数的docs表明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

1 个答案:

答案 0 :(得分:5)

我认为问题在于第二个参数,而不是range关键字参数。第二个参数必须是&#34;与x&#34;相同的形状。根据{{​​3}}。试试这个:

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

编辑正如@DSM指出的那样,我的修正是针对另一个尚未出现的错误,所以这并没有按原样运行。 binned_statistic会调用docs,这需要&#34;一系列较低和较高的bin边缘,&#34;每个维度一个。看起来像SciPy上的一个错误你可以通过以下方式解决:

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