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)
| 1880 | |
| 1881 | @array_function_dispatch(_nonzero_dispatcher) |
| 1882 | def nonzero(a): |
| 1883 | """ |
| 1884 | Return the indices of the elements that are non-zero. |
| 1885 | |
| 1886 | Returns a tuple of arrays, one for each dimension of `a`, |
| 1887 | containing the indices of the non-zero elements in that |
| 1888 | dimension. The values in `a` are always tested and returned in |
| 1889 | row-major, C-style order. |
| 1890 | |
| 1891 | To group the indices by element, rather than dimension, use `argwhere`, |
| 1892 | which returns a row for each non-zero element. |
| 1893 | |
| 1894 | .. note:: |
| 1895 | |
| 1896 | When called on a zero-d array or scalar, ``nonzero(a)`` is treated |
| 1897 | as ``nonzero(atleast_1d(a))``. |
| 1898 | |
| 1899 | .. deprecated:: 1.17.0 |
| 1900 | |
| 1901 | Use `atleast_1d` explicitly if this behavior is deliberate. |
| 1902 | |
| 1903 | Parameters |
| 1904 | ---------- |
| 1905 | a : array_like |
| 1906 | Input array. |
| 1907 | |
| 1908 | Returns |
| 1909 | ------- |
| 1910 | tuple_of_arrays : tuple |
| 1911 | Indices of elements that are non-zero. |
| 1912 | |
| 1913 | See Also |
| 1914 | -------- |
| 1915 | flatnonzero : |
| 1916 | Return indices that are non-zero in the flattened version of the input |
| 1917 | array. |
| 1918 | ndarray.nonzero : |
| 1919 | Equivalent ndarray method. |
| 1920 | count_nonzero : |
| 1921 | Counts the number of non-zero elements in the input array. |
| 1922 | |
| 1923 | Notes |
| 1924 | ----- |
| 1925 | While the nonzero values can be obtained with ``a[nonzero(a)]``, it is |
| 1926 | recommended to use ``x[x.astype(bool)]`` or ``x[x != 0]`` instead, which |
| 1927 | will correctly handle 0-d arrays. |
| 1928 | |
| 1929 | Examples |
| 1930 | -------- |
| 1931 | >>> x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]]) |
| 1932 | >>> x |
| 1933 | array([[3, 0, 0], |
| 1934 | [0, 4, 0], |
| 1935 | [5, 6, 0]]) |
| 1936 | >>> np.nonzero(x) |
| 1937 | (array([0, 1, 2, 2]), array([0, 1, 0, 1])) |
| 1938 | |
| 1939 | >>> x[np.nonzero(x)] |