numpy 1.22.4 Pypi GitHub Homepage
Other Docs
ParametersReturnsBackRef
intersect1d(ar1, ar2, assume_unique=False, return_indices=False)

Return the sorted, unique values that are in both of the input arrays.

Parameters

ar1, ar2 : array_like

Input arrays. Will be flattened if not already 1D.

assume_unique : bool

If True, the input arrays are both assumed to be unique, which can speed up the calculation. If True but ar1 or ar2 are not unique, incorrect results and out-of-bounds indices could result. Default is False.

return_indices : bool

If True, the indices which correspond to the intersection of the two arrays are returned. The first instance of a value is used if there are multiple. Default is False.

versionadded

Returns

intersect1d : ndarray

Sorted 1D array of common and unique elements.

comm1 : ndarray

The indices of the first occurrences of the common values in :None:None:`ar1`. Only provided if :None:None:`return_indices` is True.

comm2 : ndarray

The indices of the first occurrences of the common values in :None:None:`ar2`. Only provided if :None:None:`return_indices` is True.

Find the intersection of two arrays.

See Also

numpy.lib.arraysetops

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

Examples

>>> np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1])
array([1, 3])

To intersect more than two arrays, use functools.reduce:

>>> from functools import reduce
... reduce(np.intersect1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2])) array([3])

To return the indices of the values common to the input arrays along with the intersected values:

>>> x = np.array([1, 1, 2, 3, 4])
... y = np.array([2, 1, 4, 6])
... xy, x_ind, y_ind = np.intersect1d(x, y, return_indices=True)
... x_ind, y_ind (array([0, 2, 4]), array([1, 0, 2]))
>>> xy, x[x_ind], y[y_ind]
(array([1, 2, 4]), array([1, 2, 4]), array([1, 2, 4]))
See :

Back References

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

numpy.ma.extras.intersect1d

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#369
type: <class 'function'>
Commit: