لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

Python Data Science NumPy ufunc NumPy's Set Operations

What is a Set?

In mathematics set is a collection of unique elements.

Sets are used for operations involving frequent intersection, union and difference operations.

How to Create Sets in NumPy?

We can use NumPy's unique() method to find unique elements from any array.
E.g. creating a set array, but we should remember here that the set arrays should only be 1-D arrays.

Example 1: Convert following array with repeated elements to a set.

Code

import numpy as np

arr = np.array([2, 2, 2, 1, 4, 5, 6, 5, 6, 7])

x = np.unique(arr)

print(x)

the output will be

[1 2 4 5 6 7]

Finding Union

To find the unique values of two arrays, use the union1d() method.

Example 2: Find union of the following two set arrays.

Code

import numpy as np

arr1 = np.array([5, 6, 7, 8])
arr2 = np.array([7, 4, 8, 6])

newarr = np.union1d(arr1, arr2)

print(newarr)

the output will be

[4 5 6 7 8]

Finding Intersection

To find only the values that are present in both arrays, use the intersect1d() method.

Example 3: Find intersection of the following two set arrays.

Code

import numpy as np

arr1 = np.array([5, 6, 7, 8])

arr2 = np.array([7, 4, 8, 6])

newarr = np.intersect1d(arr1, arr2, assume_unique=True)

print(newarr)

the output will be

[6 7 8]

Note: the intersect1d() method takes an optional argument assume_unique.
assume_unique if set to True can speed up computation.
assume_unique should always be set to True when dealing with sets.


Finding Difference

To find only the values in the first set that is NOT present in the seconds set, use the setdiff1d() method.

Example 4: Find the difference of the set1 from set2.

Code

import numpy as np

arr1 = np.array([5, 6, 7, 8])
arr2 = np.array([7, 4, 8, 6])

newarr = np.setdiff1d(arr1, arr2, assume_unique=True)

print(newarr)

the output will be

[5]

Note: the setdiff1d() method takes an optional argument assume_unique.
assume_unique if set to True can speed up computation.
assume_unique should always be set to True when dealing with sets.


Finding Symmetric Difference

To find only the values that are NOT present in BOTH sets, use the setxor1d() method.

Example 5: Find the symmetric difference of the set1 and set2.

Code

import numpy as np

set1 = np.array([5, 6, 7, 8])
set2 = np.array([7, 4, 8, 6])

newarr = np.setxor1d(set1, set2, assume_unique=True)

print(newarr)

the output will be

[4 5]
Note: NumPy ufunc Universal function or ufuncs is a function which operates on ndarrays in an element by element fashion and supports array broadcasting, type casting, and many other standard features.