Set Operations In Numpy Ufunc

In this article, we will explore Numpy Ufunc Set Operations and their applications. You can utilize NumPy set operations to perform different tasks on arrays and sets of arrays.



Set Operations – What are they?

Set operations are mathematical operations that involve sets, which are collections of distinct objects.

There are several set operations that are commonly used, including union, intersection, difference, and symmetric difference.

These operations are useful in various fields, such as computer science, mathematics, and data analysis.


NumPy Ufunc Set Operations

NumPy provides several ufuncs to perform set operations on arrays.

These ufuncs are implemented in C for faster performance and provide an easy-to-use interface for performing set operations on arrays. Let’s explore each of these ufuncs in detail.

According to set operations, NumPy’s unique() method can be applied to identify unique items.

As an example, construct a set array, but keep in mind that the array must only be 1-dimensional.

Modify the duplicate item array to the distinct array:

Example: 

import numpy as npy duplicate_arr = npy.array([7, 12, 67, 67, 17, 8, 12, 8, 7]) distinct_arr = npy.unique(duplicate_arr) print(distinct_arr)

Apply the unique() method to the following palindrome array:

Example: 

import numpy as npy palindrome_arr = npy.array(["101", "25452", "25452", "363", "101", "64146", "363","64146"]) distinct_arr = npy.unique(palindrome_arr) print(distinct_arr)

np.union1d()

The np.union1d() ufunc computes the union of two input arrays, which is the set of elements that are present in either of the input arrays.

The output of this ufunc is a sorted, one-dimensional array containing all the unique elements from both input arrays.Implement the union1d() method in set operations to identify the distinctive data in two arrays.

Combine the distinct values of two set arrays by utilizing the union1d() method:

Example: 

import numpy as npy prime_arr1 = npy.array([2, 3, 5, 7, 11]) prime_arr2 = npy.array([3, 7, 13, 17, 2 , 19]) mrx_arr = npy.union1d(prime_arr1, prime_arr2) print(mrx_arr)

By considering the following two set arrays, identify the union:

Example: 

import numpy as npy angle_arr1 = npy.array([30, 45, 90, 180, 45, 60, 90]) angle_arr2 = npy.array([120, 180, 220, 290, 90 , 360]) mrx_arr = npy.union1d(angle_arr1, angle_arr2) print(mrx_arr)

Find Intersection

The np.intersect1d() ufunc computes the intersection of two input arrays, which is the set of elements that are present in both input arrays.

The output of this ufunc is a sorted, one-dimensional array containing all the common elements from both input arrays.

You can invoke intersect1d() according to set operations to retrieve only the values included in both arrays.

Check the intersection of the below two set arrays and apply the optional argument assume_unique:

Example: 

import numpy as npy prime_arr1 = npy.array([2, 3, 5, 7, 11]) prime_arr2 = npy.array([3, 7, 13, 17, 2 , 19]) mrx_arr = npy.intersect1d(prime_arr1, prime_arr2, assume_unique = True) print(mrx_arr)
Reminder: It is important to note that the intersect1d() method accepts an optional argument assume_unique, which, if it is set to True, will increase the speed of the operation. If you are working with sets, you must always assign them to True.

Implement the intersect1d() method in the following example:

Example: 

import numpy as npy angle_arr1 = npy.array([30, 45, 90, 180, 45, 60, 90]) angle_arr2 = npy.array([120, 180, 220, 290, 90 , 360]) mrx_arr = npy.intersect1d(angle_arr1, angle_arr2) print(mrx_arr)

Find Difference – setdiff1d()

The np.setdiff1d() ufunc computes the difference between two input arrays, which is the set of elements that are present in the first input array but not in the second input array.

The output of this ufunc is a sorted, one-dimensional array containing all the unique elements from the first input array that are not present in the second input array.

Apply the setdiff1d() method to retrieve only the values in the initial set that are not found in the second set.

Initialize the two sets array, then implement the setdiff1d() method to display the difference:

Example: 

import numpy as npy square_set1 = npy.array([1, 4, 9, 16, 25, 36]) square_set2 = npy.array([49, 9, 64, 1, 82, 16]) diff_arr = npy.setdiff1d(square_set1, square_set2, assume_unique=True) print(diff_arr)
Reminder: It is important to note that the setdiff1d() method has an optional argument assume_unique, which, if assigned to True, can increase the speed of processing considerably. If you are working with sets, you must always assign this value to True.

In the following example execute the setdiff1d() method with optional argument assume_unique:

Example: 

import numpy as npy nonprime_set1 = npy.array([4, 6, 8, 9, 10, 12]) nonprime_set2 = npy.array([14, 15, 4, 16, 12, 8]) diff_arr = npy.setdiff1d(nonprime_set1, nonprime_set2, assume_unique=True) print(diff_arr)

Find Symmetric Difference – setxor1d()

The np.setxor1d() ufunc computes the symmetric difference between two input arrays, which is the set of elements that are present in either of the input arrays but not in both input arrays.

The output of this ufunc is a sorted, one-dimensional array containing all the unique elements that are present in either of the input arrays but not in both input arrays.

You can utilize the setxor1d() method to retrieve values that don’t appear in BOTH sets.

Calculate the symmetric difference between even_set1 and even_set2:

Example: 

import numpy as npy even_set1 = npy.array([0, 2, 4, 6, 8, 10]) even_set2 = npy.array([12, 4, 0, 14, 6, 16]) mrx_arr = npy.setxor1d(even_set1, even_set2, assume_unique=True) print(mrx_arr)
Reminder: There is an optional argument assume_unique that can be passed to the setxor1d() method, which, if assigned to True, will accelerate the computation. The value of this property must always be assigned to True when working with sets.

Invoke the setxor1d() method in the below example with assume_unique = True:

Example: 

import numpy as npy odd_set1 = npy.array([1, 3, 5, 7, 9, 11]) odd_set2 = npy.array([13, 9, 15, 3, 17, 19]) mrx_arr = npy.setxor1d(odd_set1, odd_set2, assume_unique=True) print(mrx_arr)

Example Explanation

In this example, we have two NumPy arrays named odd_set1 and odd_set2 that represent two sets of odd numbers. The odd_set1 contains the elements [1, 3, 5, 7, 9, 11] and the odd_set2 contains the elements [13, 9, 15, 3, 17, 19].

We then call the setxor1d() function on the two sets and store the result in the mrx_arr variable.

The setxor1d() function returns the exclusive values between the two sets, which means it returns the values that are present only in one of the sets and not in both. In this case, the function returns [1, 5, 7, 11, 13, 15, 17, 19], which are the values that are not present in both sets.

The assume_unique parameter is set to True, which indicates that the input arrays are already unique. If this parameter is not set to True, the function will first perform a unique sort on the input arrays, which can affect the performance of the function for large arrays.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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