Numpy从数组范围创建数组

生成0到n的数组,不包含n:

In [1]: import numpy as np
In [2]: n = 5
In [3]: x = np.arange(n)
In [4]: x
Out[4]: array([0, 1, 2, 3, 4])

设置起始值、终止值以及步长:

In [5]: a = np.arange(10, 20, 2)
In [6]: a
Out[6]: array([10, 12, 14, 16, 18])

 

用np.linspace函数创建一个一维等差数组:

格式如下:

np.linspace(start, stop, num, endpoint = True)

In [7]: b = np.linspace(1, 10, 10)
In [8]: b
Out[8]: array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])

将endpoint设为False时不包含终止值:

In [9]: c = np.linspace(10, 20, 5, endpoint = False)
In [10]: c
Out[10]: array([10., 12., 14., 16., 18.])

 

用np.logspace函数创建一个一维等比数组:

格式如下:

np.logspace(start, stop, num = 50, endpoint = True, base = 10.0)

In [11]: d = np.logspace(1.0, 2.0, num = 10)
In [12]: d
Out[12]:
array([ 10.        ,  12.91549665,  16.68100537,  21.5443469 ,
        27.82559402,  35.93813664,  46.41588834,  59.94842503,
        77.42636827, 100.        ])
In [13]: e = np.logspace(0, 9, 10, base = 2)
In [14]: e
Out[14]: array([  1.,   2.,   4.,   8.,  16.,  32.,  64., 128., 256., 512.])

参考:https://www.runoob.com/

posted on 2019-05-12 22:42  qjyyz  阅读(165)  评论(0编辑  收藏  举报