NumPy教程:如何从数值范围创建数组及示例代码

2018-09-2710:19:17后端程序开发Comments2,951 views字数 1626阅读模式

NumPy - 来自数值范围的数组

学到如何从数值范围创建数组。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

numpy.arange

这个函数返回ndarray对象,包含给定范围内的等间隔值。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

numpy.arange(start, stop, step, dtype)

构造器接受下列参数:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

序号参数及描述
1.start 范围的起始值,默认为0
2.stop 范围的终止值(不包含)
3.step 两个值的间隔,默认为1
4.dtype 返回ndarray的数据类型,如果没有提供,则会使用输入数据的类型。

下面的例子展示了如何使用该函数:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

示例 1

import numpy as np
x = np.arange(5)  
print x

输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

[0  1  2  3  4]

示例 2

import numpy as np
# 设置了 dtype
x = np.arange(5, dtype =  float)  
print x

输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

[0.  1.  2.  3.  4.]

示例 3

# 设置了起始值和终止值参数  
import numpy as np
x = np.arange(10,20,2)  
print x

输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

[10  12  14  16  18]

numpy.linspace

此函数类似于arange()函数。 在此函数中,指定了范围之间的均匀间隔数量,而不是步长。 此函数的用法如下。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

numpy.linspace(start, stop, num, endpoint, retstep, dtype)

构造器接受下列参数:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

序号参数及描述
1.start 序列的起始值
2.stop 序列的终止值,如果endpointtrue,该值包含于序列中
3.num 要生成的等间隔样例数量,默认为50
4.endpoint 序列中是否包含stop值,默认为ture
5.retstep 如果为true,返回样例,以及连续数字之间的步长
6.dtype 输出ndarray的数据类型

下面的例子展示了linspace函数的用法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

示例 1

import numpy as np
x = np.linspace(10,20,5)  
print x

输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

[10.   12.5   15.   17.5  20.]

示例 2

# 将 endpoint 设为 false
import numpy as np
x = np.linspace(10,20,  5, endpoint =  False)  
print x

输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

[10.   12.   14.   16.   18.]

示例 3

# 输出 retstep 值  
import numpy as np

x = np.linspace(1,2,5, retstep =  True)  
print x
# 这里的 retstep 为 0.25

输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

(array([ 1.  ,  1.25,  1.5 ,  1.75,  2.  ]), 0.25)

numpy.logspace

此函数返回一个ndarray对象,其中包含在对数刻度上均匀分布的数字。 刻度的开始和结束端点是某个底数的幂,通常为 10。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

numpy.logscale(start, stop, num, endpoint, base, dtype)

logspace函数的输出由以下参数决定:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

序号参数及描述
1.start 起始值是base ** start
2.stop 终止值是base ** stop
3.num 范围内的数值数量,默认为50
4.endpoint 如果为true,终止值包含在输出数组当中
5.base 对数空间的底数,默认为10
6.dtype 输出数组的数据类型,如果没有提供,则取决于其它参数

下面的例子展示了logspace函数的用法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

示例 1

import numpy as np
# 默认底数是 10
a = np.logspace(1.0,  2.0, num =  10)  
print a

输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

[ 10.           12.91549665     16.68100537      21.5443469  27.82559402      
  35.93813664   46.41588834     59.94842503      77.42636827    100.    ]

示例 2

# 将对数空间的底数设置为 2  
import numpy as np
a = np.logspace(1,10,num =  10,  base  =  2)  
print a

输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html

[ 2.     4.     8.    16.    32.    64.   128.   256.    512.   1024.]
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5878.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/5878.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定