numpy.linspace函数具体使用详解


Posted in Python onMay 27, 2019

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

numpy.linspace函数具体使用详解

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

Python 相关文章推荐
零基础写python爬虫之神器正则表达式
Nov 06 Python
Python实现屏幕截图的代码及函数详解
Oct 01 Python
Python实现的简单模板引擎功能示例
Sep 02 Python
Python2和Python3中print的用法示例总结
Oct 25 Python
python实现扫描日志关键字的示例
Apr 28 Python
Python 类的特殊成员解析
Jun 20 Python
用Python实现将一张图片分成9宫格的示例
Jul 05 Python
最新2019Pycharm安装教程 亲测
Feb 28 Python
在ipython notebook中使用argparse方式
Apr 20 Python
使用tensorflow进行音乐类型的分类
Aug 14 Python
详解pandas赋值失败问题解决
Nov 29 Python
浅谈Selenium 控制浏览器的常用方法
Dec 04 Python
Django利用cookie保存用户登录信息的简单实现方法
May 27 #Python
Django框架设置cookies与获取cookies操作详解
May 27 #Python
numpy下的flatten()函数用法详解
May 27 #Python
详解numpy的argmax的具体使用
May 27 #Python
numpy.random模块用法总结
May 27 #Python
Django框架自定义session处理操作示例
May 27 #Python
numpy concatenate数组拼接方法示例介绍
May 27 #Python
You might like
php 操作调试的方法
2012/07/12 PHP
PHP Curl多线程原理实例详解
2013/11/06 PHP
php中simplexml_load_string使用实例分享
2014/02/13 PHP
php禁止浏览器使用缓存页面的方法
2014/11/07 PHP
php使用ob_flush不能每隔一秒输出原理分析
2015/06/02 PHP
PHP错误和异常处理功能模块示例
2016/11/12 PHP
php 从一个数组中随机的取出若干个不同的数实例
2016/12/31 PHP
解决jquery .ajax 在IE下卡死问题的解决方法
2009/10/26 Javascript
Javascript异步编程的4种方法让你写出更出色的程序
2013/01/17 Javascript
javascript常见用法总结
2014/05/22 Javascript
jQuery实现Meizu魅族官方网站的导航菜单效果
2015/09/14 Javascript
纯js实现手风琴效果
2020/04/17 Javascript
字符串反转_JavaScript
2016/04/28 Javascript
微信QQ的二维码登录原理js代码解析
2016/06/23 Javascript
JCrop+ajaxUpload 图像切割上传的实例代码
2016/07/20 Javascript
jQuery实现打开页面渐现效果示例
2016/07/27 Javascript
20分钟打造属于你的Bootstrap站点
2016/07/27 Javascript
BootStrap tooltip提示框使用小结
2016/10/26 Javascript
详解vue的数据binding绑定原理
2017/04/12 Javascript
结合Vue控制字符和字节的显示个数的示例
2018/05/17 Javascript
layui 上传插件 带预览 非自动上传功能的实例(非常实用)
2019/09/23 Javascript
Vue3 源码导读(推荐)
2019/10/14 Javascript
Python实现各种排序算法的代码示例总结
2015/12/11 Python
1 行 Python 代码快速实现 FTP 服务器
2018/01/25 Python
python十进制和二进制的转换方法(含浮点数)
2018/07/07 Python
Python爬虫框架scrapy实现的文件下载功能示例
2018/08/04 Python
在Python中使用defaultdict初始化字典以及应用方法
2018/10/31 Python
Python 调用PIL库失败的解决方法
2019/01/08 Python
Python requests获取网页常用方法解析
2020/02/20 Python
pygame实现弹球游戏
2020/04/14 Python
python 获取剪切板内容的两种方法
2020/11/28 Python
《四季》教学反思
2014/04/08 职场文书
2015年医院创卫工作总结
2015/04/22 职场文书
2019求职信大礼包
2019/05/15 职场文书
Python FuzzyWuzzy实现模糊匹配
2022/04/28 Python
Python pyecharts案例超市4年数据可视化分析
2022/08/14 Python