在 Numpy 中返回給定區間內的等間距值


使用 **numpy.arange()** 方法建立具有整數元素的陣列。第一個引數是“**起始**”,即區間的起始位置。第二個引數是“**結束**”,即區間的結束位置。第三個引數是值之間的間距。預設步長為 1。

在半開區間 [start, stop) 內生成值。對於整數引數,該函式等效於 Python 內建的 range 函式,但返回 ndarray 而不是列表。

stop 是區間的結束位置。區間不包含此值,但在某些情況下,步長不是整數並且浮點數舍入會影響 out 的長度。step 是值之間的間距。對於任何輸出 out,這是兩個相鄰值之間的距離,out[i+1] - out[i]。預設步長為 1。如果 step 指定為位置引數,則也必須提供 start。

步驟

首先,匯入所需的庫 -

import numpy as np

您需要使用 numpy.arange() 方法建立一個具有整數元素的陣列 -

arr = np.arange(10, 20)
print("Array...
", arr)

顯示陣列 -

print("
Array type...
", arr.dtype)

獲取陣列型別 -

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 the spacing between values. The default step size is 1
arr = np.arange(10, 20)
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...
[10 11 12 13 14 15 16 17 18 19]

Array type...
int64

Array Dimensions...
1

Our Array Shape...
(10,)

Number of elements in the Array...
10

更新於: 2022年2月10日

184 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.