numpy.linspace()

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

在指定的间隔范围内返回均匀间隔的数字。

在[start, stop]范围内计算,返回num个(默认为50)均匀间隔的样本。

可以选择性地排除间隔的终点。

        Parameters

              start  [scalar]  返回序列的初始值。

              stop  [scalar]  除非endpoint被设置为False,否则stop为序列的终点值。当endpoint=False,返回序列包含num+1个均匀间隔样本的最后一个样本以外的所有样本(原文:the sequence consists of all but the last of num + 1 evenly spaced samples。理解为:[start, stop]均匀分割为包含num+1个样本的序列,最后一个样本即为endpoint,舍弃不取,即为返回的序列,其中样本总数仍然为num),故stop是被排除在外的。值得注意的是,当endpoint=False时,返回序列的步长会发生变化。

              num  [int, optional]  产生的样本总数。默认值为50。必须为非负值。

              endpoint  [bool, optional]  若为True,则stop为最后一个样本。否则,返回序列不包含stop。默认值为True。

              retstep  [bool, optional]  若为True,返回(samples, step),step为样本间的步长。

              dtype  [dtype, optional]  返回序列的数据类型。如果dtype未给定,那么从其他输入参数推断其类型。

        Returns

              samples  [ndarray]  包含num个等间隔样本的闭区间[start, stop]或者左闭右开区间[start, stop)(区间右端的开闭基于endpoint为True或者False)。

              step  [float, optional]  仅当retstep设置为True时返回,样本的步长。

Examples

>>> 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)

Graphical illustration:

import numpy as np
import matplotlib.pyplot as plt

N = 8
y = np.zeros(N)
x1 = np.linspace(0, 10, N, endpoint=True)
x2 = np.linspace(0, 10, N, endpoint=False)
plt.plot(x1, y, 'o')
plt.plot(x2, y + 0.5, 'o')
plt.ylim([-0.5, 1])
plt.show()

numpy.linspace()_第1张图片

 

你可能感兴趣的:(Python,numpy)