# Count number of True elements in a NumPy Array in Python

In this article, we will discuss different ways to count True elements in a bool Numpy Array.

**Table of Contents**

- Use count_nonzero() to count True elements in NumPy array.
- Use sum() to count True elements in a NumPy array.
- Use bincount() to count True elements in a NumPy array.
- Count True elements in 2D Array.
- Count True elements in each row of 2D Numpy Array / Matrix.
- Count True elements in each column of 2D Numpy Array / Matrix.

## Use count_nonzero() to count True elements in NumPy array

In Python, ** False** is equivalent to

**0**, whereas

**is equivalent to 1 i.e. a non-zero value.**

*True*Numpy module provides a function ** count_nonzero(arr, axis=None)**, which returns the count of non zero values in a given numpy array. When the value of axis argument is None, then it returns the count of non zero values in the complete array. But in case you are dealing with a multi-dimensional array, then you can use the axis argument to count non zero values along the given axis.

As non zero values are equivalent to True, so we can use the count_nonzero() to count **True **elements in a numpy array. For example,

import numpy as np arr = np.array([False, True, True, True, False, True, False, True, True]) print('Numpy Array:') print(arr) # Get count of True elements in a numpy array count = np.count_nonzero(arr) print('Print count of True elements in array: ', count)

Output:

Print count of True elements in array: 6

count_nonzero() returned the count of True elements in the numpy array.

## Use sum() to count True elements in a NumPy array

As True values are equivalent to 1 in Python. So, we can also add all the True values in a numpy array to get the count of True elements in a numpy array. For example,

import numpy as np arr = np.array([False, True, True, True, False, True, False, True, True]) # Get count of True elements in a numpy array count = np.sum(arr) print('Print count of True elements in array: ', count)

Output:

Print count of True elements in array: 6

It returned the count of all True elements in the array.

## Use bincount() to count True elements in a NumPy array

In python, the numpy module provides a function ** bincount(arr)**, which returns a count of number of occurrences of each value in array of non-negative ints.

bincount(arr), returned a result array, where ith element contains the occurence of i in arr. For example,

- result[1] contains the occurrences of 1 in array
- result[2] contains the occurrences of 2 in array
- result[3] contains the occurrences of 3 in array

As True is equivalent to 1 in python, so in the array returned by bincount() the element at index 1 contains the count of True elements in numpy array. For example,

import numpy as np arr = np.array([False, True, True, True, False, True, False, True, True]) # Get count of occurence of each value in numpy array of non-negative ints bin_arr = np.bincount(arr) # Get count of True elements in a numpy array count = bin_arr[1] print('Print count of True elements in array: ', count)

**Output:**

Print count of True elements in array: 6

## Count True elements in 2D Array

We can use the count_nonzero() with default value of axis parameter i.e. None, to get the count of all non zero values or True elements in complete 2D Numpy array. For example,

import numpy as np # Create a 2D Array of shape 3X3 arr_2d = np.array([ [False, True, True], [True, False, True], [False, True, True]]) print('Matrix / 2D Array:') print(arr_2d) # Count of True in complete 2D Numpy array count = np.count_nonzero(arr_2d) print('Print count of True elements in complete 2D array: ', count)

**Output:**

Print count of True elements in complete 2D array: 6

## Count True elements in each row of 2D Numpy Array / Matrix

To count the true elements in each row of a 2D Numpy Array or Matrix, pass the axis parameter as 1. For example,

import numpy as np # Create a 2D Array of shape 3X3 arr_2d = np.array([ [False, True, True], [True, False, True], [False, True, True]]) print('Matrix / 2D Array:') print(arr_2d) # Count of True elements in each row of 2D Numpy Array count = np.count_nonzero(arr_2d, axis=1) print('Print count of True elements in each row pf the 2D array: ', count)

**Output:**

Print count of True elements in each row pf the 2D array: [2 2 2]

It returned an array containing the count of True elements in each row of the original 2D array.

**Using sum() function:**

We can also use sum() to add the True values in each row of a 2D Numpy array. For that we need to pass the axis parameter as 1. For example,

import numpy as np # Create a 2D Array of shape 3X3 arr_2d = np.array([ [False, True, True], [True, False, True], [False, True, True]]) # Count of True elements in each row of 2D Numpy Array count = np.sum(arr_2d, axis=1) print(count)

Output:

[2 2 2]

It returned an array containing the count of True elements in each row of the original 2D array.

## Count True elements in each column of 2D Numpy Array / Matrix

To count the true elements in each column of a 2D Numpy Array or Matrix, pass the axis parameter as 0. For example,

import numpy as np # Create a 2D Array of shape 3X3 arr_2d = np.array([ [False, True, True], [True, False, True], [False, True, True]]) # Count of True elements in each column of 2D Numpy Array count = np.count_nonzero(arr_2d, axis=0) print('Print count of True elements in each column of the 2D array: ', count)

Output:

Print count of True elements in each column of the 2D array: [1 2 3]

It returned an array containing the count of True elements in each column of the original 2D array.

**Using sum() function:**

We can also use sum() to add the True values in each column of a 2D Numpy array. For that we need to pass the axis parameter as 0. For example,

import numpy as np # Create a 2D Array of shape 3X3 arr_2d = np.array([ [False, True, True], [True, False, True], [False, True, True]]) # Count of True elements in each column of 2D Numpy Array count = np.sum(arr_2d, axis=0) print(count)

Output:

[1 2 3]

It returned an array containing the count of True elements in each column of the original 2D array.

## Leave a Reply