NumPy Union1d

numpy
Published

May 5, 2023

Understanding numpy.union1d

The numpy.union1d function computes the union of two arrays, returning a new array containing all unique elements from both input arrays. The order of elements in the output array is not guaranteed to be the same as the order in the input arrays. It’s crucial to remember that this function works effectively with one-dimensional arrays only. Attempting to use it with multi-dimensional arrays will result in an error.

import numpy as np

array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([3, 5, 6, 7, 8])

union_array = np.union1d(array1, array2)
print(union_array)  # Output: [1 2 3 4 5 6 7 8]

This simple example clearly demonstrates how union1d merges the unique elements from array1 and array2, eliminating duplicates.

Handling Different Data Types

numpy.union1d gracefully handles various data types, including integers, floats, and strings. However, it’s important to ensure that both input arrays have compatible data types to avoid errors. Mixing incompatible types might lead to unexpected results or errors.

array3 = np.array([1.1, 2.2, 3.3])
array4 = np.array([3.3, 4.4, 5.5])

union_array = np.union1d(array3, array4)
print(union_array) # Output: [1.1 2.2 3.3 4.4 5.5]

array5 = np.array(['apple', 'banana', 'cherry'])
array6 = np.array(['banana', 'date', 'fig'])

union_array = np.union1d(array5, array6)
print(union_array) # Output: ['apple' 'banana' 'cherry' 'date' 'fig']

More Complex Scenarios

Let’s explore a more complex scenario involving arrays with repeated elements. union1d efficiently handles these situations, ensuring that only unique values are included in the output.

array7 = np.array([1, 2, 2, 3, 3, 3, 4])
array8 = np.array([3, 4, 4, 5, 6])

union_array = np.union1d(array7, array8)
print(union_array) # Output: [1 2 3 4 5 6]

As shown, union1d correctly identifies and includes only one instance of each unique element, regardless of how many times it appears in the input arrays.

Using union1d with More Than Two Arrays

While the examples above focused on two arrays, union1d can handle more than two input arrays. The function can accept multiple arrays as arguments, effectively computing the union of all provided arrays.

array9 = np.array([1, 2, 3])
array10 = np.array([3, 4, 5])
array11 = np.array([5, 6, 7])

union_array = np.union1d(array9, array10, array11)
print(union_array) # Output: [1 2 3 4 5 6 7]

This demonstrates the versatility of union1d in handling multiple arrays simultaneously, making it a powerful tool for various data manipulation tasks.