tags: [笔记, Numpy, np.linspace]
categories: [笔记, Numpy, np.linspace]
语法
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
作用
在指定的间隔内返回均匀间隔的数字。返回num个以均匀间隔分布的样本,在[start, stop]。这个区间的末端点可以通过endpoint=False来排除在外。
Parameters(参数):
start : scalar(标量),The starting value of the sequence(序列的起始点).
stop : scalar,序列的结束点,除非endpoint被设置为False,在这种情况下, the sequence consists of all but the last of num + 1 evenly spaced samples(该序列由除最后一个样本(num + 1)以外的所有均匀间隔的样本组成), 不包括stop.当endpoint=False的时候注意步长的大小(下面有例子).
num : int, optional(可选),生成的样本数,默认是50。必须是非负。
endpoint : bool, optional,如果是真,则一定包括stop,如果为False,一定不会有stop
retstep : bool, optional,If True, return (samples, step), where step is the spacing between samples.(看例子)
dtype : dtype, optional,The type of the output array. If dtype is not given, infer the data type from the other input arguments(推断这个输入用例从其他的输入中).
Returns:
samples : ndarray,There are num equally spaced samples in the closed interval [start, stop] or the half-open interval [start, stop) (depending on whether endpoint is True or False).
step : float(只有当retstep设置为真的时候才会存在),Only returned if retstep is True, Size of spacing between samples.
# space: (3.0-2.0)/(5-1)=0.25
>>> np.linspace(2.0, 3.0, num=5)
array([ 2. , 2.25, 2.5 , 2.75, 3. ])
# step: (3.0-2.0)/5=0.2,因为要生成num+1个样本,所以step为6-1=5
>>> 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)