MCPcopy
hub / github.com/numpy/numpy / nonzero

Function nonzero

numpy/_core/fromnumeric.py:2032–2115  ·  view source on GitHub ↗

Return the indices of the elements that are non-zero. Returns a tuple of arrays, one for each dimension of `a`, containing the indices of the non-zero elements in that dimension. The values in `a` are always tested and returned in row-major, C-style order. To group the ind

(a)

Source from the content-addressed store, hash-verified

2030
2031@array_function_dispatch(_nonzero_dispatcher)
2032def nonzero(a):
2033 """
2034 Return the indices of the elements that are non-zero.
2035
2036 Returns a tuple of arrays, one for each dimension of `a`,
2037 containing the indices of the non-zero elements in that
2038 dimension. The values in `a` are always tested and returned in
2039 row-major, C-style order.
2040
2041 To group the indices by element, rather than dimension, use `argwhere`,
2042 which returns a row for each non-zero element.
2043
2044 Parameters
2045 ----------
2046 a : array_like
2047 Input array.
2048
2049 Returns
2050 -------
2051 tuple_of_arrays : tuple
2052 Indices of elements that are non-zero.
2053
2054 See Also
2055 --------
2056 flatnonzero :
2057 Return indices that are non-zero in the flattened version of the input
2058 array.
2059 ndarray.nonzero :
2060 Equivalent ndarray method.
2061 count_nonzero :
2062 Counts the number of non-zero elements in the input array.
2063
2064 Notes
2065 -----
2066 While the nonzero values can be obtained with ``a[nonzero(a)]``, it is
2067 recommended to use ``x[x.astype(np.bool)]`` or ``x[x != 0]`` instead, which
2068 will correctly handle 0-d arrays.
2069
2070 Examples
2071 --------
2072 >>> import numpy as np
2073 >>> x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
2074 >>> x
2075 array([[3, 0, 0],
2076 [0, 4, 0],
2077 [5, 6, 0]])
2078 >>> np.nonzero(x)
2079 (array([0, 1, 2, 2]), array([0, 1, 0, 1]))
2080
2081 >>> x[np.nonzero(x)]
2082 array([3, 4, 5, 6])
2083 >>> np.transpose(np.nonzero(x))
2084 array([[0, 0],
2085 [1, 1],
2086 [2, 0],
2087 [2, 1]])
2088
2089 A common use for ``nonzero`` is to find the indices of an array, where

Callers 5

extractFunction · 0.90
mask_indicesFunction · 0.90
argwhereFunction · 0.85
whereFunction · 0.85
test_nonzeroMethod · 0.85

Calls 1

_wrapfuncFunction · 0.85

Tested by 1

test_nonzeroMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…