NumPy Set Operations

A set is a collection of unique data. That is, elements of a set cannot be repeated.

NumPy set operations perform mathematical set operations on arrays like union, intersection, difference, and symmetric difference.


Set Union Operation in NumPy

The union of two sets A and B include all the elements of set A and B.

Set Union in NumPy
Set Union in NumPy

In NumPy, we use the np.union1d() function to perform the set union operation in an array. For example,

import numpy as np

A = np.array([1, 3, 5])
B = np.array([0, 2, 3])

# union of two arrays
result = np.union1d(A, B)

print(result)  

# Output: [0 1 2 3 5]

In this example, we have used the np.union1d(A, B) function to compute the union of two arrays: A and B.

Here, the function returns unique elements from both arrays.

Note: np.union1d(A,B) is equivalent to A ⋃ B set operation.


Set Intersection Operation in NumPy

The intersection of two sets A and B include the common elements between set A and B.

Set Intersection in NumPy
Set Intersection in NumPy

We use the np.intersect1d() function to perform the set intersection operation in an array. For example,

import numpy as np

A = np.array([1, 3, 5])
B = np.array([0, 2, 3])

# intersection of two arrays
result = np.intersect1d(A, B)

print(result)  

# Output: [3]

Note: np.intersect1d(A,B) is equivalent to A ⋂ B set operation.


Set Difference Operation in NumPy

The difference between two sets A and B include elements of set A that are not present on set B.

Set Difference in NumPy
Set Difference in NumPy

We use the np.setdiff1d() function to perform the difference between two arrays. For example,

import numpy as np

A = np.array([1, 3, 5])
B = np.array([0, 2, 3])

# difference of two arrays
result = np.setdiff1d(A, B)

print(result)  

# Output: [1 5]

Note: np.setdiff1d(A,B) is equivalent to A - B set operation.


Set Symmetric Difference Operation in NumPy

The symmetric difference between two sets A and B includes all elements of A and B without the common elements.

Set Symmetric Difference in NumPy
Set Symmetric Difference in NumPy

In NumPy, we use the np.setxor1d() function to perform symmetric differences between two arrays. For example,

import numpy as np

A = np.array([1, 3, 5])
B = np.array([0, 2, 3])

# symmetric difference of two arrays
result = np.setxor1d(A, B)

print(result)  

# Output: [0 1 2 5]

Unique Values From a NumPy Array

To select the unique elements from a NumPy array, we use the np.unique() function. It returns the sorted unique elements of an array. It can also be used to create a set out of an array.

Let's see an example.

import numpy as np

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

# unique values from array1
result = np.unique(array1)

print(result)  

# Output: [1 2 3 4 5 7]

Here, the resulting array [1 2 3 4 5 7] contains only the unique elements of the original array array1.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges