二维np.digitize

Ale*_*lex 5 python numpy scipy binning pandas

我有二维数据,我有一堆二维箱生成scipy.stats.binned_statistic_2d.对于每个数据点,我想要它占据的bin的索引.这正是np.digitize为了什么,但据我所知,它只涉及一维数据. 这个堆栈交换似乎有一个答案,但这完全推广到n维.对于两个维度,是否有更直接的解决方案?

ali*_*i_m 5

您可以从第四个返回变量获取每个观察的bin索引scipy.stats.binned_statistic_2d:

Returns:  
  statistic : (nx, ny) ndarray
      The values of the selected statistic in each two-dimensional bin
  xedges : (nx + 1) ndarray
      The bin edges along the first dimension.
  yedges : (ny + 1) ndarray
      The bin edges along the second dimension.
  binnumber : 1-D ndarray of ints
      This assigns to each observation an integer that represents the bin
      in which this observation falls. Array has the same length as values.
Run Code Online (Sandbox Code Playgroud)