NumPy Set Operations – A Detailed Guide!

Filed Under: NumPy
NumPy Set Operations (1)

Hello, readers! In this article, we will be focusing on NumPy Set Operations in detail.

So, let us begin!! 馃檪


Need of NumPy Set operations

Python NumPy module is the base for most of the popular libraries such as Pandas, Scikit-learn, etc. The reason being its power to add value to the mathematical computation of data in terms of multiple dimensions.

NumPy module offers us with the capability to create single or multi dimensional arrays, treat them like a matrix, perform operations on the rows and the columns, etc.

With Set operations, NumPy module gives us the capability to perform the basic set related operations such as Union, intersection, extracting unique elements for use.

In context to the current topic, we will be having a look at the below Set operations offered by NumPy–

  • Union
  • Intersection
  • Symmetric difference
  • Fetch unique values

With these operations, it helps us to get manipulated data for processing further.

Let us have a look at each one of them in detail in the upcoming section.


1. NumPy Set union operation

The Union operation merges the values from all the arrays and represents the same in a single array. In the NumPy module, we can perform Union operation using union1d function.

In case the array contains duplicate values or has multiple occurrences of an element, then the union1d() function includes only a single occurrence of that element and excludes the other copies.

Syntax–

numpy.union1d(array,array)

Example–

In the below example, we have created two arrays using numpy.array() function. Further, we have made use of union1d() function to perform the UNION operation.

As clearly seen below, the union1d() function neglects the duplicate elements and considers only a single occurrence of them in the output.

import numpy as np
array1 = np.array([30,60,90])
array2 = np.array([1,2,3,30])
 
res = np.union1d(array1,array2)
 
print(res)

Output–

As seen below, it neglects the multiple occurrences of elements and represents only a single occurrence of the value 30.

[1 2 3 30 60 90]

2. Set Intersection operation

With Intersection operation, we can select and represent the common elements from the arrays passed as parameters.

In NumPy, we can make use of intersect1d() function to extract and represent the common elements from the arrays.

Syntax–

numpy.intersect1d(array,array,assume_unique)

With assume_unique parameter, based on the below conditions it would take the decision regarding the duplicate values::

  • If set to TRUE — the intersect1d() function includes the duplicate values as a part of the output.
  • If set to FALSE — it does not include the duplicate values as the part of the output.

Example–

import numpy as np
array1 = np.array([30,60,90])
array2 = np.array([1,2,3,30])
 
res = np.intersect1d(array1,array2, assume_unique=True)
 
print(res)

Output–

[30 30]

3. Symmetric Differences

NumPy provides us with setxor1d() function to perform symmetric differences between the arrays. Symmetric differences means it selects all the uncommon elements from arrays. With setox1d() function, it basically extracts all the non common elements from the passed arrays and provides the distinct/unique elements as output.

Syntax–

np.setxor1d(arr1, arr2, assume_unique)

Example–

import numpy as np
array1 = np.array([30,60,90])
array2 = np.array([1,2,3,30])
 
res = np.setxor1d(array1,array2, assume_unique=True)
 
print(res)

Output–

[1 2 3 60 90]

4. Pick Unique values from NumPy Array

NumPy provides us with numpy.unique() function to fetch and represent the unique elements from a single array. With it, the function skips all the occurrence of duplications and represents only a single occurrence of an unique entity.

Syntax–

numpy.unique(array)

Example–

import numpy as np
array1 = np.array([30,60,90,30])
res = np.unique(array1)
print(res)

Output–

[60 90 30]

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to Python programming, Stay tuned with us!

Till then, Happy Learning!! 馃檪

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content