numpy.count_nonzero() – Python

In this article, we will learn all about numpy.count_nonzero() function in python and see how to use it to count values based on conditions in 1D or 2D Numpy Arrays.

Table of Contents

numpy.count_nonzero()

Numpy module in python provides a function to count non-zero values in array,

numpy.count_nonzero(arr, axis=None, keepdims=False)

Arguments:

  • arr: array like object
    • The Array in which we want to count the non zero values
  • axis: int or tuple, optional
    • Axis along which we want to count the values.
      • If 1 then it will count non zero values in rows.
      • If 0 then it will count non zero values in columns.
      • If None then it will flatten the array and then count non zero values in it.
  • keepdims: bool
    • If True, them the axes that are counted are left in the result as dimensions with size one.

Returns:

  • int or array of int
    • Returns the count of non zero values in numpy array.
    • If Axis is provided then returns the array of count of values along the axis.

In Python, True is equivalent to 1 and False is equivalent to 0. So, we can use can use count_nonzero() function to count values in numpy array that satisfy a condition. Let’s learn that step by step with examples.

Count non zero values in a Numpy Array

Suppose we have a numpy array of integers, which contains some zeros and some non zero values. To count the all the non zeros values in array, use the count_nonzero() function. For example,

import numpy as np

# Create a numpy array from list
arr = np.array([2, 3, 0, 5, 0, 0, 5, 0, 5])

# Count non zero elements in numpy array
count = np.count_nonzero(arr)

print('Count of non-zero values in NumPy Array: ', count)

Output:

Count of non-zero values in NumPy Array: 5

Count True values in a numpy Array

In Python, True is equivalent to 1 and False is equivalent to 0. So, we can use can use count_nonzero() function to count True values in a bool numpy array. For example,

import numpy as np

# Create a Numpy Array of bool values
arr = np.array([False, True, True, True, False, True, False, True, True])

# Count True elements in numpy array
count = np.count_nonzero(arr)

print('Count of True values in NumPy Array: ', count)

Output:

Count of True values in NumPy Array: 6

Now we will see why counting True values in a bool array is important.

Count Values in Numpy Array that satisfy a condition

When we apply a condition to a numpy array like arr > 3, then it returns a bool array of same size as arr. It contains True at places where the element in arr satisfies the condition i.e. greater than 3 in this case, all other values are False. So, if we count True values in bool array returned by arr>3, then it will give us the count of values that satisfies the condition in array i.e. values greater than 3 in this case. Let’s use this logic to count values in numpy array based on conditions. For example,

Count Even numbers in a Numpy Array

import numpy as np

# Numpy array of numbers
arr = np.array([2, 3, 1, 5, 4, 2, 5, 6, 5])

# Count even number of even elements in array
count = np.count_nonzero(arr % 2 == 0)

print('Count of Even Numbers in Numpy Array: ', count)

Output:

Count of Even Numbers in Numpy Array: 4

Count Non-Zero Values in 2D Numpy Array

Suppose we have a 2D Numpy array and we want to count all non zero values in it. To do that we can use count_nonzero() function with default value of axis parameter i.e. None. For example,

import numpy as np

# Create 2D Numpy ARray
arr_2d = np.array( [[2, 3, 0],
                    [5, 0, 0],
                    [5, 0, 5]])

# Get count of non zero values in complete 2D array
count = np.count_nonzero(arr_2d)

print('Count of non zero values in complete 2D array: ', count)

Output:

Count of non zero values in complete 2D array: 5

Count Non-Zero Values in each row of 2D Numpy Array

Suppose we have a 2D Numpy array and we want to count all non zero values in each row of it. To do that we can use count_nonzero() function with axis parameter as 1. For example,

import numpy as np

# Create 2D Numpy ARray
arr_2d = np.array( [[2, 3, 0],
                    [5, 0, 0],
                    [5, 0, 5]])

# Get count of non zero values in each row of 2D array
count = np.count_nonzero(arr_2d, axis=1)

print('Count of non zero values in each row of 2D array: ', count)

Output:

Count of non zero values in each row of 2D array: [2 1 2]

It returned an array containing count of non zero values in each row.

Count Non-Zero Values in each column of 2D Numpy Array

Suppose we have a 2D Numpy array and we want to count all non zero values in each column of it. To do that we can use count_nonzero() function with axis parameter as 0. For example,

import numpy as np

# Create 2D Numpy ARray
arr_2d = np.array( [[2, 3, 0],
                    [5, 0, 0],
                    [5, 0, 5]])

# Get count of non zero values in each column of 2D array
count = np.count_nonzero(arr_2d, axis=0)

print('Count of non zero values in each column of 2D array: ', count)

Output:

Count of non zero values in each column of 2D array: [3 1 1]

It returned an array containing count of non zero values in each column.

Count values in 2D Numpy array based on condition

To count all the values in 2D array that satisfy a condition, we can use the count_nonzero() function with different values of axis parameter

  • axis=None, to count all values in 2D array that satisfy a condition.
  • axis=1, to count all values in each row of 2D array that satisfy a condition.
  • axis=0, to count all values in each column of 2D array that satisfy a condition.

For example,

import numpy as np

# Create 2D Numpy ARray
arr_2d = np.array( [[2, 3, 0],
                    [5, 0, 0],
                    [5, 0, 5]])


# Get count of even values in complete 2D array
count = np.count_nonzero(arr_2d % 2 == 0)

print('Count of even values in complete 2D array: ', count)

# Get count of even values in each row of 2D array
count = np.count_nonzero(arr_2d % 2 == 0, axis=1)

print('Count of even values in each row of 2D array: ', count)

# Get count of even values in each column of 2D array
count = np.count_nonzero(arr_2d % 2 == 0, axis=0)

print('Count of even values in each column of 2D array: ', count)

Output:

Count of even values in complete 2D array:  5
Count of even values in each row of 2D array:  [2 2 1]
Count of even values in each column of 2D array:  [1 2 2]

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top