在 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 数组是否为空。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP