Set Operations – NumPy uFuncs (Python Tutorial)

Set Operations NumPy UFuncs (Python Tutorial)

This is a detailed tutorial of the NumPy Set Operations Universal Functions. Learn the usage of these functions with the help of examples.

Set Operations

A set is a collection of unique values on which we can perform various operations. The operation which we usually perform on sets is union, intersection and difference operations. These operations help us in data manipulation and then this data we can use anywhere.

Creating Sets

In NumPy, we have a method with the help of which we can create a set that will help us in finding unique values. This method is unique()  which will help us in finding unique values. When we are creating the set array, it should always be in one dimension only.

Let us take an example to understand it better:

#first we will import the numpy package 
import numpy as np
#now in the next step we will take an array of values
a=np.array([1,2,3,1,1,2,3,3,4,5,5,4,1,2,3])
#now we will write the fuunction to find the unique values
b=np.unique(a)
#now we will print the result
print(b)

Output.

[1 2 3 4 5]

So here in this example, we see with the help of this function we get al the unique value in the array.

Finding Union

In order to find a union, we use the union1d() method. With the help of this method, we will be able to combine all the values of the two arrays we have given which are unique.

Let us take an example:

#first we will import the numpy package 
import numpy as np
#now in the next step we will take arrays with values
a=np.array([9,8,7,6,6])
b=np.array([6,5,4,3])
#now we will write the function 
c=np.union1d(a,b)
#now we will print the result
print(c)

Output.

[3 4 5 6 7 8 9]

Finding Intersection

When we want to find only common values that present in both the array, we use the intersection method. In order to find the intersection between the two arrays we use intersect1d()method.

Let us take an example:

#first we will import the numpy package 
import numpy as np
#now in the next step we will take arrays with values
a=np.array([9,3,7,6,6])
b=np.array([6,5,4,3])
#now we will write the function 
c=np.intersect1d(a,b,assume_unique=True)
#now we will print the result
print(c)

Output.

[3 6 6]

Here we use assume_unique to be true because it helps in speeding up the computation, and it should always be true.

Finding Difference

In order to find the difference between the two sets, we use setdiff1d() the method. In this, we will get value in the first set that is not present in the second set.

Let us take an example:

#first we will import the numpy package 
import numpy as np
#now in the next step we will take arrays with values
a=np.array([9,3,7,6,6])
b=np.array([6,5,4,3])
#now we will write the function 
c=np.setdiff1d(a,b,assume_unique=True)
#now we will print the result
print(c)

Output.

[9 7]

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *