Table of Contents
ToggleIn NumPy, the term “union” refers to the operation that combines the elements of two or more arrays, removing any duplicate values.
It is commonly used when you want to merge multiple datasets or arrays, ensuring that each element appears only once in the final result.
NumPy provides the numpy.union1d() function to easily find the union of two 1-dimensional arrays.
The union of two or more arrays refers to the combined set of unique elements from all the input arrays.
This means that no duplicate elements are present in the result. The union operation is closely related to the concept of sets in mathematics.
For example, if you have two arrays containing some common and some unique elements, the union will contain all the unique elements from both arrays.
In NumPy, the numpy.union1d() function is used to compute the union of two 1-dimensional arrays. This function ensures that no elements are repeated in the result, even if the same element appears in both input arrays.
Following is the basic syntax of the NumPy union1d() function −
numpy.union1d(ar1, ar2)
Where, ar1 and ar2 are the two input arrays whose union is to be found. The arrays can contain any data type, and they may or may not have overlapping elements.
In the following example, we are calculating the union of two arrays using the union1d() function in NumPy −
# Open Compiler
import numpy as np
# Define two arrays
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([4, 5, 6, 7, 8])
# Find union of the two arrays
union = np.union1d(array1, array2)
print("Union of array1 and array2:", union)
Output:
As seen in the output, the union of array1 and array2 contains all unique elements from both arrays, without any repetition. The numbers 4 and 5, which appeared in both arrays, appear only once in the final result −
Union of array1 and array2: [1 2 3 4 5 6 7 8]
NumPy’s union1d() function can also handle arrays of different data types, such as integers, floats, and even strings. The function will convert all elements to a common type before computing the union.
As shown in the example below, NumPy has automatically converted all elements to floats because the first array contains a floating-point number, and the union contains no duplicates −
# Open Compiler
import numpy as np
# Define arrays with different data types
array1 = np.array([1, 2, 3, 4.5])
array2 = np.array([4.5, 5, 6, 7])
# Find union of the arrays
union = np.union1d(array1, array2)
print("Union of array1 and array2 with different types:", union)
Output:
The result produced is as follows −
Union of array1 and array2 with different types: [1. 2. 3. 4.5 5. 6. 7.]
The numpy.union1d() function works with two arrays at a time. However, if you need to find the union of more than two arrays, you can use loops or the reduce() function from the functools module.
Below is an example that demonstrates how to compute the union of three arrays −
# Open Compiler
import numpy as np
from functools import reduce
# Define multiple arrays
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([4, 5, 6, 7, 8])
array3 = np.array([7, 8, 9, 10])
# Find the union of all arrays
union = reduce(np.union1d, [array1, array2, array3])
print("Union of multiple arrays:", union)
Output:
As shown in the output, the union operation combines all the unique elements from the three arrays. There are no duplicates, and the union contains all the unique values across the arrays −
Union of multiple arrays: [1 2 3 4 5 6 7 8 9 10]
When the input arrays contain duplicate elements, numpy.union1d() automatically removes them in the final result. This ensures that the returned union consists of only unique elements.
Following is an example where we find the union of arrays containing duplicates −
# Open Compiler
import numpy as np
# Define arrays with duplicate elements
array1 = np.array([1, 2, 2, 3, 4])
array2 = np.array([3, 4, 4, 5, 6])
# Find union of the arrays
union = np.union1d(array1, array2)
print("Union with duplicates removed:", union)
Output:
The output obtained is as shown below −
Union with duplicates removed: [1 2 3 4 5 6]
In NumPy, you can also perform union operations on arrays containing strings. The function will combine all unique strings from both arrays.
Let us take a look at an example with string arrays −
# Open Compiler
import numpy as np
# Define arrays with strings
array1 = np.array(['apple', 'banana', 'cherry'])
array2 = np.array(['banana', 'cherry', 'date'])
# Find the union of the string arrays
union = np.union1d(array1, array2)
print("Union of string arrays:", union)
Output:
We get the following output −
Union of string arrays: [‘apple’ ‘banana’ ‘cherry’ ‘date’]
The numpy.union1d() function is efficient, but the performance can depend on the size of the input arrays. When you are working with very large arrays, it is a good idea to ensure that the arrays are as efficient as possible.
For example, if the arrays contain only unique elements, you can set the assume_unique parameter to True to speed up the union operation:
By assuming that the arrays contain only unique elements, NumPy can perform the union operation more quickly as shown in the example below −
# Open Compiler
import numpy as np
# Define arrays with unique elements
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([6, 7, 8, 9, 10])
# Find union assuming unique elements
union = np.union1d(array1, array2)
print("Union of unique arrays:", union)
Output:
The result produced is as follows −
Union of unique arrays: [1 2 3 4 5 6 7 8 9 10]
Key Takeaway: Master array union with NumPy at Vista Academy!
