# numpy.count_nonzero() – Python

## Using numpy.count_nonzero() Function

np.count_nonzero: In this article we will discuss about how to count values based on conditions in 1D or 2D Numpy Arrays using numpy.count_nonzero() function in python. So let’s explore the topic.

### numpy.count_nonzero() :

A function numpy.count_nonzero() is provided by Numpy module in python to count the non-zero values in array,

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

Where,

• arr : It represents the array like object in which we want to count the non zero values.
• axis : It represents along which we want to count the values. If the value is 1 then then it will count non zero values in rows  and if the value is 0 then it will count non zero values in columns and if the value is None then it will count non zero values by flattening the arrays.
• kepdims : It takes the bool value and if the value is True, then the axes that are counted are left in the result as dimensions with size one.

Which returns int or array of int containing count of non zero values in numpy array and if the Axis is provided then it returns the array of count of values along the axis.

### Counting non zero values in a Numpy Array :

Suppose we have a numpy array with some zeros and non zero values. Now we will count the non zero values using numpy.count_nonzero() function.

So let’s see the program to understand how it actually works.

# Program :

import numpy as np
# numpy array from list created
arr = np.array([2, 3, 0, 5, 0, 0, 5, 0, 5])
# Counting non zero elements in numpy array
count = np.count_nonzero(arr)
print('Total count of non-zero values in NumPy Array: ', count)
Output :
Total count of non-zero values in NumPy Array: 5

### Counting True values in a numpy Array :

As we know in python True is equivalent to 1 and False is equivalent to 0 then we can use the numpy.count_nonzero() function to count the True values in a bool numpy array.

# Program :

import numpy as np
# Numpy Array of bool values created
arr = np.array([False, True, True, True, False, False, False, True, True])
# Counting True elements in numpy array
count = np.count_nonzero(arr)
print('Total count of True values in NumPy Array: ', count)
Output :
Total count of True values in NumPy Array: 5

### Counting Values in Numpy Array that satisfy a condition :

It is very simple to count the non-zero values as we did in previous example we only passed the complete numpy array , here we will pass the condition.

So lets see the example to understand it clearly.

# Program :

import numpy as np
# A Numpy array of numbers is created
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('Total count of Even Numbers in Numpy Array: ', count)
Output :
Total count of Even Numbers in Numpy Array: 4

In the above example which element will satisfy the condition the value will be True and which will not satisfy the value will be false. And it will count the True values.

### Counting Non-Zero Values in 2D Numpy Array :

By using the same numpy.count_nonzero() function we can count the non-zero values in a 2D array where the default axis value is None.

So lets see the example to understand it clearly.

# Program :

import numpy as np
# 2D Numpy Array created
arr_2d = np.array( [[20, 30, 0],
[50, 0, 0],
[50, 0, 50]])
# counting of non zero values in complete 2D array
count = np.count_nonzero(arr_2d)
print('Total count of non zero values in complete 2D array: ', count)
Output :
Total count of non zero values in complete 2D array:  5

### Counting Non-Zero Values in each row of 2D Numpy Array :

To count the non-zero values in each row of 2D numpy array just pass value of axis as 1.

So lets see the example to understand it clearly.

# Program :

import numpy as np
# Create 2D Numpy ARray
arr = np.array( [[20, 30, 0],
[50, 0, 0],
[50, 0, 50]])
# Get count of non zero values in each row of 2D array
count = np.count_nonzero(arr, axis=1)
print('Total count of non zero values in each row of 2D array: ', count)
Output :
Total count of non zero values in each row of 2D array: [2 1 2]

### Counting Non-Zero Values in each column of 2D Numpy Array :

To count the non-zero values in each columnof 2D numpy array just pass value of axis as 0.

So lets see the example to understand it clearly.

# Program :

import numpy as np
# 2D Numpy Array created
arr = np.array( [[20, 30, 0],
[50, 0, 0],
[50, 0, 50]])
# counting of non zero values in each column of 2D array
count = np.count_nonzero(arr, axis=0)
print('Total count of non zero values in each column of 2D array: ', count)
Output :
Total count of non zero values in each column of 2D array: [3 1 1]