Find the union of two arrays. Return the unique, sorted array of values that are in either of the two input arrays. Parameters ---------- ar1, ar2 : array_like Input arrays. They are flattened if they are not already 1D. Returns ------- union1d : ndarr
(ar1, ar2)
| 897 | |
| 898 | @array_function_dispatch(_union1d_dispatcher) |
| 899 | def union1d(ar1, ar2): |
| 900 | """ |
| 901 | Find the union of two arrays. |
| 902 | |
| 903 | Return the unique, sorted array of values that are in either of the two |
| 904 | input arrays. |
| 905 | |
| 906 | Parameters |
| 907 | ---------- |
| 908 | ar1, ar2 : array_like |
| 909 | Input arrays. They are flattened if they are not already 1D. |
| 910 | |
| 911 | Returns |
| 912 | ------- |
| 913 | union1d : ndarray |
| 914 | Unique, sorted union of the input arrays. |
| 915 | |
| 916 | See Also |
| 917 | -------- |
| 918 | numpy.lib.arraysetops : Module with a number of other functions for |
| 919 | performing set operations on arrays. |
| 920 | |
| 921 | Examples |
| 922 | -------- |
| 923 | >>> np.union1d([-1, 0, 1], [-2, 0, 2]) |
| 924 | array([-2, -1, 0, 1, 2]) |
| 925 | |
| 926 | To find the union of more than two arrays, use functools.reduce: |
| 927 | |
| 928 | >>> from functools import reduce |
| 929 | >>> reduce(np.union1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2])) |
| 930 | array([1, 2, 3, 4, 6]) |
| 931 | """ |
| 932 | return unique(np.concatenate((ar1, ar2), axis=None)) |
| 933 | |
| 934 | |
| 935 | def _setdiff1d_dispatcher(ar1, ar2, assume_unique=None): |