numpy是python中常用的库
import numpy as np
–>返回指定区域内的均匀分布的数字,默认包含端点。
–>endpoint默认stop是最后一个点,选择为False是不包含。
–>如果retstep是True,返回 (samples, step),step是间隔。
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
Return evenly spaced numbers over a specified interval. #返回指定区间内间隔均匀的数字
Returns num evenly spaced samples, calculated over the interval [start, stop]. #返回num个间隔均匀的样本,计算间隔[开始,停止]
The endpoint of the interval can optionally be excluded. #区间的端点可以选择排除
-->np.linspace(2.0, 3.0, num=5)
array([2. , 2.25, 2.5 , 2.75, 3. ])
-->np.linspace(2.0, 3.0, num=5, endpoint=False)
array([2. , 2.2, 2.4, 2.6, 2.8])
-->np.linspace(2.0, 3.0, num=5, retstep=True)
(array([2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
返回输入数组x中每个值对应bins的索引,right表示是否包含右边边界(左右边界只有一个包含,因此可以通过一个参数控制左右边界)
Return the indices of the bins to which each value in input array belongs.
注意!这个索引不是对应值,而是一个区间
bins是上升数组时:
right=False —> bins[i-1] <= x < bins[i]
right=True —> bins[i-1] < x <= bins[i]
bins是下降数组时:
right=False —> bins[i-1] > x >= bins[i]
right=True —> bins[i-1] >= x > bins[i]
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])