numpy.digitize(x, bins, right=False)
函数功能:返回一个和x形状相同的数据,返回值中的元素为对应x位置的元素落在bins中区间的索引号。
实际返回的结果可以查看如下表格
right | order of bins | returned index i satisfies |
---|---|---|
False | increasing | bins[i-1] <= x < bins[i] |
True | increasing | bins[i-1] < x <= bins[i] |
False | decreasing | bins[i-1] > x >= bins[i] |
True | decreasing | bins[i-1] >= x > bins[i] |
If values in x are beyond the bounds of bins, 0 or len(bins) is returned as appropriate.
x = np.array([0.2, 6.4, 3.0, 1.6])
bins = np.array([0.0, 1.0, 2.5, 4.0, 10.0])
inds = np.digitize(x, bins)
inds
array([1, 4, 3, 2])