numpy.linspace()函数介绍和示例
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
释义:生成等差数列的数据
- start,起始点
- stop,结束点
- num,元素个数,默认 50
- endpoint,是否包含 stop 数值,默认为 True,包含 stop 值;若为 False,则不包含 stop 值
- retstep,返回值形式,默认为 False,返回等差数列组,若为True,则返回结果 (array([‘samples’, ‘step’]))
- dtype,返回结果的数据类型,默认无,若无,则参考输入数据类型
示例1:
import numpy as np
np.linspace(1, 50) # 默认 num=50,endpoint=True,retstep=False,dtype=None
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13.,
14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26.,
27., 28., 29., 30., 31., 32., 33., 34., 35., 36., 37., 38., 39.,
40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50.])
示例2:
np.linspace(5, 50, num=10) # num=10
array([ 5., 10., 15., 20., 25., 30., 35., 40., 45., 50.])
示例3:
np.linspace(5, 50, num=10, endpoint=False) # endpoint=False,包括起始点,不包括结束点
array([ 5. , 9.5, 14. , 18.5, 23. , 27.5, 32. , 36.5, 41. , 45.5])
示例4:
np.linspace(5, 50, num=10, retstep=True) # retstep=True,返回 (array([`samples`, `step`]))
(array([ 5., 10., 15., 20., 25., 30., 35., 40., 45., 50.]), 5.0)
示例5:
np.linspace(5, 50, num=10, dtype=int) # dtype=int,数据类型为 int
array([ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50])
参考:博客园