Table of Contents
ToggleIn NumPy, the difference operation is used to find elements present in one array but not in another. It is commonly used to compare two arrays and identify the unique elements in one array that do not exist in the other.
In NumPy, the setdiff1d() function is used to perform this operation.
The “set difference” operation refers to finding the elements that are present in one set but not in another. In NumPy, this operation is applied to arrays, and it returns the elements in the first array that are not found in the second array.
This concept is closely related to the mathematical set theory, where the difference of two sets A – B contains elements in set A but not in set B.
For example, given two arrays −
array1 = [1, 2, 3, 4, 5]array2 = [3, 4, 5, 6, 7]The set difference will give us the elements in array1 that are not in array2, which are [1, 2].
Following is the basic syntax for the setdiff1d() function in NumPy −
numpy.setdiff1d(ar1, ar2)
Where,
The result is a sorted array containing the unique values that are in ar1 but not in ar2.
In the following example, we are calculating the difference between two arrays using the setdiff1d() function in NumPy −
# Open Compiler
import numpy as np
# Define two arrays
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([3, 4, 5, 6, 7])
# Find the difference between the two arrays
difference = np.setdiff1d(array1, array2)
print("Difference between array1 and array2:", difference)
Output:
Following is the output obtained −
Difference between array1 and array2: [1 2]
If the input arrays contain duplicate elements, the numpy.setdiff1d() function will remove the duplicates before performing the difference operation. This ensures that the result contains only unique values.
Here, we are removing the duplicates in array1 before computing the difference, resulting in the final output containing only the unique elements −
# 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 the difference between the arrays
difference = np.setdiff1d(array1, array2)
print("Difference with duplicates removed:", difference)
Output:
The result produced is as follows −
Difference with duplicates removed: [1 2]
NumPy’s setdiff1d() function works with arrays of different data types, including integers, floats, and strings.
However, the function will automatically convert the data types to a common type before performing the difference operation.
Let us take a look at an example where we calculate the difference between an integer array and a float array −
# 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])
# Find the difference between the arrays
difference = np.setdiff1d(array1, array2)
print("Difference with different data types:", difference)
Output:
After executing the above code, we get the following output −
Difference with different data types: [1. 2. 3.]
In NumPy, you can only use setdiff1d() function to compute the difference between two arrays at a time.
If you want to compute the difference with multiple arrays, you can use a combination of setdiff1d() function and loops or reduce() function from the functools module.
Following is an example that demonstrates how to calculate the difference between multiple 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([3, 4, 5, 6, 7])
array3 = np.array([5, 6, 7, 8])
# Calculate the difference of all arrays
difference = reduce(lambda x, y: np.setdiff1d(x, y), [array1, array2, array3])
print("Difference of multiple arrays:", difference)
Output:
The output obtained is as shown below −
Difference of multiple arrays: [1 2]
The numpy.setdiff1d() function is quite efficient, but performance can be a consideration when dealing with large arrays.
If your arrays contain only unique elements, you can use the assume_unique parameter to speed up the computation.
By setting the assume_unique parameter to True, NumPy optimizes the operation when dealing with arrays that already contain unique elements, leading to faster performance 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([3, 4, 5, 6, 7])
# Find the difference assuming unique elements
difference = np.setdiff1d(array1, array2, assume_unique=True)
print("Difference with unique elements:", difference)
Output:
The result produced is as follows −
Difference with unique elements: [1 2]
Key Takeaway: Master array difference with NumPy at Vista Academy!
