numpy.arange([start, ]stop, [step, ]dtype=None, *, like=None)
函数返回一个有终点和起点的固定步长的排列,如[1,2,3,4,5],起点是1,终点是6,步长为1。 参数个数情况:
np.arange()函数分为一个参数,两个参数,三个参数三种情况
1)一个参数时,参数值为终点,起点取默认值0,步长取默认值1。
2)两个参数时,第一个参数为起点,第二个参数为终点,步长取默认值1。
3)三个参数时,第一个参数为起点,第二个参数为终点,第三个参数为步长。其中步长支持小数
#一个参数 默认起点0,步长为1 输出:[0 1 2]
a = np.arange(3)
#两个参数 默认步长为1 输出[3 4 5 6 7 8]
a = np.arange(3,9)
#三个参数 起点为0,终点为3,步长为0.1 输出[ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2. 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9]
a = np.arange(0, 3, 0.1)
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
主要用来创建等差数列
其中后面斜体的参数为可选的,均有默认值。以下为几种可能的使用情况:
当start和stop均为单个数值时:
1)将区间[start, stop]分成等间隔的num个数(包含start和stop两个数),并返回它们组成的数组;
2)若endpoint=False,就将区间[start, stop]分成等间隔的num+1个数,但返回的数组中不包括‘stop’项;
3)若retstep=True,返回值格式为 (数组部分, 间隔长度); dtype决定输出数组的数据类型,若是未指定,则根据输入参数决定。
a = np.linspace(start=2, stop=3, num=5, endpoint=True, retstep=False, dtype=None, axis=0)
print(a) # [2. 2.25 2.5 2.75 3. ] ,即将区间[start, stop]分成等间隔的num个数,并返回它们组成的数组
b = np.linspace(start=2, stop=3, num=5, endpoint=False, retstep=False, dtype=None, axis=0)
print(b) # [2. 2.2 2.4 2.6 2.8] ,若endpoint=False,就将区间[start, stop]分成等间隔的num+1个数,但返回时不包括‘stop’
c = np.linspace(start=2, stop=3, num=5, endpoint=True, retstep=True, dtype=None, axis=0)
print(c) # (array([2. , 2.25, 2.5 , 2.75, 3. ]), 0.25) ,若retstep=True,返回值格式为 (数组部分, 间隔长度)
d = np.linspace(start=2, stop=3, num=5, endpoint=True, retstep=False, dtype=np.float32, axis=0)
print(d.dtype) # float32 , dtype决定输出数组的数据类型,若是未指定,则根据输入参数决定
当start或stop为一维类数组时:
# 当start和stop均为类数组的情况
s1 = [1, 2]
s2 = [4, 5]
e = np.linspace(start=s1, stop=s2, num=3, endpoint=True, retstep=False, dtype=None, axis=0)
print(e)
# [[1. 2. ]
# [2.5 3.5]
# [4. 5. ]]
# 当start和stop有一个为类数组
s1 = [1, 2]
s2 = 5
e = np.linspace(start=s1, stop=s2, num=3, endpoint=True, retstep=False, dtype=None, axis=0)
print(e)
# [[1. 2. ]
# [3. 3.5]
# [5. 5. ]]
s1 = 1
s2 = [4, 5]
e = np.linspace(start=s1, stop=s2, num=3, endpoint=True, retstep=False, dtype=None, axis=0)
print(e)
# [[1. 1. ]
# [2.5 3. ]
# [4. 5. ]]
当start和stop为二维类数组时,axis不同取值下的情况:
# 当axis=0时
s1 = [[1, 2], [3, 4]]
s2 = [[5, 6], [7, 8]]
e = np.linspace(start=s1, stop=s2, num=3, endpoint=True, retstep=False, dtype=None, axis=0)
print(e)
# [[[1. 2.]
# [3. 4.]]
#
# [[3. 4.]
# [5. 6.]]
#
# [[5. 6.]
# [7. 8.]]]
# 当axis=1时
s1 = [[1, 2], [3, 4]]
s2 = [[5, 6], [7, 8]]
e = np.linspace(start=s1, stop=s2, num=3, endpoint=True, retstep=False, dtype=None, axis=1)
print(e)
# [[[1. 2.]
# [3. 4.]
# [5. 6.]]
#
# [[3. 4.]
# [5. 6.]
# [7. 8.]]]
# 当axis=2或-1时
s1 = [[1, 2], [3, 4]]
s2 = [[5, 6], [7, 8]]
e = np.linspace(start=s1, stop=s2, num=3, endpoint=True, retstep=False, dtype=None, axis=2)
print(e)
# [[[1. 3. 5.]
# [2. 4. 6.]]
#
# [[3. 5. 7.]
# [4. 6. 8.]]]
参考:
https://blog.csdn.net/qq_41550480/article/details/89390579?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162504137616780366564257%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=162504137616780366564257&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_positive~default-1-89390579.first_rank_v2_pc_rank_v29_1&utm_term=np.arange&spm=1018.2226.3001.4187
https://blog.csdn.net/qq_35240640/article/details/110660500?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162503824516780261924751%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=162503824516780261924751&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_click~default-2-110660500.first_rank_v2_pc_rank_v29_1&utm_term=np.linspace&spm=1018.2226.3001.4187