NumPy - Difference



Difference in NumPy

In 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.

What is Set Difference?

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].

Syntax

Following is the basic syntax for the setdiff1d() function in NumPy −

numpy.setdiff1d(ar1, ar2)

Where,

  • ar1: The first input array. It is the array from which we want to subtract elements.
  • ar2: The second input array. It contains elements that will be removed from the first array.

The result is a sorted array containing the unique values that are in ar1 but not in ar2.

Example

In the following example, we are calculating the difference between two arrays using the setdiff1d() function in NumPy −

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)

Following is the output obtained −

Difference between array1 and array2: [1 2]

Handling Arrays with Duplicate Elements

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.

Example

Here, we are removing the duplicates in array1 before computing the difference, resulting in the final output containing only the unique elements −

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)

The result produced is as follows −

Difference with duplicates removed: [1 2]

Handling Arrays with Different Data Types

NumPys 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.

Example

Let us take a look at an example where we calculate the difference between an integer array and a float array −

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)

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

Difference with different data types: [1. 2. 3.]

Difference with Multiple Arrays

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.

Example

Following is an example that demonstrates how to calculate the difference between multiple arrays −

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)

The output obtained is as shown below −

Difference of multiple arrays: [1 2]

Performance Considerations

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.

Example

By setting theassume_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 −

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)

The result produced is as follows −

Difference with unique elements: [1 2]
Advertisements