原文:https://docs.scipy.org/doc/numpy/reference/generated/numpy.digitize.html
校对:(虚位以待)
numpy.
digitize
(x, bins, right=False)返回输入数组中每个值所属的bin的索引。
Each index i
returned is such that bins[i-1] <= x < bins[i]
if bins is monotonically increasing, or bins[i-1] > x >= bins[i]
if bins is monotonically decreasing. 如果x中的值超出了bin的界限,则适当返回0或len(bins)
。如果right为True,则右边仓关闭,使得索引i
使得bin [i-1] > x bins [i]
或bins [i-1]> = x> bins [i] `if bins分别单调递增或递减。
参数: | x:array_like
bin:array_like
右:bool,可选
|
---|---|
返回: | out:intar的ndarray
|
上升: | ValueError
TypeError
|
笔记
如果x中的值落在bin范围之外,则尝试使用digitize
返回的索引将bin编入索引将导致IndexError 。
版本1.10.0中的新功能。
np.digitize按照np.searchsorted执行。这意味着二进制搜索用于对值进行二进制化,对于大于先前线性搜索的二进制数,这些二进制搜索的尺度更好。它也删除了对输入数组为一维的要求。
例子
>>> 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])
>>> for n in range(x.size):
... print(bins[inds[n]-1], "<=", x[n], "<", bins[inds[n]])
...
0.0 <= 0.2 < 1.0
4.0 <= 6.4 < 10.0
2.5 <= 3.0 < 4.0
1.0 <= 1.6 < 2.5
>>> x = np.array([1.2, 10.0, 12.4, 15.5, 20.])
>>> bins = np.array([0, 5, 10, 15, 20])
>>> np.digitize(x,bins,right=True)
array([1, 2, 3, 4, 4])
>>> np.digitize(x,bins,right=False)
array([1, 3, 3, 4, 5])