In this post, we are going to learn how to count False in NumPy array with examples by using Numpy library built-in function np. where(),np.sum(),np.size(),np.count_nonzero(),len().To run all the program NumPy must be installed and after successfully installation we can import it in our code.
1. Count False in NumPy array using np.where()
In this Python program example, we are using the numpy the where() function to count the True values in NumPy 2D array bypassing the condition nparr==False.The return an
- Import the Numpy library using “import numpy as np”
- Used the Numpy library function np.where() with condition nparr==False
- Get the number of False values in the Numpy array using result[0].size
import numpy as np
nparr = np.array([[True, True, True, True],
[ True, True, False, True],
[True,False,False,False]])
result = np.where(nparr==False)
element_count = result[0].size
print('Total False values in whole Numpy array :', element_count)
Output
Total False values in whole Numpy array:4
2. Count False in NumPy array using np.sum() and np.count_nonzero()
In this python program example, we have used the numpy library function np. size that returns the number of elements in the array and np.count_nonzero() that counts the number of non-zero values in the array we will subtract non-zero values from total elements in the array. To get the number of False values in a given array.
- Import the Numpy library using “import numpy as np”
- Used the Numpy library function np.size() to get the number of elements in the array.
- Used the count_nonzero() function to Get the number of non-zero values in the array.
import numpy as np
nparr = np.array([[True, True, True, True],
[ True, True, False, True],
[True,False,False,False]])
element_count = np.size(nparr) - np.count_nonzero(nparr)
print('Total False values in whole Numpy array :', element_count)
Output
Total False values in whole Numpy array : 4
3. How to count False in NumPy array using np.sum()
In this python program example, we will learn how to count False values in the NumPy array by using the numpy library sum() function with not operator(~) to get the number of False values in the numpy array.
#Python 3 program to count False in NumPy array
import numpy as np
nparr = np.array([[True, True, True, True],
[ True, True, False, True],
[True,False,False,False]])
element_count = (~nparr).sum()
print('Count of False values in whole Numpy array :', element_count)
Output
Total False values in whole Numpy array : 4
4. count False in NumPy array using len()
In this python program example, we have used the length function with conditions where values in the numpy array are equal to False. To get the number of False values in the Numpy array.
#Python 3 program to count False in NumPy array
import numpy as np
nparr = np.array([[True, True, True, True],
[ True, True, False, True],
[True,False,False,False]])
element_count = len(nparr[nparr == False])
print('Total False values in whole Numpy array :', element_count)
Output
Total False values in whole Numpy array : 4
5. count False in NumPy array using np.sum()
In this python program, we have used the numpy library function np. size that returns the number of elements in the array and np.sum() that counts the number of True values in the array we will subtract True values count from the array total elements. To get the number of False values in a given array
#Python 3 program to count False in NumPy array
import numpy as np
nparr = np.array([[True, True, True, True],
[ True, True, False, True],
[True,False,False,False]])
element_count = np.size(nparr) - np.sum(nparr)
print('Total False values in whole Numpy array :', element_count)
Output
Total False values in whole Numpy array:4
Summary
In this post, we have learned how to count False in the NumPy array built-in function np. where(),np.sum(), np.size(), np.count_nonzero(), len() with program examples.