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)
| 618 | |
| 619 | @array_function_dispatch(_argwhere_dispatcher) |
| 620 | def argwhere(a): |
| 621 | """ |
| 622 | Find the indices of array elements that are non-zero, grouped by element. |
| 623 | |
| 624 | Parameters |
| 625 | ---------- |
| 626 | a : array_like |
| 627 | Input data. |
| 628 | |
| 629 | Returns |
| 630 | ------- |
| 631 | index_array : (N, a.ndim) ndarray |
| 632 | Indices of elements that are non-zero. Indices are grouped by element. |
| 633 | This array will have shape ``(N, a.ndim)`` where ``N`` is the number of |
| 634 | non-zero items. |
| 635 | |
| 636 | See Also |
| 637 | -------- |
| 638 | where, nonzero |
| 639 | |
| 640 | Notes |
| 641 | ----- |
| 642 | ``np.argwhere(a)`` is almost the same as ``np.transpose(np.nonzero(a))``, |
| 643 | but produces a result of the correct shape for a 0D array. |
| 644 | |
| 645 | The output of ``argwhere`` is not suitable for indexing arrays. |
| 646 | For this purpose use ``nonzero(a)`` instead. |
| 647 | |
| 648 | Examples |
| 649 | -------- |
| 650 | >>> import numpy as np |
| 651 | >>> x = np.arange(6).reshape(2,3) |
| 652 | >>> x |
| 653 | array([[0, 1, 2], |
| 654 | [3, 4, 5]]) |
| 655 | >>> np.argwhere(x>1) |
| 656 | array([[0, 2], |
| 657 | [1, 0], |
| 658 | [1, 1], |
| 659 | [1, 2]]) |
| 660 | |
| 661 | """ |
| 662 | # nonzero does not behave well on 0d, so promote to 1d |
| 663 | if np.ndim(a) == 0: |
| 664 | a = shape_base.atleast_1d(a) |
| 665 | # then remove the added dimension |
| 666 | return argwhere(a)[:, :0] |
| 667 | return transpose(nonzero(a)) |
| 668 | |
| 669 | |
| 670 | def _flatnonzero_dispatcher(a): |