mars.tensor.linspace#
- mars.tensor.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, gpu=None, chunk_size=None)[来源]#
返回在指定区间内均匀分布的数字。
返回 num 个均匀间隔的样本,在区间 [start, stop] 上计算。
间隔的端点可以选择性地排除。
- Parameters
start (scalar) – 序列的起始值。
stop (标量) – 序列的结束值,除非 endpoint 被设置为 False。在这种情况下,序列由
num + 1个均匀间隔的样本组成,所以 stop 被排除在外。请注意,当 endpoint 为 False 时,步长会发生变化。num (int, 可选) – 生成的样本数量。默认值为50。必须是非负数。
端点 (布尔值, 可选) – 如果为 True,stop 是最后一个样本。否则,它不包括在内。 默认为 True。
retstep (bool, 可选) – 如果为 True,返回 (samples, step),其中 step 是样本之间的间距。
dtype (dtype, 可选) – 输出张量的类型。如果未给出dtype,则从其他输入参数推断数据类型。
gpu (bool, 可选) – 如果为True,则在GPU上分配张量,默认为False
chunk_size (int 或 tuple 的 int 或 tuple 的 ints, 可选) – 每个维度上所需的块大小
- Returns
样本 (张量) – 在闭区间
[start, stop]或半开区间[start, stop)中有 num 个等间隔样本(取决于 endpoint 是否为真)。步长 (浮点数,选项) – 仅在 retstep 为真时返回
样本之间的间隔大小。
另请参阅
arange类似于 linspace,但使用步长(而不是样本数量)。
logspace在对数空间中均匀分布的样本。
示例
>>> import mars.tensor as mt
>>> mt.linspace(2.0, 3.0, num=5).execute() array([ 2. , 2.25, 2.5 , 2.75, 3. ]) >>> mt.linspace(2.0, 3.0, num=5, endpoint=False).execute() array([ 2. , 2.2, 2.4, 2.6, 2.8]) >>> mt.linspace(2.0, 3.0, num=5, retstep=True).execute() (array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
图形说明:
>>> import matplotlib.pyplot as plt >>> N = 8 >>> y = mt.zeros(N) >>> x1 = mt.linspace(0, 10, N, endpoint=True) >>> x2 = mt.linspace(0, 10, N, endpoint=False) >>> plt.plot(x1.execute(), y.execute(), 'o') [<matplotlib.lines.Line2D object at 0x...>] >>> plt.plot(x2.execute(), y.execute() + 0.5, 'o') [<matplotlib.lines.Line2D object at 0x...>] >>> plt.ylim([-0.5, 1]) (-0.5, 1) >>> plt.show()