NumPy Count Nonzero Values in Python

NumPy count_nonzero() function in Python is used to count the number of nonzero elements present in the one-dimensional or multi-dimensional array. This function has 3 parameters as arr, axis, and keepdims. In this article, I will explain the syntax of NumPy count_nonzero() and use this function to count the number of nonzero values in the input array arr.

1. Quick Examples of Python NumPy count_nonzero() Function

If you are in a hurry, below are some quick examples of how to get count of nonzero by using NumPy count_nonzero() function in Python. For more examples of NumPy refer to NumPy Tutorial.


# Below are the quick examples

# Example 1: count non-zero elements in numpy array
arr = np.array([8, 23, 45, 0, 60, 0, 85, 0, 101])
arr2 = np.count_nonzero(arr)

# Example 2: count values in numpy array that satisfy a condition 
arr2 = np.count_nonzero(arr % 45 == 0)

# Example 3: another example of satisfying a condition
arr2 = np.count_nonzero(arr > 8)

# Example 4: get numpy count_nonzero() value of 2-d array
arr = np.array([[4, 0, 0, 12], [15, 0, 26, 34], [0, -7, 15, 0]])
arr2 = np.count_nonzero(arr)

# Example 5: get numpy count_nonzero() along the axis = 0  
arr2 = np.count_nonzero(arr, axis = 0)

# Example 6: get numpy count_nonzero() along the axis = 1
arr2 = np.count_nonzero(arr, axis = 1)  
  
# Example 7: use axis=1 and keepdims = True
arr2 = np.count_nonzero(arr, axis=1, keepdims=True)

# Example 8: get counting true elements in numpy array
arr = np.array([True, True, False, True, False, True, False, True, False])
arr2 = np.count_nonzero(arr)

2. Syntax of NumPy count_nonzero()

Following is the syntax of the numpy.count_nonzero() function.


# Syntax of numpy.count_nonzero()
numpy.count_nonzero(arr, axis=None, *, keepdims=False)

2.1 Parameters of count_nonzero()

The count_nonzero() function allows the following parameters.

  • arr - This is the input array you wanted to count nonzeros values.
  • axis - Axis or tuple of axes along which to count nonzeros. By default, a flattened array is used. axis = 0 means along the column and axis = 1 means working along the row.
  • keepdims - The value is set to be True, this creates reduced axes with dimensions of one size.

2.2 Return Value of count_nonzero()

It returns int or array of int with number of nonzero values in the array along a given axis. If no axis specified, the total number of nonzero values in the array is returned.

3. Count nonzero Elements in NumPy Array

Let’s count the number of nonzero values of a single dimension array using the NumPy count_nonzero() function. This function takes the array as input and returns the count of the elements by ignoring zero’s.


import numpy as np

# Create one-dimensional array of numbers
arr = np.array([8, 23, 45, 0, 60, 0, 85, 0, 101])

# count non-zero elements in numpy array
arr2 = np.count_nonzero(arr)
print (arr2) 

# Output
# 6

4. Count Values in Numpy Array that Satisfy a Condition

We can count the nonzero values as we did in the previous example but, here I will pass the condition to check.


# count values in numpy array that satisfy a condition 
arr2 = np.count_nonzero(arr % 45 == 0)
print (arr2) 

# Output
# 4

# another example of satisfying a condition
arr2 = np.count_nonzero(arr > 8)
print (arr2) 

# Output
# 5

5. Get NumPy count_nonzero() Value of 2-D Array

Let’s take a 2-Dimensional array and counts the number of nonzero value using numpy.count_nonzero() function. If you don’t specify the axis, it just flattern the array and return the count by ignoring zero values.


import numpy as np

# Create 2-D array
arr = np.array([[4, 0, 0, 12], [15, 0, 26, 34], [0, -7, 15, 0]])

# Get numpy count_nonzero() value of 2-d array
arr2 = np.count_nonzero(arr)
print(arr2)

# Output
# 6

6. Get NumPy count_nonzero() along the Axis

We can count the nonzero values along the axis, For example, if we set axis=0, then nonzero values are counted along the column, and if axis=1, then nonzero values are counted along the row.


# Get numpy count_nonzero() along the axis = 0  
arr2 = np.count_nonzero(arr, axis = 0)
print (arr2)

# Output
[2 1 2 2]

# Get numpy count_nonzero() along the axis = 1
arr2 = np.count_nonzero(arr, axis = 1)
print (arr2) 

# Output
# [2 3 2]

7. Use axis=1 and keepdims = True

We can also use NumPy to count the nonzero value of an array along with specified axis and keepdims, keepdims argument keeps the dimensions in the result.


# Use axis=1 and keepdims = True
arr2 = np.count_nonzero(arr, axis=1, keepdims=True)
print (arr2) 

# Output
# [[2]
#  [3]
#  [2]]

8. Get Counting True Elements in NumPy Array

We can use numpy.count_nonzero() function to get count the True elements in a bool numpy array. python True is equivalent to one and False is equivalent to zero.


import numpy as np

# Create array
arr = np.array([True, True, False, True, False, True, False, True, False])

# get counting true elements in numpy array
arr2 = np.count_nonzero(arr)
print (arr2) 

# Output
# 5

Conclusion

In this article, I have explained how to use NumPy count_nonzero() function in Python and using this function how to get count the number of nonzero values for 1-dimension and 2-dimension arrays along with specified axis.

Happy Learning!!

You May Also Like

References

Leave a Reply

You are currently viewing NumPy Count Nonzero Values in Python