To retrieve only the values that are common to both arrays, use the intersect1d() method.
Calculate the intersection of the following two set arrays.
import numpy as np arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([3, 4, 5, 6]) newarr = np.intersect1d(arr1, arr2, assume_unique=True) print(newarr) |
Note: The intersect1d() method includes an optional argument called assume_unique, which, when set to True, can enhance computation speed. It is recommended to always set this to True when working with sets. |