【问题标题】:numpy digitize for 2 dimensional histogramnumpy 数字化二维直方图
【发布时间】:2021-08-05 15:45:58
【问题描述】:

我正在寻找二维直方图的np.digitize 函数。

从这些代码行:

    H, xedges, yedges = np.histogram2d(coord[:, 0], coord[:, 1])
    H = H.T
    print(H)

我得到以下直方图:

[[ 7. 20. 16. 14. 10.  8. 16.  7. 10.  7.]
 [11. 11. 10. 10.  5. 10.  9. 12.  7.  7.]
 [13. 11. 13.  9. 13. 10. 14.  6.  9.  9.]
 [ 5.  5.  4.  5.  7. 13. 14. 11.  6. 10.]
 [14.  4. 11.  5.  7. 14.  6. 11. 11.  5.]
 [12.  9.  5.  7.  9. 14. 15. 15. 13. 12.]
 [ 5. 13. 15.  9. 10.  7. 10. 12.  7.  5.]
 [ 4. 10. 15.  7.  6. 10. 13.  5. 12. 12.]
 [12.  6. 11.  8.  5.  5. 13. 14. 13.  9.]
 [10. 11.  9.  8. 18. 13. 16.  8.  8. 13.]]

我想找出直方图的每个元素所代表的索引,例如(应计算第一行,第一列 -> 7 -> 7 个索引)。 我花了很多时间试图关注this post,但我被困在一个地方(如果没有更好的方法,我可以解释在哪里)。另一个可能的重复是here,但这也不能解决问题。

那么有谁知道,如何解决这个问题?

谢谢!

【问题讨论】:

    标签: python arrays numpy histogram histogram2d


    【解决方案1】:

    你想要scipy.stats.binned_statistic_2d:

    H, xedges, yedges, binnumber = scipy.stats.binned_statistic_2d(
        coord[:, 0], coord[:, 1], None, 'count', expand_binnumbers=True)
    

    前三个返回值与您的原始代码相同。第四个(binnumber)`是:

    一个形状 (2,N) ndarray,其中每一行给出相应维度中的 bin 编号。

    【讨论】:

      猜你喜欢
      • 2020-12-04
      • 2021-01-17
      • 2015-01-25
      • 1970-01-01
      • 2013-09-21
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多