Python: Count Number of True Elements in Numpy Array in Python

Method to find count number of true element in numpy array in python

In this article, we will study what are true elements in python and different methods to calculate the count of true elements in both 1-D and 2-D Numpy array. First, let see what are true elements in python.

True Elements

True elements in numpy array are equivalent to 1 and False element is equivalent to 0. So we can say that it is basically a boolean numpy array.

Now let us see different methods to count a number of True elements in a 1-D numpy array.

  • Method 1-Using count_nonzero() method

This is one of the methods to count a number of true elements in a numpy array. count_nonzero() method require 1 argument i.e. the name of numpy array and then it return the count of true element in numpy array. It also accepts axis as one of the arguments but we do not need it in this case.

Syntax: count_nonzero(arr, axis=None)

Let see this with the help of an example.

import numpy as np

arr1 = np.array([False, True, True, False,True])
arr2 = np.array([0,1,1,0,1])
count1 = np.count_nonzero(arr1)
count2 = np.count_nonzero(arr2)
print('Print count of True elements in array1: ', count1)
print('Print count of True elements in array2: ', count2)

Output

Print count of True elements in array1:  3
Print count of True elements in array2:  3

There are two things to notice in this example first is that in arr1 count of True is 3 hence we get output as 3 and the second thing to notice is that if we write 1 and 0 instead of True and False respectively then we will get the same output. Hence it is also verified that True is equivalent to 1 and False is equivalent to 0 in a numpy array.

  • Method 2-Using sum() function

sum() is another method to count a number of true elements in a numpy array. Here sum() method works in the same way that we see in basic python. It returns the sum of all the elements in a Numpy array. As True is equivalent to 1 and False is equivalent to 0 so the sum we get is equal to count of True elements.

syntax: sum(arr)

Let see this with the help of an example.

import numpy as np

arr1 = np.array([False, True, True, False,True])
count1 = sum(arr1)
print('Print count of True elements in array1: ', count1)

Output

Print count of True elements in array1:  3
  • Method 3 Using bincount() method

bincount() return list of occurrence of each value in numpy array. Let see this with an example.

import numpy as np

arr1 = np.array([False, True, True, False,True])
l = np.bincount(arr1)
print(l)

Output

[2 3]

Here we see that at index 0 it return the count of the number of false values and at index 1 it return a count of true values. So now using list indexing we can return the count of elements at the 1st index which is the count of True values in the Numpy array.

So these are the methods to count a number of True values in a 1-D Numpy array.

Now we see that how to calculate the count of numpy array in the 2-D array. The best part is that the above methods that we study are used to calculate the count of True elements in a 2-D numpy array but the difference is that we can give an extra parameter as the axis to calculate the count of True elements row-wise or column-wise.

  • Method 1-Using count_nonzero() method

The working of the method is the same that we study in the case of a 1-D numpy array. But here we have a choice that we can give the axis as a parameter or not. If we do not give axis as a parameter then this method will return the total count of True elements in a numpy array but if we give an axis it returns a list of the count of True element row-wise or column-wise. There are two axis

axis=1 means return count row-wise

axis=0 means return count column-wise

Let see this with the help of an example.

import numpy as np

arr_2d = np.array([ [False, True, True,False],
                    [True, False, True,False],
                    [False, True, True,False]])
count1 = np.count_nonzero(arr_2d)
count2 = np.count_nonzero(arr_2d,axis=1)
count3 = np.count_nonzero(arr_2d,axis=0)
print("Total count of True values",count1)
print("Count of True values row-wise",count2)
print("Count of True values column-wise",count3)

Output

 

Total count of True values 6
Count of True values row-wise [2 2 2]
Count of True values column-wise [1 2 3 0]
  • Method 2-Using sum() method

The working of the method is the same that we study in the case of a 1-D numpy array. But here we have a choice that we can give the axis as a parameter or not. If we do not give axis as a parameter then this method will return the total count of True elements in a numpy array but if we give an axis it returns a list of the count of True element row-wise or column-wise. There are two axis

axis=1 means return count row-wise

axis=0 means return count column-wise

Here we have to take care of one this that we can’t simply write sum() here we have to write np.sum() because we use axis as one of argument.

Let see this with the help of an example.

import numpy as np

arr_2d = np.array([ [False, True, True,False],
                    [True, False, True,False],
                    [False, True, True,False]])
count1 = np.sum(arr_2d)
count2 = np.sum(arr_2d,axis=1)
count3 = np.sum(arr_2d,axis=0)
print("Total count of True values",count1)
print("Count of True values row-wise",count2)
print("Count of True values column-wise",count3)

Output

Total count of True values 6
Count of True values row-wise [2 2 2]
Count of True values column-wise [1 2 3 0]

So these are the methods to count the number of True values in both 1-D and 2-D numpy array.