在 NumPy 中檢查空陣列的首選方法是什麼?


在這篇文章中,我們將向您展示在 Numpy 中檢查空陣列的首選方法。

NumPy 是一個 Python 庫,旨在高效地處理 Python 中的陣列。它速度快、易於學習且儲存效率高。它還改進了資料處理的方式。在 NumPy 中,我們可以生成一個 n 維陣列。要使用 NumPy,我們只需將其匯入到我們的程式中,然後我們就可以輕鬆地在程式碼中使用 NumPy 的功能。

NumPy 是一個流行的 Python 包,用於科學和統計分析。NumPy 陣列是來自相同資料型別的值的網格。

使用 numpy.any() 方法

numpy.any() 函式確定給定軸上的任何陣列元素是否計算結果為 True。

語法

numpy.any(a, axis = None, out = None, keepdims = <no value>)

引數

  • a − 必須檢查其元素的輸入陣列。

  • axis − 它是評估陣列元素的軸。

  • out − 與輸入陣列具有相同維度的輸出陣列。

  • keepdims − 如果將其設定為 True,則結果中將保留縮減的軸。

返回值 − 根據“out”引數返回一個新的布林陣列

演算法(步驟)

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

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

  • 使用 array() 函式(返回一個 ndarray。ndarray 是一個滿足給定要求的陣列物件)NumPy 模組來建立 NumPy 陣列。

  • 使用 numpy.any() 函式(numpy.any() 函式確定指定軸上的任何陣列成員是否為 True)來檢查輸入陣列是否為空或不為空,即返回 True。

  • 如果 any() 函式返回 False,則給定陣列為空,否則不為空。

示例

以下程式使用 numpy.any() 函式返回給定的 NumPy 陣列是否為空:

# importing NumPy module with an alias name import numpy as np # creating a NumPy array inputArray = np.array([]) # Checking whether the input array is empty or not using any() function # Here it returns false if the array is empty else it returns true temp_flag = np.any(inputArray) # checking whether the temp_flag is false (Numpy array is empty) if temp_flag == False: # printing empty array, if the condition is true print('Empty NumPy Input Array') else: # else printing NOT Empty print('Input NumPy Array is NOT Empty')

輸出

執行上述程式將生成以下輸出:

Empty NumPy Input Array

使用 numpy.size() 方法

要計算給定軸上元素的數量,我們使用 Python 的 numpy.size() 方法。

語法

numpy.size(a, axis=None)

引數

  • a − 輸入陣列。

  • axis − 它是計算陣列元素的軸。

返回值 − 返回指定軸上元素的數量。

示例

以下程式使用 numpy.size() 函式返回給定的 NumPy 陣列是否為空:

# importing numpy module with an alias name import numpy as np # creating a numpy array inputArray = np.array([]) # Getting the size of inputArray arraySize = np.size(inputArray) # Checking whether the array size is 0(empty array) or not(array containing elements) if arraySize==0: # printing empty array, if the condition is true print('Empty NumPy Input Array') else: # else printing NOT Empty print('Input NumPy Array is NOT Empty')

輸出

執行上述程式將生成以下輸出:

Empty NumPy Input Array

透過將 NumPy 陣列轉換為列表

在這種方法中,我們首先使用 tolist() 方法將陣列轉換為列表。然後使用 len() 方法檢查列表的長度以檢視陣列是否為空。

示例

以下程式使用 len() 函式返回給定的 NumPy 陣列是否為空:

# importing numpy module with an alias name import numpy as np # creating an array inputArray = np.array([]) # converting NumPy array to list and checking # whether the length of the list is equal to 0 if len(inputArray.tolist()) == 0: # Print Empty if condition is true print("Empty input array") else: # else printing not Empty array print('Input array is NOT empty')

輸出

執行上述程式將生成以下輸出:

Empty input array

使用 size 屬性方法

size 屬性 −此屬性計算 NumPy 陣列中元素的總數。

示例

以下程式使用 size 屬性返回給定的 NumPy 陣列是否為空:

import numpy as np # creating a NumPy array inputArray = np.array([]) # checking whether the size of the array is equal to 0 if inputArray.size == 0: # prining empty array if condition is true print("Empty input array") else: # else printing not Empty array print('Input array is NOT empty')

輸出

執行上述程式將生成以下輸出:

Empty input array

在這種方法中,我們使用 inputArray.size 屬性來確定陣列是否為空。此運算子返回陣列大小,在本例中為 0,從而產生預期結果。

使用 shape 屬性

這是一個 NumPy 陣列屬性,它提供一個包含陣列形狀的元組。這可以用來檢視陣列是否為空。

示例

以下程式使用 shape 屬性返回給定的 NumPy 陣列是否為空:

import numpy as np # creating a numpy array inputArray = np.array([]) # checking whether the shape of the array is equal to 0 (Empty array condition) if inputArray.shape[0] == 0: # prining empty array if condition is true print("Empty input array") else: # else printing not Empty array print('Input array is NOT empty')

輸出

執行上述程式將生成以下輸出:

Empty input array

結論

本文向我們介紹了 5 種不同型別的首選方法,用於確定給定的 NumPy 陣列是否為空。

更新於: 2022 年 10 月 20 日

19K+ 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.