4 Examples to Use Numpy count_nonzero() Function

In the Numpy module, we have discussed many functions used to operate on the multidimensional array. Sometimes, we come to a situation where we need to find the count of the nonzero elements in the array. So, there is a function in the numpy library, i.e., count_nonzero(). In this tutorial, we will discuss the concept of the Numpy count_nonzero(), which is used to give the count of the nonzero elements present in the multidimensional array.

What is Numpy count_nonzero() function?

The Numpy count_nonzero() function is used to give the count of the nonzero elements present in the multidimensional array. With the help of this function, we can find the count of the elements in the multidimensional array which are not zero. This function has 3 parameters as arr, axis and, keepdims.

Syntax of numpy count_nonzero() function

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

Parameters of numpy count_nonzero() function

  • arr: It is an array-like input for which we have to count the nonzero array’s value.
  • axis: It is an integer value or a tuple and an optional input—Axis or tuple of axes along which we have to count non-zeros. By default, it is None, meaning that non-zeros will be counted along with a flattened version of arr.
  • Keepdims: It is the boolean value and an optional input. If set to True, the axes counted are left in the result as dimensions with size one.

Return value of numpy count_nonzero() function

The function’s return value is the number of nonzero values in the given array along the particular axis. Otherwise, It returns the count of nonzero values in the array.

Examples of numpy count_nonzero() function

Here, we will be discussing how we can count the nonzero values with the help of the function from the numpy package of python.

1. Using eye() function

In this example, we will be importing the numpy library. Then, we will be using the eye() function from the numpy library for the input array. After that, we will be applying the numpy count_nonzero() function for calculating the count of nonzero values in the input array. Let us look at the example for understanding the concept in detail.

#importing numpy library
import numpy as np

#using eye() for input
arr = np.eye(5)

#applying count_nonzero() function
count = np.count_nonzero(arr)

print("Nonzero count in the input array : ",count) 

Output:

Nonzero count in the input array :  5

Explanation:

  • Firstly, we will import the numpy library with an alias name as np.
  • Then, we will take the input from the eye() function in the numpy library.
  • After that, we will apply the count function and store the count in the count variable.
  • At last, we will print the output.
  • Hence, you can see the count of nonzero values in the given array.

2. Taking input as multidimensional array

In this example, we will be importing the numpy library. Then, we will be taking the input as a multidimensional array. After that, we will apply the numpy count_nonzero() function and print the output. Let us look at the example for understanding the concept in detail.

#importing numpy library
import numpy as np

arr = [[0, 1, 2, 0, 4], [5, 0, 6, 0, 7]]

#applying count_nonzero() function
count = np.count_nonzero(arr)

print("Nonzero count in the input array : ",count) 

Output:

zero count in the input array :  6

Explanation:

  • Firstly, we will import the numpy library with an alias name as np.
  • Then, we will take a multidimensional array as input in the arr variable.
  • After that, we will apply the count function and store the count in the count variable.
  • At last, we will print the output.
  • Hence, you can see the count of the nonzero value in the multidimensional array.

3. Taking multidimensional array and axis as parameter

In this example, we will be importing the numpy library. Then, we will be taking the input as a multidimensional array. We will then apply the numpy count_nonzero() function and axis equal to 0 and 1. Then, print both the output in different variables. Let us look at the example for understanding the concept in detail.

#importing numpy library
import numpy as np

arr = [[0, 1, 2, 0, 4], [5, 0, 6, 0, 7]]

#applying count_nonzero() function
count = np.count_nonzero(arr, axis = 0)
print("Nonzero count in the input array with axis = 0 : ",count) 

c = np.count_nonzero(arr, axis = 1)
print("Nonzero count in the input array with axis = 1 : ",c) 

Output:

Nonzero count in the input array with axis = 0 :  [1 1 2 0 2]
Nonzero count in the input array with axis = 1 :  [3 3]

Explanation:

  • Firstly, we will import the numpy library with an alias name as np.
  • Then, we will take a multidimensional array as input in the arr variable.
  • After that, we will apply the count function and store the count in the count variable.
  • Inside the count function, we have applied axis equals to 0 and 1 as the parameter.
  • At last, we will print the output.
  • Hence, you can see the count of the nonzero value in the multidimensional array.

4. Taking all the parameters

In this example, we will be importing the numpy library. Then, we will be taking the input as a multidimensional array. We will then apply the count_nonzero() function and axis equal to 0 and 1, and keepdims equals True as a parameter. Then, print both the output in different variables. Let us look at the example for understanding the concept in detail.

#importing numpy library
import numpy as np

arr = [[0, 1, 2, 0, 4], [5, 0, 6, 0, 7]]

#applying count_nonzero() function
count = np.count_nonzero(arr, axis = 0, Keepdims = True)
print("Nonzero count in the input array with axis = 0 : ",count) 

c = np.count_nonzero(arr, axis = 1, Keepdims = True)
print("Nonzero count in the input array with axis = 1 : ",c) 

Output:

Nonzero count in the input array with axis = 0 :  [[1 1 2 0 2]]
Nonzero count in the input array with axis = 1 :  [[3]
 [3]]

Explanation:

  • Firstly, we will import the numpy library with an alias name as np.
  • Then, we will take a multidimensional array as input in the arr variable.
  • After that, we will apply the count function and store the count in the count variable.
  • Inside the count function, we have applied axis equals 0 and 1, and keepdims equals True as the parameter.
  • At last, we will print the output.
  • Hence, you can see the count of the nonzero value in the multidimensional array.

Also, See

Conclusion

In this tutorial, we have learned about the concept of the Numpy count_nonzero() function. We have seen that what is numpy count_nonzero() function with its syntax and parameters explained. We have also discussed all the ways through which we can use the given function. All the ways are explained in detail with the help of examples. You can use any of the functions according to your choice and your requirement in the program.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments