In this post, we are going to learn NumPy count zero values with examples. We will learn how to count zero in each row or column of a numpy array by using np. where(), np.count_nonzero(),np.sum(), np.bincount()
1.NumPy count zero in whole array
To count all zero values in the NumPy array we have three NumPy functions that we are going to understand with examples one by one that can be used with 1D,2D, and Multidimensional arrays.
- np.count_nonzero()
- np.where()
- np.sum()
- np.bincount()
1.1 np.count_nonzero() to count All zero values in array
The count_nonzero() returns the count of elements of the numpy array for a condition that returns True. Where True is equal to 1 or False is equal is 0. If we do not pass the axis in count_nonzero() returns the count of all zero values in the numpy array.
import numpy as np
nparr = np.array([[3, 6, 9, 0],
[ 0, 0, 12, 0],
[15,28,0,27]])
print('Total zero in Whole array :',np.count_nonzero(nparr==0))
Output
Total zero in Whole array : 5
Frequently Asked Questions
- NumPy count elements by condition
- Numpy count Frequency of value
- NumPy 2D array count unique value occurrence
- How to count non-nan elements in NumPy array
- NumPy count zero values (4 methods)
1.2 NumPy.sum() to count all zero values in array
The np. sum() function returns the sum of elements over the specified axis of an array default value for the axis is None. If the axis is not specified the sum() function count of all Zero values in the whole NumPy array is returned.
import numpy as np
nparr = np.array([[3, 6, 9, 0],
[ 0, 0, 12, 0],
[15,28,0,27]])
print('np.sum() : Total zero in Whole array :',np.sum(nparr==0))
Output
np.sum() : Total zero in Whole array : 5
1.3 Numpy count all zero using np.where()
In this python program example, we are using the numpy the where() function to count the zero values in NumPy 2D array bypassing the condition nparr==0
import numpy as np
nparr = np.array([[3, 6, 9, 0],
[ 0, 0, 12, 0],
[15,28,0,27]])
result = np.where(nparr==0)
print(result)
zero_inArr = result[0].size
print('Total zero in Whole array :',zero_inArr)
Output
Total zero in Whole array : 5
1.4 bincount() to count all zero in a 1D numpy array
In this Python program, we have used np. bincount() function to count all zero values in 1D numpy array
import numpy as np
nparr = np.array([3, 6, 9, 0, 0, 0, 12, 0,15,28,0,27])
zerocount = np.bincount(nparr)
print('NumPy all zero vales in 1D array:',zerocount[0])
Output
NumPy all zero vales in 1D array: 5
2. NumPy count zero values in each row using count_nonzero()
To count elements rows and columns wise we pass the axis as we are in the below programs.
- To count zeros values in each row of a 2D array We pass argument axis=1 in the count_nonzero() function.
import numpy as np
nparr = np.array([[3, 6, 9, 0],
[ 0, 0, 12, 0],
[15,28,0,27]])
print('np.count_nonzero(): count zero in each row :',np.count_nonzero(nparr==0,axis=1))
Output
np.count_nonzero(): count zero in each row : [1 3 1]
3. NumPy count zero values in each row using np.sum()
To count zeros values in each row of a 2D array ,We pass argument axis=1 in the np.sum() function.Let us understand with the below python program.
import numpy as np
nparr = np.array([[3, 6, 9, 0],
[ 0, 0, 12, 0],
[15,28,0,27]])
print('np.sum(): count zero in each row :',np.sum(nparr==0,axis =1))
Output
np.sum(): count zero in each row : [1 3 1]
4. NumPy count zero values in each column using np.count_nonzero()
In the case of the multidimensional array To count Zero values in each column of the 2D array we pass argument axis=0.Let us understand how to count zero in each column of a numpy 2D array.
import numpy as np
nparr = np.array([[3, 6, 9, 0],
[ 0, 0, 12, 0],
[15,28,0,27]])
print('np.count_nonzero(): count zero in each column :',np.count_nonzero(nparr==0,axis=0))
Output
np.count_nonzero(): count zero in each column : [1 1 1 2]
5. NumPy count zero in each column using np. sum()
In this Python program, we are counting zero values in each column of the numpy array using the np.sum() function by setting the axis=0 values in the whole array.Install and Import the Numpy library using “import numpy as np”
import numpy as np
nparr = np.array([[3, 6, 9, 0],
[ 0, 0, 12, 0],
[15,28,0,27]])
print('np.sum(): count zero in each column :',np.sum(nparr==0,axis =0))
Output
np.sum(): count zero in each column : [1 1 1 2]