NumPy - Union of Arrays



Union of Arrays in NumPy

The union of arrays in NumPy refers to combining multiple arrays into a single array while removing duplicate elements. This ensures that each element appears only once in the array. In NumPy, we can achieve this using the union1d() function.

The union of arrays in NumPy is similar to the union operation in set theory where all unique elements from multiple sets are combined into one set.

Using union1d() Function

The np.union1d() function in NumPy is used to compute the union of two arrays. This function returns a sorted array of unique values that are present in either of the input arrays. Following is the syntax −

numpy.union1d(arr1, arr2)

Where,

  • arr1 − It is the first input array. This array can be of any shape or data type, but typically is a 1D array for simplicity.
  • arr2 − It is the second input array. It should have the same data type as "arr1".

Example

In the following example, we are using the union1d() function to find the union of 2 arrays "arr1" and "arr2" −

import numpy as np

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

# Compute the union of the two arrays
union_result = np.union1d(arr1, arr2)
print("Union of two arrays:", union_result)

Following is the output obtained −

Union of two arrays: [1 2 3 4 5 6]

Union of Multiple Arrays

To find the union of more than two arrays, you can use union1d() function multiple times. This involves applying the function iteratively to pairs of arrays until all arrays have been included in the union.

Alternatively, you can use functions like np.concatenate() along with np.unique(). Following is the syntax −

numpy.concatenate((array1, array2, ...))
numpy.unique(array)

Example: Sequential Union

In the example below, we are finding the union of 3 arrays iteratively using the union1d() function −

import numpy as np

# Define multiple 1D arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([2, 3, 4])
arr3 = np.array([4, 5, 6])

# Compute the union of three arrays
# Union of first two arrays
union_temp = np.union1d(arr1, arr2)  
# Union with the third array
union_result = np.union1d(union_temp, arr3) 

print("Union of multiple arrays (sequential):", union_result)

This will produce the following result −

Union of multiple arrays (sequential): [1 2 3 4 5 6]

Example: Using np.concatenate() and np.unique() Functions

In this example, we first concatenate all arrays into a single array. Then, we extract the unique elements from this concatenated array using the unique() function −

import numpy as np

# Define multiple 1D arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([2, 3, 4])
arr3 = np.array([4, 5, 6])

# Concatenate all arrays into one
concatenated_array = np.concatenate((arr1, arr2, arr3))

# Find unique elements
union_result = np.unique(concatenated_array)

print("Union of multiple arrays (concatenate and unique):", union_result)

Following is the output of the above code −

Union of multiple arrays (concatenate and unique): [1 2 3 4 5 6]

Handling Multi-dimensional Arrays

We can also apply union operations to multi-dimensional arrays. To perform union operations on multi-dimensional arrays, you need to flatten the arrays first.

Flattening transforms a multi-dimensional array into a 1D array, allowing you to perform union operations as if the arrays were one-dimensional. After performing the union operation, you can reshape the result back into the original dimensions if needed.

In NumPy, multi-dimensional arrays are arrays with more than one dimension. They are commonly referred to as "ndarrays" and are used to represent complex data structures like matrices or higher-dimensional tensors.

Managing these arrays involves understanding their structure, performing operations, and efficiently manipulating data.

Example

In this example, we first define two 2D arrays "arr1" and "arr2". We then flatten them into 1d arrays using the flatten() function and then compute their union −

import numpy as np

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

# Flatten the arrays and compute the union
flattened_arr1 = arr1.flatten()
flattened_arr2 = arr2.flatten()
union_result = np.union1d(flattened_arr1, flattened_arr2)

print("Union of 2D arrays:", union_result)

The output obtained is as shown below −

Union of 2D arrays: [1 2 3 4 5 6]

Union with Complex Data Types

Union operations with complex data types in NumPy involve working with arrays that have structured or object-oriented data. Unlike simple numeric arrays, complex data types can include fields such as integers, floats, strings, and even other arrays.

The np.union1d() function can handle arrays of any data type as long as they are comparable.

Example

In the following example, we first create 2 structured arrays and then combine them using the union1d() function, removing duplicates and keeping the unique entries −

import numpy as np

# Define structured arrays
arr1 = np.array([(1, 'a'), (2, 'b')], dtype=[('num', 'i4'), ('letter', 'S1')])
arr2 = np.array([(2, 'b'), (3, 'c')], dtype=[('num', 'i4'), ('letter', 'S1')])

# Compute the union of structured arrays
union_result = np.union1d(arr1, arr2)
print("Union of structured arrays:", union_result)

After executing the above code, we get the following output −

Union of structured arrays: [(1, b'a') (2, b'b') (3, b'c')]
Advertisements