如何在NumPy中检查空数组?
在本文中,我们将展示检查Numpy中空数组的首选方法。
NumPy 是为了在Python中有效地处理数组而设计的Python库。它速度快,易于学习,并且存储效率高。它还改进了处理数据的方式。在NumPy中,我们可以生成n维数组。要使用NumPy,我们只需在程序中导入它,然后就可以轻松地在代码中使用NumPy的功能。
NumPy是一种流行的Python包,用于科学和统计分析。 NumPy数组是具有相同数据类型值的网格。
阅读更多:Python 教程
使用numpy.any()方法
numpy.any() 函数确定沿给定轴的任何数组元素是否计算为True。
语法
numpy.any(a, axis=None, out=None, keepdims=<no value>)
参数
- a - 必须检查其元素的输入数组。
-
axis - 它是要评估数组元素的轴。
-
out - 与输入数组具有相同维数的输出数组。
-
keepdims - 如果将其设置为True,则缩小的轴将保留在结果中。
返回值 - 根据“out”参数返回新的布尔数组
算法(步骤)
以下是执行所需任务的算法/步骤 –
- 使用import关键字以别名(np)导入numpy模块。
-
使用numPy模块的array()函数(返回ndarray。 ndArray是满足给定要求的数组对象)创建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 attribute -该属性计算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,结果是预期的。
使用形状属性
这是一个numpy数组属性,它给出包含数组 形状 的元组。可以用来查看数组是否为空。
例子
下面的程序使用形状属性返回给定的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数组是否为空。