Numpy count True in 2D array

In this post, we are going to learn about how to count True in a 2D Numpy array for example by using some built-in function in NumPy library np.sum(), np.count_nonzero() in rows of the 2D array, and columns of the 2D array. We have a boolean array of True and False in which we are counting total True values. Instead of counting the True value from the boolean array, count based on condition.

1. Numpy count True in a 2D 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 of True values in the whole NumPy array is returned. In this Python program, we have counted total True values in the whole array.

  • Install and Import the Numpy library using “import numpy as np”
  • To count True values column-wise set the axis = 0;
  • To count True values row-wise set the axis=1
import numpy as np
nparr =  np.array([[True, True, True, True],
                   [ True, True, False, True],
                   [True,False,False,False]])
print('Total true values in Whole Array: ', np.sum(nparr))
print('True values colum-wise: ', np.sum(nparr,axis=0))
print('True values row-wise: ', np.sum(nparr,axis=1))

Output

Total true values in Whole Array:  8
True values colum-wise:  [3 2 1 2]
True values row-wise:  [4 3 1]

Frequently Asked Questions

2. Numpy count True in the 2D array using count_nonzero()


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 elements in the whole numpy array. To count elements rows and columns wise we pass the axis as we are in the below programs.

  • In the case of the multidimensional array To count True in each column of the 2D array we pass argument axis=0.
  • To count True 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([[True, True, True, True],
                   [ True, True, False, True],
                   [True,False,False,False]])

print('Total true values in Whole Array: ', np.count_nonzero(nparr))
print('True values colum-wise: ', np.count_nonzero(nparr,axis=0))
print('True values row-wise: ', np.count_nonzero(nparr,axis=1))

Output

Total true values in Whole Array:  8
True values colum-wise:  [3 2 1 2]
True values row-wise:  [4 3 1]

3. Numpy count True in 2D using np.bincount


In this Python program we are using NumPy library function np.apply_along_axis() function along with and np.bincount() along as parameter to find True values count based on axis =0 or axis=1

import numpy as np
nparr =  np.array([[True, True, True, True],
                   [ True, True, False, True],
                   [True,False,False,False]])


print('as per axis=1 value : \n', np.apply_along_axis(np.bincount, 1, nparr))
print('as per axis=0 value:\n ',np.apply_along_axis(np.bincount, 0, nparr))

Output

as per axis=1 value : 
 [[0 4]
 [1 3]
 [3 1]]
as per axis=0 value:
  [[0 1 2 1]
 [3 2 1 2]]

4. Numpy count True in 2D 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==True.

import numpy as np
nparr =  np.array([[True, True, True, True],
                   [ True, True, False, True],
                   [True,False,False,False]])

result = np.where(nparr==True)
element_count = result[0].size

print('Count of True values in whole Numpy array :', element_count)

Output

Count of True values in whole Numpy array : 8

5. Numpy count True in 2D using np.bincount


In this Python program, we have used the python built-in sum() method to find all the True values in the NumPy array without using any numpy library function.

import numpy as np
nparr =  np.array([[True, True, True, True],
                   [ True, True, False, True],
                   [True,False,False,False]])

print('Numpy count True in Whole array  :',sum(nparr))

Output

Numpy count True in Whole array  : [3 2 1 2]

Summary

In this post, we have learned how to Numpy count True in a 2D array with examples.We have used built-in np.sum(),count_nonzero() and np.bincount,without using Numpy library function Python built-in function sum().