Find the union of two NumPy arrays


NumPy is a popular Python library that provides support for numerical computations. It is widely used for array and matrix operations in scientific computing, data analysis, and machine learning. One of the most common tasks in NumPy is finding the union of two arrays. A new array that contains all the distinct elements from both arrays is created when two arrays are joined. In this article, we will explore different ways to find the union of two NumPy arrays.

Installation and Syntax

NumPy is usually installed with Anaconda or Miniconda distribution. If you don't have it installed, you can install it using pip. The following command will install the latest version of NumPy −

pip install numpy

The NumPy library provides a function called union1d() to find the union of two arrays.

numpy.union1d(ar1, ar2)

Algorithm

To find the union of two NumPy arrays using the union1d() function, we first import the NumPy library. We then create two arrays arr1 and arr2. Finally, we use the union1d() function to find the union of these two arrays.

Example

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([2, 3, 4])

union = np.union1d(arr1, arr2)
print(union)

Output

 [1 2 3 4]

Construct two arrays, arr1 and arr2, and add some shared components to them and proceed to figure out the union of these two arrays - determined by the union1d() method. The result is a new array with all the distinct components from both arrays.

Example 2

import numpy as np

arr1= np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

union = np.union1d(arr1, arr2)
print(union)

Output

[1 2 3 4 5 6]

Create two independent arrays with no common members, a1 and a2, and then use the same technique as the example before to determine the union of these two arrays. The result will output the total of both collections because there is no overlap.

Example 3

import numpy as np

arr1 = np.array([])
arr2 = np.array([1, 2, 3])

union = np.union1d(arr1, arr2)
print(union)

Output

[1. 2. 3.]

Create an empty array arr1 and an array arr2 with some elements. We then use union1d() function to find the union of these two arrays. The output is a new array that contains all the unique elements from both arrays.

Example 4

import numpy as np

# create two arrays with some common elements
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([4, 5, 6, 7, 8])

# find the union of the two arrays
union = np.union1d(arr1, arr2)

# print the union array
print(union)

Output

[1 2 3 4 5 6 7 8]

We first import the NumPy library using import numpy as np. We then create two arrays arr1 and arr2 with some common elements using np.array(). We use the np.union1d() function to find the union of these two arrays and assign the result to a variable called union. Finally, we print the union array using print() function.

Applications

  • Many data science and machine learning problems use the common function of locating the union of two arrays.

  • For instance, it is sometimes important to integrate several datasets into a single dataset while working with datasets. Finding the union of arrays might be helpful in these circumstances.

  • It is also possible to eliminate duplicates and guarantee that each element only appears once in the merged dataset by using the union of two arrays.

  • An increase in accuracy and efficiency may be advantageous for machine learning algorithms that depend on accurate and trustworthy data.

Conclusion

In this article, we have discussed different ways to find the union of two NumPy arrays using the union1d() function. The union1d() function takes two arrays as input and returns a new array that contains all the unique elements from both arrays. We have also provided some examples to illustrate the usage of the union1d() function. The knowledge of finding the union of two arrays is important in many data science and machine learning tasks.

Updated on: 21-Aug-2023

170 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements