What is the numpy.intersect1d() function in Python?
Overview
NumPy is a popular library for working with arrays. NumPy’s intersect1d() function returns the intersection between two arrays. In other words, it returns the common elements for two given arrays.
Syntax
numpy.intersect1d(ar1, ar2, assume_unique=False, return_indices=False)
Parameters
This function accepts the following parameter values:
ar1andar2: These two required parameters represent the input arrays for whichintersect1d()will return the intersection.
Note:
intersect1d()accepts any array-like objects; this includes NumPy arrays andscalars.
Note: If any input array is not one-dimensional, the function will flatten them and convert them to a single dimensional array.
assume_unique: An optional parameter, passed asTrueif both input arrays are assumed to be unique andFalseotherwise. If both input arrays are unique, passingassume_uniqueasTruecan speed up calculation.
Note: If the input arrays are not unique and the user passesassume_uniqueasTrue, the function could return an incorrect result or an out-of-bound exception.
return_indices: An optional parameter, which determines ifintersect1d()will return two extra arrays containing indices of the elements of the intersection array in the two input arrays.
Return value
- The function always returns an array that includes the intersection elements found in both the input arrays; this is the intersection array from earlier.
- The function optionally returns two additional arrays, which contain the indices of intersection elements in the input arrays. Each of these two optionally returned arrays represents one input array.
Note: The optional arrays are only returned when thereturn_indicesinput argument has been set toTrue.
Example
import numpy as np# creating the input arraysa = np.array([1,3,5,7,9])b = np.array([2,4,6,8])# finding the intersect of the two arraysprint(np.intersect1d(a, b))# creating the input arraysc = np.array([[1,2,3], [4,5,6]])d = np.array([[1,2,3], [4,5,6]])# finding the intersect of the two arraysprint(np.intersect1d(c, d))# creating the input arrayse = np.array([[1,2,3], [7,8,9]])f = np.array([[1,2,3], [4,5,6]])# finding the intersect of the two arraysprint(np.intersect1d(e, f, return_indices = True))
Explanation
- Line 1: We import
numpyasnp. - Lines 3–4: We create two input arrays,
aandb. - Line 7: We use
intersect1d()to find the intersection ofaandb, and print the results. - Lines 10–11: We create two input arrays,
candd. These are two 2D arrays. - Line 14: We use
intersect1d()to find the intersection ofcanddand print the results. Theintersect1d()function returns a 1D array even though we input two 2D arrays. - Lines 17–18: We create two input arrays,
eandf. - Line 21: We use
intersect1d()to find the intersection ofeandf, and print the results. Thereturn_indicesargument inintersect1d()has been set toTrue. As a result,intersect1d()returns two extra arrays, which contain indices of the intersection elements in the two input arrays.eandfboth contain the intersection elements1,2, and3at indices0,1, and2.