Find the intersection of two arrays. Return the sorted, unique values that are in both of the input arrays. Parameters ---------- ar1, ar2 : array_like Input arrays. Will be flattened if not already 1D. assume_unique : bool If True, the input arrays are bot
(ar1, ar2, assume_unique=False, return_indices=False)
| 665 | |
| 666 | @array_function_dispatch(_intersect1d_dispatcher) |
| 667 | def intersect1d(ar1, ar2, assume_unique=False, return_indices=False): |
| 668 | """ |
| 669 | Find the intersection of two arrays. |
| 670 | |
| 671 | Return the sorted, unique values that are in both of the input arrays. |
| 672 | |
| 673 | Parameters |
| 674 | ---------- |
| 675 | ar1, ar2 : array_like |
| 676 | Input arrays. Will be flattened if not already 1D. |
| 677 | assume_unique : bool |
| 678 | If True, the input arrays are both assumed to be unique, which |
| 679 | can speed up the calculation. If True but ``ar1`` or ``ar2`` are not |
| 680 | unique, incorrect results and out-of-bounds indices could result. |
| 681 | Default is False. |
| 682 | return_indices : bool |
| 683 | If True, the indices which correspond to the intersection of the two |
| 684 | arrays are returned. The first instance of a value is used if there are |
| 685 | multiple. Default is False. |
| 686 | |
| 687 | Returns |
| 688 | ------- |
| 689 | intersect1d : ndarray |
| 690 | Sorted 1D array of common and unique elements. |
| 691 | comm1 : ndarray |
| 692 | The indices of the first occurrences of the common values in `ar1`. |
| 693 | Only provided if `return_indices` is True. |
| 694 | comm2 : ndarray |
| 695 | The indices of the first occurrences of the common values in `ar2`. |
| 696 | Only provided if `return_indices` is True. |
| 697 | |
| 698 | Examples |
| 699 | -------- |
| 700 | >>> import numpy as np |
| 701 | >>> np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1]) |
| 702 | array([1, 3]) |
| 703 | |
| 704 | To intersect more than two arrays, use functools.reduce: |
| 705 | |
| 706 | >>> from functools import reduce |
| 707 | >>> reduce(np.intersect1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2])) |
| 708 | array([3]) |
| 709 | |
| 710 | To return the indices of the values common to the input arrays |
| 711 | along with the intersected values: |
| 712 | |
| 713 | >>> x = np.array([1, 1, 2, 3, 4]) |
| 714 | >>> y = np.array([2, 1, 4, 6]) |
| 715 | >>> xy, x_ind, y_ind = np.intersect1d(x, y, return_indices=True) |
| 716 | >>> x_ind, y_ind |
| 717 | (array([0, 2, 4]), array([1, 0, 2])) |
| 718 | >>> xy, x[x_ind], y[y_ind] |
| 719 | (array([1, 2, 4]), array([1, 2, 4]), array([1, 2, 4])) |
| 720 | |
| 721 | """ |
| 722 | ar1 = np.asanyarray(ar1) |
| 723 | ar2 = np.asanyarray(ar2) |
| 724 |
searching dependent graphs…