Check if Numpy Array is Empty
Numpy is a Python library that stands for ‘Numerical Python’. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently. As with any programming language, it is often necessary to check if an array is empty before performing any operations on it. In this article, we will explore different methods to check if a Numpy array is empty and provide code examples with their execution results.
Method 1: Using the size()
attribute
One simple way to check if a Numpy array is empty is by looking at its size
attribute. If the size of the array is zero, then it is empty.
import numpy as np
arr1 = np.array([])
arr2 = np.array([1, 2, 3])
print(arr1.size == 0) # True
print(arr2.size == 0) # False
Output:
In the above code, arr1
is an empty array, and arr2
contains elements. By comparing the sizes of these arrays with zero, we determine if they are empty or not.
Method 2: Using the shape()
attribute
The shape
attribute of a Numpy array returns a tuple that specifies the dimensions of the array. If the shape tuple is empty, it means the array has no elements and is therefore empty.
import numpy as np
arr1 = np.array([])
arr2 = np.array([1, 2, 3])
print(arr1.shape == ()) # False
print(arr2.shape == ()) # False
Output:
In this example, the shape of arr1
is an empty tuple ()
, indicating that it has zero dimensions. On the other hand, arr2
has a shape (3,)
, indicating it has a single dimension with three elements.
Method 3: Using the ndim()
attribute
The ndim
attribute of a Numpy array specifies the number of dimensions it has. An empty array has zero dimensions, so we can check if the ndim
attribute is zero to determine if the array is empty.
import numpy as np
arr1 = np.array([])
arr2 = np.array([1, 2, 3])
print(arr1.ndim == 1) # True
print(arr2.ndim == 0) # False
Output:
In the above code, arr1
has zero dimensions, while arr2
has one dimension. By comparing the ndim
attribute of these arrays with zero, we can check if they are empty.
Method 5: Using the all()
function
The all()
function can be used to check if all elements in an array evaluate to True
. If an array is empty, the all()
function will return True
since there are no elements to evaluate.
import numpy as np
arr1 = np.array([])
arr2 = np.array([1, 2, 3])
print(all(arr1)) # True
print(all(arr2)) # True
Output:
In the code above, both arr1
and arr2
are considered empty because all their elements evaluate to True
(probabilistically, False
maps to 0 and True
maps to a non-zero value).
Method 6: Using the any()
function
The any()
function returns True
if at least one element in an array evaluates to True
. Thus, for an empty array, the any()
function will return False
.
import numpy as np
arr1 = np.array([])
arr2 = np.array([1, 2, 3])
print(any(arr1)) # False
print(any(arr2)) # True
Output:
In the above code, arr1
is an empty array, so the any()
function returns False
. However, arr2
contains elements, so the function returns True
.
Method 7: Using the np.prod()
function
The np.prod()
function calculates the product of all elements in an array. If an array is empty, the result will be 1
since there are no elements to multiply.
import numpy as np
arr1 = np.array([])
arr2 = np.array([1, 2, 3])
print(np.prod(arr1) == 1) # True
print(np.prod(arr2) == 6) # True
Output:
In this code, both arr1
and arr2
are considered empty because their product is equal to 1
. Note that the product of an empty array is defined as 1
mathematically.
Method 8: Using the np.any()
function with np.isnan()
The np.isnan()
function returns a boolean mask indicating the positions of NaN
(Not a Number) values in an array. We can use this function in conjunction with np.any()
to check if an array contains NaN
values or is empty.
import numpy as np
arr1 = np.array([])
arr2 = np.array([1, 2, 3])
print(np.any(np.isnan(arr1)) or arr1.size == 0) # True
print(np.any(np.isnan(arr2)) or arr2.size == 0) # False
Output:
In the above code, arr1
is empty, and we have considered empty arrays as containing NaN
values by using arr1.size == 0
. Therefore, the expression evaluates to True
. However, arr2
contains a NaN
value, so the expression evaluates to False
.
Method 9: Using the np.inf()
value and np.isfinite()
function
The np.inf
value represents infinity in Numpy. By using the np.isfinite()
function, we can check if an array contains infinite values or is empty.
import numpy as np
arr1 = np.array([])
arr2 = np.array([1, 2, 3])
print(not np.any(np.isfinite(arr1)) or arr1.size == 0) # True
print(not np.any(np.isfinite(arr2)) or arr2.size == 0) # False
Output:
In this code, arr1
is empty, and we have considered empty arrays as containing infinite values by using arr1.size == 0
. Therefore, the expression evaluates to True
. However, arr2
contains an infinite value, so the expression evaluates to False
.
Method 10: Using a combination of methods
Sometimes, it can be useful to combine multiple methods to check if a Numpy array is empty. By using the logical operators and
and or
, we can construct more complex expressions.
import numpy as np
arr1 = np.array([])
arr2 = np.array([1, 2, 3])
print((arr1.shape == ()) and (arr1.size == 0)) # True
print((arr2.shape == ()) and (arr2.size == 0)) # False
Output:
In the code above, we combine two conditions for an empty array: the shape tuple is empty and the size is zero. By using the logical operator and
, both conditions must be satisfiedfor the expression to evaluate to True
. For arr1
, both conditions are met, so the expression evaluates to True
. However, for arr2
, the shape is (3,)
and the size is 3
, so the expression evaluates to False
.
Conclusion of check if numpy array is empty
In conclusion, there are multiple ways to check if a Numpy array is empty. The most commonly used methods include checking the size
attribute, the shape
attribute, and the ndim
attribute of the array. Additionally, Numpy provides functions like all()
, any()
, and np.prod()
that can be used to check for emptiness. By combining these methods and using logical operators, you can create more complex expressions to suit your specific needs.
Keep in mind that when working with arrays, it is essential to check if they are empty before performing any operations to avoid errors and unexpected results.