【问题标题】:Python buggy histogram?Python错误的直方图?
【发布时间】:2013-07-09 19:49:11
【问题描述】:

我对这个非常简单的代码有一个奇怪的行为

import numpy as np
[y, binEdges] = np.histogram(x, xout)

其中 x 和 xout 是 numpy 数组(xout 描述了等间距的 bin 的边缘)。

如果我这样做

np.sum(y)

该值不等于 x (x.shape) 中的元素数,该值比 x.shape 小很多,我不知道为什么。它是 np.histogram 的错误吗?如果需要,我可以上传 x 和 xout numpy 数组,但它们很长(x.shape 是 19133 float64,xout.shape 是 1360 float64)。如果我在上面的代码中做错了什么,请告诉我。

【问题讨论】:

  • 可能x的实际取值范围超出了xout的取值范围,可以试试np.histogram(x, bins-100)看看y的和是否正确?
  • 嗨,你是对的 xD np.max(xout)

标签: python numpy histogram


【解决方案1】:

试试这个:

y.sum() + (x < xout[0]).sum() + (x > xout[-1]).sum()

【讨论】:

  • Thx,现在问题已经解决了 xD 阅读上面的 cmets :) 我以错误的方式初始化了 xout 变量
【解决方案2】:

使用它来正确计算xoutn 是 bin 的数量。

xout = np.linspace(floor(x.min()), ceil(x.max(), n)

然后调用直方图函数:

[y, binEdges] = histogram(x, xout)

【讨论】:

  • Thx,我解决了问题 xD 在 cmets xD 上面读取的问题是 xout
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-07
相关资源
最近更新 更多