Find the set difference of two arrays. Return the unique values in `ar1` that are not in `ar2`. Parameters ---------- ar1 : array_like Input array. ar2 : array_like Input comparison array. assume_unique : bool If True, the input arrays are both
(ar1, ar2, assume_unique=False)
| 938 | |
| 939 | @array_function_dispatch(_setdiff1d_dispatcher) |
| 940 | def setdiff1d(ar1, ar2, assume_unique=False): |
| 941 | """ |
| 942 | Find the set difference of two arrays. |
| 943 | |
| 944 | Return the unique values in `ar1` that are not in `ar2`. |
| 945 | |
| 946 | Parameters |
| 947 | ---------- |
| 948 | ar1 : array_like |
| 949 | Input array. |
| 950 | ar2 : array_like |
| 951 | Input comparison array. |
| 952 | assume_unique : bool |
| 953 | If True, the input arrays are both assumed to be unique, which |
| 954 | can speed up the calculation. Default is False. |
| 955 | |
| 956 | Returns |
| 957 | ------- |
| 958 | setdiff1d : ndarray |
| 959 | 1D array of values in `ar1` that are not in `ar2`. The result |
| 960 | is sorted when `assume_unique=False`, but otherwise only sorted |
| 961 | if the input is sorted. |
| 962 | |
| 963 | See Also |
| 964 | -------- |
| 965 | numpy.lib.arraysetops : Module with a number of other functions for |
| 966 | performing set operations on arrays. |
| 967 | |
| 968 | Examples |
| 969 | -------- |
| 970 | >>> a = np.array([1, 2, 3, 2, 4, 1]) |
| 971 | >>> b = np.array([3, 4, 5, 6]) |
| 972 | >>> np.setdiff1d(a, b) |
| 973 | array([1, 2]) |
| 974 | |
| 975 | """ |
| 976 | if assume_unique: |
| 977 | ar1 = np.asarray(ar1).ravel() |
| 978 | else: |
| 979 | ar1 = unique(ar1) |
| 980 | ar2 = unique(ar2) |
| 981 | return ar1[in1d(ar1, ar2, assume_unique=True, invert=True)] |