首頁  >  文章  >  後端開發  >  如何在Python中從Numpy數組中選擇元素?

如何在Python中從Numpy數組中選擇元素?

WBOY
WBOY轉載
2023-08-30 09:17:06791瀏覽

如何在Python中從Numpy數組中選擇元素?

在本文中,我們將向您展示如何在 Python 中從 NumPy 陣列中選擇元素。

Python 中的 Numpy 陣列

顧名思義,NumPy 陣列是 NumPy 函式庫的中心資料結構。該函式庫的名稱是“Numeric Python”或“Numerical Python”的縮寫。

換句話說,NumPy 是一個 Python 函式庫,是 Python 科學計算的基礎。其中一個工具是高效能多維數組對象,它是用於高效數組和矩陣計算的強大資料結構。

我們可以一次從 Numpy 陣列中選擇一個元素或一個子陣列。現在我們看到以下從 Numpy 陣列中選擇元素的方法。

  • 選擇單一 NumPy 陣列元素
  • 使用切片從 NumPy 陣列中選擇子陣列
  • 僅透過給出停止值來選擇/訪問子數組
  • 僅透過給出起始值來選擇/訪問子數組

方法 1 - 選擇單一 NumPy 陣列元素

這些 ndarray 的每個元素都可以透過它們的索引號來存取。

演算法(步驟)

以下是執行所需任務所需遵循的演算法/步驟 -

  • 使用 import 關鍵字,匯入帶有別名 (np) 的 numpy 模組。

  • 使用numpy.array()函數(傳回一個ndarray。ndarray是滿足給定要求的陣列物件),透過傳遞一維數組來建立numpy數組數組作為它的參數。

  • 使用正索引存取索引 1 處的 NumPy 陣列元素並列印 它。

  • Use negative indexing to access the NumPy array element at index -1 i.e the last element of an array and print 它。

Negative Indexing():
Python allows for "indexing from the end," i.e., negative indexing.
This means that the last value in a sequence has an index of -1, the
second last has an index of -2, and so on.
When you want to pick values from the end (right side) of an iterable, you
can utilize negative indexing to your benefit.

範例

以下程式使用索引號碼從輸入 NumPy 陣列傳回指定索引處的元素 -

# importing numpy module with an alias name
import numpy as np

# creating a 1-Dimensional NumPy array
inputArray = np.array([4, 5, 1, 2, 8])

# printing the array element at index 1 (positive indexing)
print("The input array = ",inputArray)
print("Numpy array element at index 1:", inputArray[1])

# printing the array element at index -1 i.e last element (negative indexing)
print("Numpy array element at index -1(last element):", inputArray[-1])

輸出

執行時,上述程式將產生以下輸出 -

The input array =  [4 5 1 2 8]
Numpy array element at index 1: 5
Numpy array element at index -1(last element): 8

方法 2 - 使用切片從 NumPy 陣列中選擇子陣列

為了得到子數組,我們用切片代替元素索引。

文法

numpyArray[start:stop]

其中,start、stop分別是子陣列的第一個和最後一個索引。

演算法(步驟)

以下是執行所需任務所需遵循的演算法/步驟 -

  • 使用numpy.array()函數(傳回一個ndarray。ndarray是滿足給定要求的陣列物件),透過傳遞一維數組來建立numpy數組數組作為它的參數。

  • 透過給出起始值和終止值來存取從索引 2 到 5(不包括)的子數組 using slicing and printing 它。

#範例

以下程式透過給出開始值和停止值,使用切片從輸入 NumPy 陣列傳回子數組 -

# importing NumPy module with an alias name
import numpy as np

# creating a 1-Dimensional numpy array
inputArray = np.array([4, 5, 1, 2, 8, 9, 7])
print("Input Array =",inputArray)

# printing the sub-array from index 2 to 5(excluded) by giving start, stop values
print("The sub-array from index 2 to 5(excluded)=", inputArray[2:5])

輸出

執行時,上述程式將產生以下輸出 -

Input Array = [4 5 1 2 8 9 7]
The sub-array from index 2 to 5(excluded)= [1 2 8]

方法 3 - 透過僅給出停止值來選擇/訪問子數組

透過將起始索引留空,您可以從第一個元素開始對子陣列進行切片。

預設起始值為0

範例

以下程式傳回輸入 NumPy 陣列中從索引 0(預設)到給定停止值的子陣列 -

# importing NumPy module with an alias name
import numpy as np

# creating a 1-Dimensional NumPy array
inputArray = np.array([4, 5, 1, 2, 8, 9, 7])
print("Input Array =",inputArray)

# printing the sub-array till index 5(excluded) by giving only stop value

# it starts from index 0 by default
print("The sub-array till index 5(excluded)=", inputArray[:5])

輸出

執行時,上述程式將產生以下輸出 -

Input Array = [4 5 1 2 8 9 7]
The sub-array till index 5(excluded)= [4 5 1 2 8]

方法 4 - 僅給出起始值來選擇/訪問子數組

同樣,將冒號左側留空將為您提供一個數組,直到最後一個元素。

範例

以下程式傳回輸入 NumPy 陣列中從給定起始索引值到陣列最後一個索引(預設)的子陣列。

p>#

# importing NumPy module with an alias name
import numpy as np

# creating a 1-Dimensional NumPy array
inputArray = np.array([4, 5, 1, 2, 8, 9, 7])

# printing the sub-array from index 2 to the last index by giving only the start value
print("Input Array = ",inputArray)
# It extends till the last index value by default
print("The sub-array till index 5(excluded)=", inputArray[2:])

輸出

執行時,上述程式將產生以下輸出 -

Input Array = [4 5 1 2 8 9 7]
The sub-array till index 5(excluded)= [1 2 8 9 7]

結論

我們在本文中使用四個不同的範例學習如何在 Python 中選擇 numpy 陣列的元素。我們也了解了切片 Numpy 數組。

以上是如何在Python中從Numpy數組中選擇元素?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除