python - scipy 中 binned_statistic 的范围

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

最佳答案

我认为问题在于第二个参数,而不是 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/

相关文章:

python - 为什么 Python 不索引我的列表并用新的索引版本覆盖它?仍在打印原件

python - 列名称的 sqlalchemy 别名也保留原始名称

python - 在 Linux 上使用可滚动的 x(时间/水平)轴绘制数据

python - 在 Numpy 中有效地构建一个 'rolled' 行的矩阵

python - Mac 10.6 通用二进制 scipy : cephes/specfun "_aswfa_" symbol not found

python - 使用scipy对样本进行离散积分

python - 如何计算从 pb 文件加载的 tensorflow 模型的触发器

python - 如何在 .NET-Core 应用程序中使用 Python?

scipy - 如何使用 SciPy 最小化重用 Jacobian 和 Inverse Hessian

python - 如何使用 Wand 更改图片的对比度?