Calculates ``element in test_elements``, broadcasting over `element` only. Returns a boolean array of the same shape as `element` that is True where an element of `element` is in `test_elements` and False otherwise. Parameters ---------- element : array_like Input a
(element, test_elements, assume_unique=False, invert=False, *,
kind=None)
| 957 | |
| 958 | @array_function_dispatch(_isin_dispatcher) |
| 959 | def isin(element, test_elements, assume_unique=False, invert=False, *, |
| 960 | kind=None): |
| 961 | """ |
| 962 | Calculates ``element in test_elements``, broadcasting over `element` only. |
| 963 | Returns a boolean array of the same shape as `element` that is True |
| 964 | where an element of `element` is in `test_elements` and False otherwise. |
| 965 | |
| 966 | Parameters |
| 967 | ---------- |
| 968 | element : array_like |
| 969 | Input array. |
| 970 | test_elements : array_like |
| 971 | The values against which to test each value of `element`. |
| 972 | This argument is flattened if it is an array or array_like. |
| 973 | See notes for behavior with non-array-like parameters. |
| 974 | assume_unique : bool, optional |
| 975 | If True, the input arrays are both assumed to be unique, which |
| 976 | can speed up the calculation. Default is False. |
| 977 | invert : bool, optional |
| 978 | If True, the values in the returned array are inverted, as if |
| 979 | calculating `element not in test_elements`. Default is False. |
| 980 | ``np.isin(a, b, invert=True)`` is equivalent to (but faster |
| 981 | than) ``np.invert(np.isin(a, b))``. |
| 982 | kind : {None, 'sort', 'table'}, optional |
| 983 | The algorithm to use. This will not affect the final result, |
| 984 | but will affect the speed and memory use. The default, None, |
| 985 | will select automatically based on memory considerations. |
| 986 | |
| 987 | * If 'sort', will use a mergesort-based approach. This will have |
| 988 | a memory usage of roughly 6 times the sum of the sizes of |
| 989 | `element` and `test_elements`, not accounting for size of dtypes. |
| 990 | * If 'table', will use a lookup table approach similar |
| 991 | to a counting sort. This is only available for boolean and |
| 992 | integer arrays. This will have a memory usage of the |
| 993 | size of `element` plus the max-min value of `test_elements`. |
| 994 | `assume_unique` has no effect when the 'table' option is used. |
| 995 | * If None, will automatically choose 'table' if |
| 996 | the required memory allocation is less than or equal to |
| 997 | 6 times the sum of the sizes of `element` and `test_elements`, |
| 998 | otherwise will use 'sort'. This is done to not use |
| 999 | a large amount of memory by default, even though |
| 1000 | 'table' may be faster in most cases. If 'table' is chosen, |
| 1001 | `assume_unique` will have no effect. |
| 1002 | |
| 1003 | |
| 1004 | Returns |
| 1005 | ------- |
| 1006 | isin : ndarray, bool |
| 1007 | Has the same shape as `element`. The values `element[isin]` |
| 1008 | are in `test_elements`. |
| 1009 | |
| 1010 | Notes |
| 1011 | ----- |
| 1012 | `isin` is an element-wise function version of the python keyword `in`. |
| 1013 | ``isin(a, b)`` is roughly equivalent to |
| 1014 | ``np.array([item in b for item in a])`` if `a` and `b` are 1-D sequences. |
| 1015 | |
| 1016 | `element` and `test_elements` are converted to arrays if they are not |
searching dependent graphs…