在 NumPy 中返回给定区间和步长内的等间距值
使用 **numpy.arange()** 方法创建一个包含整数元素的数组。第一个参数是“**start**”,即区间的起点。第二个参数是“**end**”,即区间的终点。第三个参数是步长,即值之间的间距。这里的默认步长为 2。
值在半开区间 [start, stop) 内生成。对于整数参数,该函数等效于 Python 内置的 range 函数,但返回的是 ndarray 而不是列表。
stop 是区间的终点。区间不包含此值,但在步长不是整数且浮点舍入影响 out 长度的一些情况下除外。步长是值之间的间距。对于任何输出 out,这是两个相邻值之间的距离,out[i+1] - out[i]。默认步长为 1。如果步长作为位置参数指定,则也必须给出 start。
步骤
首先,导入所需的库:
import numpy as np
使用 numpy.arange() 方法创建一个包含整数元素的数组。这里我们将步长设置为 2:
arr = np.arange(15, 30, step = 2)
print("Array...
", arr)获取数组的类型:
print("
Array type...
", arr.dtype)
获取数组的维度:
print("
Array Dimensions...
",arr.ndim)获取数组的形状:
print("
Our Array Shape...
",arr.shape)
获取数组的元素个数:
print("
Number of elements in the Array...
",arr.size)示例
import numpy as np
# Creating an array with int elements using the numpy.arange() method
# The 1st parameter is the "start" i.e. the start of the interval
# The 2nd parameter is the "end" i.e. the end of the interval
# The 3rd parameter is step size i.e. the spacing between values. The default step size is 1
# We have set the step size to 2 here
arr = np.arange(15, 30, step = 2)
print("Array...
", arr)
# Get the array type
print("
Array type...
", arr.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr.shape)
# Get the number of elements of the Array
print("
Number of elements in the Array...
",arr.size)输出
Array... [15 17 19 21 23 25 27 29] Array type... int64 Array Dimensions... 1 Our Array Shape... (8,) Number of elements in the Array... 8
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP