如何使用 Numpy 排列创建一系列线性增加的值?
在本文中,我们将使用 Numpy排列()函数创建一系列线性增加的值。
入门
通过使用arrange()函数,我们可以创建一系列线性增加的值。此方法返回一个数组,其中元素按间隔均匀分布。提到的间隔是半开即 [Start, Stop)
Syntax: numpy.arrange(start, stop, step)
Parameters:
- start is the starting value
- stop is the ending value
- step is a linear increment value with in the given range. It is optional. By default, it is 1.
Return Type:
- Return an array of elements
例如:
numpy.arrange(1,10,3) # array([1,4,7])
在这里,我们给出了从 1 到 10(开始 = 1 和停止 = 10)的范围,但我们指定了 step=3。这意味着它跳过一个范围内的每 3 个元素。所以通过这种方式,我们可以增加数据的线性度。
示例 1:
以 3 步线性方式创建范围内的元素。
Python3
# importing numpy module
import numpy as np
# create an elements from 1
# to 10 with 3 step linearity
print(np.arrange(1, 10, 3))
# create an elements from 1
# to 20 with 3 step linearity
print(np.arrange(1, 20, 3))
# create an elements from 1
# to 30 with 3 step linearity
print(np.arrange(1, 30, 3))
# create an elements from 1
# to 40 with 3 step linearity
print(np.arrange(1, 40, 3))
# create an elements from 1
# to 50 with 3 step linearity
print(np.arrange(1, 50, 3))
Python3
#importing numpy module
import numpy as np
#create an elements from 1 to
# 10 with 5 step linearity
print(np.arrange(1,10,5))
#create an elements from 1 to
# 20 with 5 step linearity
print(np.arrange(1,20,5))
#create an elements from 1 to
# 30 with 5 step linearity
print(np.arrange(1,30,5))
#create an elements from 1 to
# 40 with 5 step linearity
print(np.arrange(1,40,5))
#create an elements from 1 to
# 50 with 5 step linearity
print(np.arrange(1,50,5))
Python3
#importing numpy module
import numpy as np
#create an elements from 34 to 50 with 4 step linearity
print(np.arrange(34,50,5))
输出:
示例 2:
以 5 步线性方式创建范围内的元素。
蟒蛇3
#importing numpy module
import numpy as np
#create an elements from 1 to
# 10 with 5 step linearity
print(np.arrange(1,10,5))
#create an elements from 1 to
# 20 with 5 step linearity
print(np.arrange(1,20,5))
#create an elements from 1 to
# 30 with 5 step linearity
print(np.arrange(1,30,5))
#create an elements from 1 to
# 40 with 5 step linearity
print(np.arrange(1,40,5))
#create an elements from 1 to
# 50 with 5 step linearity
print(np.arrange(1,50,5))
输出:
示例 3:
以 5 步线性创建 34 到 50 范围内的元素。
蟒蛇3
#importing numpy module
import numpy as np
#create an elements from 34 to 50 with 4 step linearity
print(np.arrange(34,50,5))
输出:
[34 39 44 49]