Find the indices of array elements that are non-zero, grouped by element. Parameters ---------- a : array_like Input data. Returns ------- index_array : (N, a.ndim) ndarray Indices of elements that are non-zero. Indices are grouped by element. T
(a)
| 560 | |
| 561 | @array_function_dispatch(_argwhere_dispatcher) |
| 562 | def argwhere(a): |
| 563 | """ |
| 564 | Find the indices of array elements that are non-zero, grouped by element. |
| 565 | |
| 566 | Parameters |
| 567 | ---------- |
| 568 | a : array_like |
| 569 | Input data. |
| 570 | |
| 571 | Returns |
| 572 | ------- |
| 573 | index_array : (N, a.ndim) ndarray |
| 574 | Indices of elements that are non-zero. Indices are grouped by element. |
| 575 | This array will have shape ``(N, a.ndim)`` where ``N`` is the number of |
| 576 | non-zero items. |
| 577 | |
| 578 | See Also |
| 579 | -------- |
| 580 | where, nonzero |
| 581 | |
| 582 | Notes |
| 583 | ----- |
| 584 | ``np.argwhere(a)`` is almost the same as ``np.transpose(np.nonzero(a))``, |
| 585 | but produces a result of the correct shape for a 0D array. |
| 586 | |
| 587 | The output of ``argwhere`` is not suitable for indexing arrays. |
| 588 | For this purpose use ``nonzero(a)`` instead. |
| 589 | |
| 590 | Examples |
| 591 | -------- |
| 592 | >>> x = np.arange(6).reshape(2,3) |
| 593 | >>> x |
| 594 | array([[0, 1, 2], |
| 595 | [3, 4, 5]]) |
| 596 | >>> np.argwhere(x>1) |
| 597 | array([[0, 2], |
| 598 | [1, 0], |
| 599 | [1, 1], |
| 600 | [1, 2]]) |
| 601 | |
| 602 | """ |
| 603 | # nonzero does not behave well on 0d, so promote to 1d |
| 604 | if np.ndim(a) == 0: |
| 605 | a = shape_base.atleast_1d(a) |
| 606 | # then remove the added dimension |
| 607 | return argwhere(a)[:,:0] |
| 608 | return transpose(nonzero(a)) |
| 609 | |
| 610 | |
| 611 | def _flatnonzero_dispatcher(a): |