Find the union of two NumPy arrays
To find union of two 1-dimensional arrays we can use function numpy.union1d() of Python Numpy library. It returns unique, sorted array with values that are in either of the two input arrays.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Note The arrays given in input are flattened if they are not 1-dimensional.
Let’s see examples of how to find union of two arrays.
Let’s see example of finding union of a 2-d and a 1-d array. As discussed earlier, if array passed as arguments to function numpy.union1d is 2-dimensional, then they are flattened to 1-dimension.
If we want to find union of more than two arrays, then we can find that by using functools.reduce function.