NumPy 什么是检查空数组的首选方法

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模块。

  • 使用numPy模块的array()函数(返回一个ndarray。ndarray是满足给定要求的数组对象)创建一个numpy数组。

  • 使用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的元组。可以用来判断数组是否为空。

示例

下面的程序使用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数组是否为空的首选方法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程