numpy 1.22.4 Pypi GitHub Homepage
Other Docs
NotesParametersReturnsBackRef
isin(element, test_elements, assume_unique=False, invert=False)

Notes

isin is an element-wise function version of the python keyword :None:None:`in`. isin(a, b) is roughly equivalent to np.array([item in b for item in a]) if a and :None:None:`b` are 1-D sequences.

:None:None:`element` and :None:None:`test_elements` are converted to arrays if they are not already. If :None:None:`test_elements` is a set (or other non-sequence collection) it will be converted to an object array with one element, rather than an array of the values contained in :None:None:`test_elements`. This is a consequence of the array constructor's way of handling non-sequence collections. Converting the set to a list usually gives the desired behavior.

versionadded

Parameters

element : array_like

Input array.

test_elements : array_like

The values against which to test each value of :None:None:`element`. This argument is flattened if it is an array or array_like. See notes for behavior with non-array-like parameters.

assume_unique : bool, optional

If True, the input arrays are both assumed to be unique, which can speed up the calculation. Default is False.

invert : bool, optional

If True, the values in the returned array are inverted, as if calculating :None:None:`element not in test_elements`. Default is False. np.isin(a, b, invert=True) is equivalent to (but faster than) np.invert(np.isin(a, b)) .

Returns

isin : ndarray, bool

Has the same shape as :None:None:`element`. The values :None:None:`element[isin]` are in :None:None:`test_elements`.

Calculates :None:None:`element in test_elements`, broadcasting over :None:None:`element` only. Returns a boolean array of the same shape as :None:None:`element` that is True where an element of :None:None:`element` is in :None:None:`test_elements` and False otherwise.

See Also

in1d

Flattened version of this function.

numpy.lib.arraysetops

Module with a number of other functions for performing set operations on arrays.

Examples

>>> element = 2*np.arange(4).reshape((2, 2))
... element array([[0, 2], [4, 6]])
>>> test_elements = [1, 2, 4, 8]
... mask = np.isin(element, test_elements)
... mask array([[False, True], [ True, False]])
>>> element[mask]
array([2, 4])

The indices of the matched values can be obtained with nonzero :

>>> np.nonzero(mask)
(array([0, 1]), array([1, 0]))

The test can also be inverted:

>>> mask = np.isin(element, test_elements, invert=True)
... mask array([[ True, False], [False, True]])
>>> element[mask]
array([0, 6])

Because of how array handles sets, the following does not work as expected:

>>> test_set = {1, 2, 4, 8}
... np.isin(element, test_set) array([[False, False], [False, False]])

Casting the set to a list gives the expected result:

>>> np.isin(element, list(test_set))
array([[False,  True],
       [ True, False]])
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

numpy.isin numpy.in1d numpy.ma.extras.in1d numpy.ma.extras.isin

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /numpy/lib/arraysetops.py#640
type: <class 'function'>
Commit: