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)
| 5047 | return out |
| 5048 | |
| 5049 | def nonzero(self): |
| 5050 | """ |
| 5051 | Return the indices of unmasked elements that are not zero. |
| 5052 | |
| 5053 | Returns a tuple of arrays, one for each dimension, containing the |
| 5054 | indices of the non-zero elements in that dimension. The corresponding |
| 5055 | non-zero values can be obtained with:: |
| 5056 | |
| 5057 | a[a.nonzero()] |
| 5058 | |
| 5059 | To group the indices by element, rather than dimension, use |
| 5060 | instead:: |
| 5061 | |
| 5062 | np.transpose(a.nonzero()) |
| 5063 | |
| 5064 | The result of this is always a 2d array, with a row for each non-zero |
| 5065 | element. |
| 5066 | |
| 5067 | Parameters |
| 5068 | ---------- |
| 5069 | None |
| 5070 | |
| 5071 | Returns |
| 5072 | ------- |
| 5073 | tuple_of_arrays : tuple |
| 5074 | Indices of elements that are non-zero. |
| 5075 | |
| 5076 | See Also |
| 5077 | -------- |
| 5078 | numpy.nonzero : |
| 5079 | Function operating on ndarrays. |
| 5080 | flatnonzero : |
| 5081 | Return indices that are non-zero in the flattened version of the input |
| 5082 | array. |
| 5083 | numpy.ndarray.nonzero : |
| 5084 | Equivalent ndarray method. |
| 5085 | count_nonzero : |
| 5086 | Counts the number of non-zero elements in the input array. |
| 5087 | |
| 5088 | Examples |
| 5089 | -------- |
| 5090 | >>> import numpy as np |
| 5091 | >>> import numpy.ma as ma |
| 5092 | >>> x = ma.array(np.eye(3)) |
| 5093 | >>> x |
| 5094 | masked_array( |
| 5095 | data=[[1., 0., 0.], |
| 5096 | [0., 1., 0.], |
| 5097 | [0., 0., 1.]], |
| 5098 | mask=False, |
| 5099 | fill_value=1e+20) |
| 5100 | >>> x.nonzero() |
| 5101 | (array([0, 1, 2]), array([0, 1, 2])) |
| 5102 | |
| 5103 | Masked elements are ignored. |
| 5104 | |
| 5105 | >>> x[1, 1] = ma.masked |
| 5106 | >>> x |