NumPy - Set Operations ufunc



Set Operations Universal Functions (ufunc)

NumPy provides several universal functions (ufuncs) that perform set operations on arrays. These operations are used to compare and manipulate sets of elements within arrays. The main set operations include union, intersection, difference, and exclusive-or.

NumPy Union Function

The numpy.union1d() function is used to find the union of two arrays. The union of two sets is a set containing all elements from both sets, without duplicates.

Example

In the following example, we use the numpy.union1d() function to find the union of two arrays −

import numpy as np

# Define two arrays
array1 = np.array([1, 2, 3])
array2 = np.array([3, 4, 5])

# Find the union of the arrays
union_result = np.union1d(array1, array2)

print("Union of arrays:", union_result)

The output obtained is as follows −

Union of arrays: [1 2 3 4 5]

NumPy Intersection Function

The numpy.intersect1d() function is used to find the intersection of two arrays. The intersection of two sets is a set containing only the elements that are present in both sets.

Example

In the following example, we use the numpy.intersect1d() function to find the intersection of two arrays −

import numpy as np

# Define two arrays
array1 = np.array([1, 2, 3])
array2 = np.array([3, 4, 5])

# Find the intersection of the arrays
intersection_result = np.intersect1d(array1, array2)

print("Intersection of arrays:", intersection_result)

The output produced is as follows −

Intersection of arrays: [3]

NumPy Set Difference Function

The numpy.setdiff1d() function is used to find the set difference of two arrays. The set difference of two sets is a set containing elements that are present in the first set but not in the second set.

Example

In this example, we use the numpy.setdiff1d() function to find the difference between two arrays −

import numpy as np

# Define two arrays
array1 = np.array([1, 2, 3])
array2 = np.array([3, 4, 5])

# Find the set difference of the arrays
difference_result = np.setdiff1d(array1, array2)

print("Difference of arrays:", difference_result)

We get the following result −

Difference of arrays: [1 2]

NumPy Set Exclusive-or Function

The numpy.setxor1d() function is used to find the set exclusive-or (symmetric difference) of two arrays. The set exclusive-or of two sets is a set containing elements that are present in either of the sets but not in both.

Example

In this example, we use the numpy.setxor1d() function to find the exclusive-or of two arrays −

import numpy as np

# Define two arrays
array1 = np.array([1, 2, 3])
array2 = np.array([3, 4, 5])

# Find the set exclusive-or of the arrays
xor_result = np.setxor1d(array1, array2)

print("Exclusive-or of arrays:", xor_result)

The output produced is as follows −

Exclusive-or of arrays: [1 2 4 5]

The NumPy in1d() Function

The numpy.in1d() function tests whether each element of an array is also present in a second array. It returns a boolean array of the same shape as the first array, indicating whether each element is present in the second array.

Example

In the example below, we use the numpy.in1d() function to test whether elements of one array are present in another array −

import numpy as np

# Define two arrays
array1 = np.array([1, 2, 3])
array2 = np.array([3, 4, 5])

# Test whether elements of array1 are in array2
in1d_result = np.in1d(array1, array2)

print("Elements of array1 in array2:", in1d_result)

The output obtained is as follows −

Elements of array1 in array2: [False False  True]

The NumPy isin() Function

The numpy.isin() function tests whether each element of an array is present in a list of values or another array. It is similar to numpy.in1d() function but can be used with multi-dimensional arrays.

Example

In this example, we use the numpy.isin() function to test whether elements of an array are present in another array −

import numpy as np

# Define an array and a list of values
array = np.array([1, 2, 3, 4])
values = [2, 4, 6]

# Test whether elements of the array are in the list of values
isin_result = np.isin(array, values)

print("Elements of array in values:", isin_result)

The result produced is as follows −

Elements of array in values: [False  True False  True]
Advertisements