Numpy count_nonzero method | Python (original) (raw)
Last Updated : 22 Apr, 2020
numpy.count_nonzero()
function counts the number of non-zero values in the array arr.
Syntax : numpy.count_nonzero(arr, axis=None)
Parameters :
arr : [array_like] The array for which to count non-zeros.
axis : [int or tuple, optional] Axis or tuple of axes along which to count non-zeros. Default is None, meaning that non-zeros will be counted along a flattened version of arr.Return : [int or array of int] Number of non-zero values in the array along a given axis. Otherwise, the total number of non-zero values in the array is returned.
Code #1 :
import
numpy as geek
arr
=
[[
0
,
1
,
2
,
3
,
0
], [
0
,
5
,
6
,
0
,
7
]]
gfg
=
geek.count_nonzero(arr)
print
(gfg)
Output :
6
Code #2 :
import
numpy as geek
arr
=
[[
0
,
1
,
2
,
3
,
4
], [
5
,
0
,
6
,
0
,
7
]]
gfg
=
geek.count_nonzero(arr, axis
=
0
)
print
(gfg)
Output :
7
Similar Reads
- numpy.nonzero() in Python numpy.nonzero()function is used to Compute the indices of the elements that are non-zero. It returns a tuple of arrays, one for each dimension of arr, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values in the array can be obtained with arr[nonzero(ar 2 min read
- numpy.count() in Python numpy.core.defchararray.count(arr, substring, start=0, end=None): Counts for the non-overlapping occurrence of sub-string in the specified range. Parameters: arr : array-like or string to be searched. substring : substring to search for. start, end : [int, optional] Range to search in. Returns : An 1 min read
- numpy.ma.MaskedArray.nonzero() function - Python numpy.ma.MaskedArray.nonzero() function return the indices of unmasked elements that are not zero. This function returns a tuple of arrays, one for each dimension, containing the indices of the non-zero elements in that dimension. Syntax : numpy.ma.MaskedArray.nonzero(self) Return : [tuple] Indices 1 min read
- Python | Numpy np.legzero() method np.legzero() method can be used instead of np.zeros for creating a array whose elements are 0. Syntax : np.legzero() Return : Return array([0]) Example #1 : # Python program explaining # numpy.legzero() method # import numpy and legzero import numpy as np from numpy.polynomial.legendre import legzer 1 min read
- Python | os.cpu_count() method ]os.cpu_count() method in Python is used to get the number of CPUs in the system. This method returns None if the number of CPUs in the system is undetermined. In this article, we will see how to get several cores in Python. Python os.cpu_count() Method SyntaxSyntax: os.cpu_count() Parameter: No par 1 min read
- Python | sympy.count_ops() method Using the count_ops() method in simpy module, we can count the number of operations used in the mathematical function. count_ops() method returns the number of operations used in the mathematical function. Syntax : sympy.count_ops() Return : the count of operations. Code #1: With the help of below e 1 min read
- Python | Numpy np.hermezero() method With the help of np.hermezero() method, we can use hermezero instead of np.zeros() by using np.hermezero() method. Syntax : np.hermezero Return : Return the array of zeros. Example #1 : In this example we can see that by using np.hermezero() method, we are able to get the functionality of np.zeros a 1 min read
- Python | sympy.is_nonzero() method In sympy module, we can test whether a given number n is nonzero or not using sympy.is_nonzero() function. Syntax: sympy.is_nonzero(n) Parameter: n; number to be tested Return: bool value result Code #1: C/C++ Code # Python program to check nonzero # using sympy.is_nonzero() method # importing sympy 1 min read
- numpy.bincount() in Python In an array of +ve integers, the numpy.bincount() method counts the occurrence of each element. Each bin value is the occurrence of its index. One can also set the bin size accordingly. Syntax : numpy.bincount(arr, weights = None, min_len = 0) Parameters : arr : [array_like, 1D]Input array, having p 2 min read
- numpy.flatnonzero() in Python numpy.flatnonzero()function is used to Compute indices that are non-zero in the flattened version of arr. Syntax : numpy.flatnonzero(arr) Parameters : arr : [array_like] Input array. Return : ndarray Output array, containing the indices of the elements of arr.ravel() that are non-zero. Code #1 : Wor 1 min read