python等间隔取样
import numpy as np
linspace numpy# 1表⽰开始位置
# 25表⽰结束位置
# 15表⽰抽取数据个数
# int表⽰转换为整型数据,默认是float
data=np.linspace(1, 25, 15, dtype=int)
print(data)
import numpy as np
ddd= np.linspace(0,1,num=5,endpoint=True,retstep=False, dtype=None)
print(ddd)
以下内容转⾃:
linspace可以⽤来实现相同间隔的采样;
numpy.linspace(start,stop,num=50,endpoint=True,retstep=False, dtype=None)
返回num均匀分布的样本,在[start, stop]。
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 is 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(推断这个输⼊⽤例从其他的输⼊中). New in version 1.9.0.
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.
当endpoint被设置为False的时候
import numpy as np
np.linspace(1, 10, 10)
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
np.linspace(1, 10, 10, endpoint=False)
array([ 1. , 1.9, 2.8, 3.7, 4.6, 5.5, 6.4, 7.3, 8.2, 9.1])
In [4]: np.linspace(1, 10, 10, endpoint=False, retstep=True)
Out[4]: (array([ 1. , 1.9, 2.8, 3.7, 4.6, 5.5, 6.4, 7.3, 8.2, 9.1]), 0.9)
官⽹的例⼦Examples
Graphical illustration: