NumPy count_nonzero() - Count the number of non-zero elements in an array
The count_nonzero()
counts the number of non-zero elements in a NumPy array.
import numpy
a1 = numpy.array([5, 0, 9, 1])
a2 = numpy.array([[1, 0, 0, 1], [1, 1, 0, 2]])
c1 = numpy.count_nonzero(a1)
c2 = numpy.count_nonzero(a2)
print(c1) # 3
print(c2) # 5
NumPy Array
- Addition / subtraction
- Add a NumPy array and Python integer
- Dimension of array
- Zero array
- Make an arithmetic progression
- Get random integers
- get the value of a given index
- Sort an array
- Split an array
- Iterate an array
- NumPy where
- Get the subarray of an array satisfying a condition
- Sum all the elements in an array by columns or rows
- Count non-zero elements
- Get the index of the max value
- Convert the 2D or 3D array to 1D array
- Convert 1D array to 2D array
- Convert an NumPy array to a Python list
- Concatenate arrays
- Delete the elements from an array