Test whether each element of a 1-D array is also present in a second array. Returns a boolean array the same length as `ar1` that is True where an element of `ar1` is in `ar2` and False otherwise. We recommend using :func:`isin` instead of `in1d` for new code. Parameters
(ar1, ar2, assume_unique=False, invert=False, *, kind=None)
| 523 | |
| 524 | @array_function_dispatch(_in1d_dispatcher) |
| 525 | def in1d(ar1, ar2, assume_unique=False, invert=False, *, kind=None): |
| 526 | """ |
| 527 | Test whether each element of a 1-D array is also present in a second array. |
| 528 | |
| 529 | Returns a boolean array the same length as `ar1` that is True |
| 530 | where an element of `ar1` is in `ar2` and False otherwise. |
| 531 | |
| 532 | We recommend using :func:`isin` instead of `in1d` for new code. |
| 533 | |
| 534 | Parameters |
| 535 | ---------- |
| 536 | ar1 : (M,) array_like |
| 537 | Input array. |
| 538 | ar2 : array_like |
| 539 | The values against which to test each value of `ar1`. |
| 540 | assume_unique : bool, optional |
| 541 | If True, the input arrays are both assumed to be unique, which |
| 542 | can speed up the calculation. Default is False. |
| 543 | invert : bool, optional |
| 544 | If True, the values in the returned array are inverted (that is, |
| 545 | False where an element of `ar1` is in `ar2` and True otherwise). |
| 546 | Default is False. ``np.in1d(a, b, invert=True)`` is equivalent |
| 547 | to (but is faster than) ``np.invert(in1d(a, b))``. |
| 548 | kind : {None, 'sort', 'table'}, optional |
| 549 | The algorithm to use. This will not affect the final result, |
| 550 | but will affect the speed and memory use. The default, None, |
| 551 | will select automatically based on memory considerations. |
| 552 | |
| 553 | * If 'sort', will use a mergesort-based approach. This will have |
| 554 | a memory usage of roughly 6 times the sum of the sizes of |
| 555 | `ar1` and `ar2`, not accounting for size of dtypes. |
| 556 | * If 'table', will use a lookup table approach similar |
| 557 | to a counting sort. This is only available for boolean and |
| 558 | integer arrays. This will have a memory usage of the |
| 559 | size of `ar1` plus the max-min value of `ar2`. `assume_unique` |
| 560 | has no effect when the 'table' option is used. |
| 561 | * If None, will automatically choose 'table' if |
| 562 | the required memory allocation is less than or equal to |
| 563 | 6 times the sum of the sizes of `ar1` and `ar2`, |
| 564 | otherwise will use 'sort'. This is done to not use |
| 565 | a large amount of memory by default, even though |
| 566 | 'table' may be faster in most cases. If 'table' is chosen, |
| 567 | `assume_unique` will have no effect. |
| 568 | |
| 569 | .. versionadded:: 1.8.0 |
| 570 | |
| 571 | Returns |
| 572 | ------- |
| 573 | in1d : (M,) ndarray, bool |
| 574 | The values `ar1[in1d]` are in `ar2`. |
| 575 | |
| 576 | See Also |
| 577 | -------- |
| 578 | isin : Version of this function that preserves the |
| 579 | shape of ar1. |
| 580 | numpy.lib.arraysetops : Module with a number of other functions for |
| 581 | performing set operations on arrays. |
| 582 |