Introduction to Python Numpy Set

In This Article, You Will Learn About Python Numpy Set.

Python Numpy Set – Before moving ahead, let’s know a bit of Python Numpy Hyperbolic Functions

Table of Contents

Set

Set – It is collection of unordered and unindexed elements.

Sets are used on its method, difference(), intersection(), and isdisjoint() etc.

Create Sets in NumPy

To create a set from unique elements in Numpy, use Numpy’s unique() method.

Note: Use only 1d array.

Example – Create a set from unique elements of an array.

				
					import numpy as np

array = np.array([11, 12, 12, 13, 13, 13, 15])

x = np.unique(array)

print(x)

				
			
Output - 

[11 12 13 15]

As shown above, it returned a set of unique elements.

Finding Union

To create a set from the set of two arrays of unique elements in Numpy, use Numpy’s union1d() method.

Example – Create a set from unique elements of an array.

				
					import numpy as np

array_1 = np.array([1, 2, 3, 4, 4, 4, 5])
array_2 = np.array([6, 6, 7, 8, 8, 9])

array_set = np.union1d(array_1, array_2)

print(array_set)

				
			
Output - 

[1 2 3 4 5 6 7 8 9]

As a result, it returned a set of unique elements.

Finding Intersection

To create a set from common elements presented in both arrays, use intersect1d() method.

Example – Create a set from common elements of both arrays.

				
					import numpy as np

array_1 = np.array([1, 1, 2, 3, 5]) 
array_2 = np.array([2, 5, 7, 1, 1])

array_set = np.intersect1d(array_1, array_2, assume_unique=True)

print(array_set)

				
			
Output - 

[1 2]

As shown above, it returned a set of common unique elements.

Finding Difference

To create a set from elements presented only in an array first, not in array second, use setdiff1d() method.

 Example – Create a set from elements presented in only first array.

				
					import numpy as np

array_1 = np.array([1, 0, 2, 3, 5]) 
array_2 = np.array([4, 2, 3, 5, 6])

array_set = np.setdiff1d(array_1, array_2, assume_unique=True)

print(array_set)

				
			
Output - 

[1 0]

As shown above, it returned a set of unique elements of first array.

Example – Create a set from elements presented in only second array.

				
					import numpy as np

array_1 = np.array([1, 0, 2, 3, 5]) 
array_2 = np.array([4, 2, 8, 5, 8])

array_set = np.setdiff1d(array_2, array_1, assume_unique=True)

print(array_set)

				
			
python-numpy-set

As can be seen, it returned a set of unique elements of second array.

Finding Boolean Value

To create a set of Boolean values based on elements that are presented both arrays, use in1d() method.

				
					import numpy as np

array_1 = np.array([1, 0, 2, 3, 5])
array_2 = np.array([4, 2, 6, 5, 6])

array_set = np.in1d(array_1, array_2,  assume_unique=True)

print(array_set)

				
			
Output - 

[False False True False True]

As shown above, it returned a set of Boolean values based on unique elements.

Finding Symmetric Difference

To create a set from elements that are not presented in both array, use setxor1d() method.

Example – Create a set from elements not presented in both array.

				
					import numpy as np

array_1 = np.array([1, 0, 2]) 
array_2 = np.array([4, 2, 3])
array_set = np.setxor1d(array_1, array_2, assume_unique=True)

print(array_set)

				
			
Output - 

[0 1 4 3]

As has been noted, it returned a set of unique elements that are not presented in both sets.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Leave a Comment

Stay in the loop

codingstreets