您好,我正在尝试使用以下方法将一些数据放入频率箱中:
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
最佳答案
我认为问题在于第二个参数,而不是 range
关键字参数。根据 docs,第二个参数必须是“与 x
形状相同” 。试试这个:
sc.stats.binned_statistic([0, 1, 0, 0, 1], np.arange(5),
statistic="count", bins=2, range=(0, 2.0))
编辑 正如@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)])
关于python - scipy 中 binned_statistic 的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17635074/