python numpy.linspace() 使用介绍

numpy.linspace()

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

示例

1)

# -*- coding: utf-8 -*-
"""
@File    : test.py
@Time    : 2020/4/1 19:24
@Author  : Dontla
@Email   : sxana@qq.com
@Software: PyCharm
"""
import numpy as np

# 在闭区间[2, 3]生成5个间隔相同的数字
print(np.linspace(2.0, 3.0, num=5))
# [2.   2.25 2.5  2.75 3.  ]

# 在半开区间[2, 3)生成5个间隔相同的数字
print(np.linspace(2.0, 3.0, num=5, endpoint=False))
# [2.  2.2 2.4 2.6 2.8]

# 在闭区间[2, 3]生成5个间隔相同的数字(除了返回生成的样本数字,还返回样本数字之间的间距)
print(np.linspace(2.0, 3.0, num=5, retstep=True))
# (array([2.  , 2.25, 2.5 , 2.75, 3.  ]), 0.25)

2)

官方doc

def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None):
    """
    Return evenly spaced numbers over a specified interval.

    Returns `num` evenly spaced samples, calculated over the
    interval [`start`, `stop`].

    The endpoint of the interval can optionally be excluded.

	返回指定区间内的均匀间隔的数字。

	返回在区间[`start`,`stop`]中计算出的num个均匀间隔的样本。

	区间的端点可以选择排除。

    Parameters
    ----------
    start : scalar
        The starting value of the sequence.
        序列的起始值。
        
    stop : scalar
        The end value of the sequence, unless `endpoint` is set to False.
        In that case, the sequence consists of all but the last of ``num + 1``
        evenly spaced samples, so that `stop` is excluded.  Note that the step
        size changes when `endpoint` is False.
        序列的结束值,除非将“ endpoint”设置为False。
         在这种情况下,该序列由除num + 1之外的所有其他均匀间隔的样本组成,
         因此排除了stop。 注意,当`endpoint`为False时步长会改变。
         
    num : int, optional
        Number of samples to generate. Default is 50. Must be non-negative.
        要生成的样本数。 默认值为50。必须为非负数。
        
    endpoint : bool, optional
        If True, `stop` is the last sample. Otherwise, it is not included.
        Default is True.
        如果为True,则“ stop”是最后一个样本。 否则,不包括在内。
         默认值为True。
         
    retstep : bool, optional
        If True, return (`samples`, `step`), where `step` is the spacing
        between samples.
        如果为True,则返回(“ samples”,“ step”),其中“ step”是样本之间的间距。
        
    dtype : dtype, optional
        The type of the output array.  If `dtype` is not given, infer the data
        type from the other input arguments.
        输出数组的类型。 如果未给出dtype,则从其他输入参数推断数据类型。

        .. versionadded:: 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).
        在闭区间“ [start,stop]”或半开区间“ [start,stop)”中有“ num”个等距样本(取决于“ endpoint”是True还是False)。
        
    step : float, optional
        Only returned if `retstep` is True
        仅在`retstep`为True时返回

        Size of spacing between samples.
        样本之间的间距大小。



    See Also
    --------
    arange : Similar to `linspace`, but uses a step size (instead of the
             number of samples).
             类似于linspace,但是使用步长(而不是样本数)。
             
    logspace : Samples uniformly distributed in log space.
    			样本在日志空间中均匀分布。

    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 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')
    [<matplotlib.lines.Line2D object at 0x...>]
    >>> plt.plot(x2, y + 0.5, 'o')
    [<matplotlib.lines.Line2D object at 0x...>]
    >>> plt.ylim([-0.5, 1])
    (-0.5, 1)
    >>> plt.show()

    """

参考文章:numpy.linspace使用详解

8小时Python零基础轻松入门

  • 13
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dontla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值