问题
I have two-dimensional data and I have a bunch of two-dimensional bins generated with scipy.stats.binned_statistic_2d
. For each data point, I want the index of the bin it occupies. This is exactly what np.digitize
is for, but as far as I can tell, it only deals with one-dimensional data. This stackexchange seems to have an answer, but that is totally generalized to n-dimensions. Is there a more straightforward solution for two dimensions?
回答1:
You can already get the bin index of each observation from the fourth return variable of 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.
来源:https://stackoverflow.com/questions/31635265/two-dimensional-np-digitize