numpy.linspace函数具体使用详解

 更新时间:2019年05月27日 14:26:18   作者:蚂蚁flow  
这篇文章主要介绍了numpy.linspace具体使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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

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

返回num均匀分布的样本,在[start, stop]。

这个区间的端点可以任意的被排除在外。

Parameters(参数):

 

start : scalar(标量)

The starting value of the sequence(序列的起始点).

stop : scalar

序列的结束点,除非endpoint被设置为False,在这种情况下, the sequence consists of all but the last of num + 1 evenly spaced samples(该序列包括所有除了最后的num+1上均匀分布的样本(感觉这样翻译有点坑)), 以致于stop被排除.当endpoint is False的时候注意步长的大小(下面有例子).

num : int, optional(可选)

生成的样本数,默认是50。必须是非负。

endpoint : bool, optional

如果是真,则一定包括stop,如果为False,一定不会有stop

retstep : bool, optional

If True, return (samples, step), where step is the spacing between samples.(看例子)

dtype : dtype, optional

The type of the output array. If dtype is not given, infer the data type from the other input arguments(推断这个输入用例从其他的输入中).

New in version 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).

step : float(只有当retstep设置为真的时候才会存在)

Only returned if retstep is True

Size of spacing between samples.

See also

arange

Similar to linspace, but uses a step size (instead of the number of samples)

.arange使用的是步长,而不是样本的数量

logspace

Samples uniformly distributed in log space. 

当endpoint被设置为False的时候

>>> import numpy as np
>>> np.linspace(1, 10, 10)
array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
>>> np.linspace(1, 10, 10, endpoint = False)
array([ 1. , 1.9, 2.8, 3.7, 4.6, 5.5, 6.4, 7.3, 8.2, 9.1])

In [4]: np.linspace(1, 10, 10, endpoint = False, retstep= True)
Out[4]: (array([ 1. , 1.9, 2.8, 3.7, 4.6, 5.5, 6.4, 7.3, 8.2, 9.1]), 0.9)

官网的例子 

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • python BeautifulSoup使用方法详解

    python BeautifulSoup使用方法详解

    Beautiful Soup 是用Python写的一个HTML/XML的解析器,它可以很好的处理不规范标记并生成剖析树(parse tree)。 它提供简单又常用的导航(navigating),搜索以及修改剖析树的操作。它可以大大节省你的编程时间,下面我们就看看他是如何使用
    2013-11-11
  • 如何利用python之wxpy模块玩转微信

    如何利用python之wxpy模块玩转微信

    这篇文章主要介绍了利用python之wxpy模块玩转微信,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • Python利用Beautiful Soup模块搜索内容详解

    Python利用Beautiful Soup模块搜索内容详解

    这篇文章主要给大家介绍了python中 Beautiful Soup 模块的搜索方法函数。 方法不同类型的过滤参数能够进行不同的过滤,得到想要的结果。文中介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-03-03
  • python简单利用字典破解zip文件口令

    python简单利用字典破解zip文件口令

    这篇文章主要给大家介绍了关于python简单利用字典破解zip文件口令的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • pandas获取对应的行或者列方式

    pandas获取对应的行或者列方式

    这篇文章主要介绍了pandas获取对应的行或者列方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • python模拟登陆Tom邮箱示例分享

    python模拟登陆Tom邮箱示例分享

    这篇文章主要介绍了python登陆Tom邮箱的示例,大家参考使用吧
    2014-01-01
  • Python requests库用法实例详解

    Python requests库用法实例详解

    这篇文章主要介绍了Python requests库用法,结合实例形式分析了Request库的功能、安装、请求创建、响应等相关操作技巧,需要的朋友可以参考下
    2018-08-08
  • Keras 在fit_generator训练方式中加入图像random_crop操作

    Keras 在fit_generator训练方式中加入图像random_crop操作

    这篇文章主要介绍了Keras 在fit_generator训练方式中加入图像random_crop操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • 解析Python中的__getitem__专有方法

    解析Python中的__getitem__专有方法

    __getitem__是Python双下划线包围的special method之一,这里我们就来解析Python中的__getitem__专有方法的使用,需要的朋友可以参考下:
    2016-06-06
  • numpy 返回函数的上三角矩阵实例

    numpy 返回函数的上三角矩阵实例

    今天小编就为大家分享一篇numpy 返回函数的上三角矩阵实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11

最新评论