MCPcopy Create free account
hub / github.com/numpy/numpy / nonzero

Method nonzero

numpy/ma/core.py:4978–5074  ·  view source on GitHub ↗

Return the indices of unmasked elements that are not zero. Returns a tuple of arrays, one for each dimension, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values can be obtained with:: a[a.nonzero()]

(self)

Source from the content-addressed store, hash-verified

4976 return out
4977
4978 def nonzero(self):
4979 """
4980 Return the indices of unmasked elements that are not zero.
4981
4982 Returns a tuple of arrays, one for each dimension, containing the
4983 indices of the non-zero elements in that dimension. The corresponding
4984 non-zero values can be obtained with::
4985
4986 a[a.nonzero()]
4987
4988 To group the indices by element, rather than dimension, use
4989 instead::
4990
4991 np.transpose(a.nonzero())
4992
4993 The result of this is always a 2d array, with a row for each non-zero
4994 element.
4995
4996 Parameters
4997 ----------
4998 None
4999
5000 Returns
5001 -------
5002 tuple_of_arrays : tuple
5003 Indices of elements that are non-zero.
5004
5005 See Also
5006 --------
5007 numpy.nonzero :
5008 Function operating on ndarrays.
5009 flatnonzero :
5010 Return indices that are non-zero in the flattened version of the input
5011 array.
5012 numpy.ndarray.nonzero :
5013 Equivalent ndarray method.
5014 count_nonzero :
5015 Counts the number of non-zero elements in the input array.
5016
5017 Examples
5018 --------
5019 >>> import numpy.ma as ma
5020 >>> x = ma.array(np.eye(3))
5021 >>> x
5022 masked_array(
5023 data=[[1., 0., 0.],
5024 [0., 1., 0.],
5025 [0., 0., 1.]],
5026 mask=False,
5027 fill_value=1e+20)
5028 >>> x.nonzero()
5029 (array([0, 1, 2]), array([0, 1, 2]))
5030
5031 Masked elements are ignored.
5032
5033 >>> x[1, 1] = ma.masked
5034 >>> x
5035 masked_array(

Callers 15

test_instance_methodsMethod · 0.80
flatnonzeroFunction · 0.80
_array_nonzeroFunction · 0.80
test_half_correctnessMethod · 0.80
test_half_orderingMethod · 0.80
test_half_funcsMethod · 0.80
test_attributesMethod · 0.80

Calls 1

filledMethod · 0.95