语法格式

numpy.linspace(startstopnum=50endpoint=Trueretstep=Falsedtype=Noneaxis=0)

常用参数解释:

  • start: 序列的起始值
  • stop: 序列的终止值
  • num: 要生成的样本数量。默认值是50。必须是非负的
  • endpoint: 如果是True,最后一个值取到stop值。否则,不包括它。默认为True
  • retstep: 如果为True,则返回(samples, step),其中step是样本之间的间距。
  • dtype: 输出数组的类型。如果没有给出dtype,则从start和stop推断数据类型。推断的dtype永远不会是整数;即使参数将产生一个整数数组,也会选择Float
  • axis: 设置轴存储数据,默认为0

代码示例

>>> import numpy as np
>>> np.linspace(1.0, 2.0, num=5)
array([1.  , 1.25, 1.5 , 1.75, 2.  ])
>>> np.linspace(1.0, 2.0, num=5,endpoint=False)
array([1. , 1.2, 1.4, 1.6, 1.8])
>>> np.linspace(1.0, 2.0, num=5,endpoint=False,retstep=True)
(array([1. , 1.2, 1.4, 1.6, 1.8]), 0.2)