×

NumPy Set Operations

Set is a collection of well-defined and distinct elements. Sets are one of the fundamental concepts in mathematics. The NumPy package provides the following functions to perform set operations on the array.

  • unique()
  • in1d()
  • intersect1d()
  • isin()
  • setdiff1d()
  • setxor1d()
  • union1d()

unique():

The unique() function finds the unique elements of an array. The unique() function returns the sorted unique elements of an array.

There are three optional outputs in addition to the unique elements:

  • The indices of the input array that give the unique values.
  • The indices of the unique array that reconstruct the input array.
  • The number of times each unique value comes up in the input array

Syntax:

unique(input_array, return_index=False, return_inverse=False, return_counts=False, axis=None) 

The input_array parameter is the input array. The return_index parameter if True, returns the indices of the input array that result in the unique array. The return_inverse parameter if True, returns the indices of the unique array that can be used to reconstruct the input array.

The return_counts parameter if True, returns the number of times each unique item appears in the input array. The axis parameter is the axis to operate on. if the axis is not given, the input array will be flattened. 

import numpy as np
input_array = np.array([1, 2, 3, 4, 1, 3, 4, 1, 4, 5, 6, 3, 8, 8, 7])
print(np.unique(input_array, return_index = True, return_counts = True))

#Output:
(array([1, 2, 3, 4, 5, 6, 7, 8]), array([ 0,  1,  2,  3,  9, 10, 14, 12], dtype=int64), array([3, 1, 3, 3, 1, 1, 1, 2], dtype=int64))

in1d():

The in1d() function tests whether each element of a 1-D array is also present in a second array. The in1d() function returns a boolean array the same length as the input array that is True where the elements of the first array are in the second array and False otherwise.

Syntax:

in1d(input_array1, input_array2)

The input_array1 and input_array2 parameters are the two input arrays to be compared.

input_array1 = np.array([1, 5, 2, 6, 9, 7, 10])
input_array2 = np.array([1, 5, 2, 6, 9, 7, 8])
print(np.in1d(input_array1,input_array2))

#Output:
[ True  True  True  True  True  True False]

intersect1d():

The instersect1d() function finds the intersection of two arrays. The intersect1d() function returns the sorted, unique values that are in both of the input arrays.

Syntax:

intersect1d(input_array1, input_array2, return_indices = False)

The input_array1 and input_array2 parameters are the two input arrays. The return_indices parameter If True, the indices which correspond to the intersection of the two arrays are returned(The first instance of a value is used if there are multiple).

input_array1 = np.array([1, 5, 2, 6, 9, 7, 10])
input_array2 = np.array([1, 3, 2, 6, 4, 7, 8])
print(np.intersect1d(input_array1,input_array2, return_indices = True))

#Output:
(array([1, 2, 6, 7]), array([0, 2, 3, 5], dtype=int64), array([0, 2, 3, 5], dtype=int64))

isin():

The isin() function calculates if elements specified are in the input array. The isin() function returns a boolean array of the same shape as the input array that is True where an element specified is in the input array and False otherwise.

Syntax:

isin(input_array, test_elements)

The input_array parameter is the input array. The test_elements parameter is the values to check whether they exist in the input array.

input_array = np.array([1, 5, 23, 8, 9, 7, 67])
test_elements = np.array([1, 8, 9])
print(np.isin(input_array,test_elements))

#Output:
[ True False False  True  True False False]

setdiff1d():

The setdiff1d() function finds the set difference of two input arrays. The setdiff1d() function returns the unique values in the first array that are not in the second array.

Syntax:

setdiff1d(input_array1, input_array2)

The input_array1 and input_array2 parameters are the two input arrays.

input_array1 = np.array([1, 8, 2, 6, 9, 7, 23])
input_array2 = np.array([1, 3, 2, 6, 4, 7, 8])
print(np.setdiff1d(input_array1,input_array2))

#Output:
[ 9 23]

setxor1d():

The setxor1d() function finds the set exclusive-or of two arrays. The setxor1d() function returns the sorted, unique values that are in only one (not both) of the input arrays.

Syntax:

setxor1d(input_array1, input_array2)
input_array1 = np.array([1, 8, 2, 6, 9, 7, 23])
input_array2 = np.array([1, 3, 2, 6, 4, 7, 8])
print(np.setxor1d(input_array1,input_array2))

#Output:
[ 3  4  9 23]

union1d():

The union1d() function finds the union of two arrays. The union1d() function returns the unique, sorted array of values that are in either of the two input arrays.

Syntax:

union1d(input_array1, input_array2)
input_array1 = np.array([1, 0, 3, 7])
input_array2 = np.array([8, 3, 2, 6])
print(np.union1d(input_array1,input_array2))

#Output:
[0 1 2 3 6 7 8]