【问题标题】:How to find what points lie in each bin of a histogram?如何找到直方图的每个 bin 中有哪些点?
【发布时间】:2020-01-16 23:42:51
【问题描述】:

我有一个二维直方图,其 bin 大小为 10。我想知道是否有一个 numpy 函数(或任何更快的方法)来获取二维网格中每个 bin 中的点。有没有办法访问 bin 元素?

【问题讨论】:

  • 你是如何制作直方图的?
  • B = np.random.random((100,2)); H, edges = np.histogramdd(B, bins = [10]*2, density=True)

标签: python numpy histogram


【解决方案1】:

我希望这能解决您的问题。但是,我相信其他人可以改进我的代码,因为我是 python 新手。

使用 matplotlib 创建直方图

import matplotlib.pyplot as plt
rng = np.random.RandomState(10)  # deterministic random data
a = np.hstack((rng.normal(size=100), rng.normal(loc=5, scale=2, size=1000)))
n ,bins ,patches  = plt.hist(a, bins=10)  # arguments are passed to np.histogram
plt.title("Histogram with '10' bins")

plt.show()

重塑数组和..

newbin =  np.repeat(np.reshape(bins,(-1, len(bins))), a.shape, axis=0)
newa = np.repeat(np.reshape(a,(len(a),-1)),len(bins),axis=1)
#index_bin = (np.where(newbin[:,0] >np.reshape(a,(1,-1))[:,0] )  )[0][0]

index_bin = (newbin>newa).argmax(axis=1).T

测试

print(a[0] ,    bins)
print(index_bin[0])

输出

1.331586504129518 [-2.13171211 -0.88255884  0.36659444  1.61574771  2.86490098  4.11405425
  5.36320753  6.6123608   7.86151407  9.11066734 10.35982062]
3

【讨论】:

    猜你喜欢
    • 2017-01-18
    • 2017-04-11
    • 2015-10-11
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 2020-09-21
    相关资源
    最近更新 更多