numpy.unique() in Python

The numpy module of Python provides a function for finding unique elements in a numpy array. The numpy.unique() function finds the unique elements of an array and returns these unique elements as a sorted array. Apart from the unique elements, there are some optional outputs also, which are as follows:

  • The output can be the indices of the input array which give the unique values
  • The output can be the indices of the unique array which reconstruct the input array
  • The output can be an array of the number of times each unique value comes in the input array.

Syntax

snippet
numpy.unique(a, return_index=False, return_inverse=False, return_counts=False, axis=None)

Parameters

These are the following parameters in numpy.mean() function:

a: array_like

This parameter defines the source array containing elements whose unique values are desired. The array will be flattened if it is not 1-D array.

Return_index: bool(optional)

If this parameter is set True, the function will return the indices of the input array(along the specified axis if provided or in the flattened array), which results in the unique array.

return_inverse: bool(optional)

If this parameter is set True, the function will also return the indices of the input array(along the specified axis if provided or in the flattened array), which can be used to reconstruct the input array.

Return_counts: bool(optional)

If this parameter is set True, the function will return the number of times each unique item appeared in the input array 'a'.

axis: int or None(optional)

This parameter defines the axis to operate on. If this parameter is not set, then the array 'a' will be flattened. If this parameter is an integer, then the subarrays indexed by the given axis will be flattened and treated as an element of a 1-D array with the dimension of the given axis. Structured arrays or object arrays that contain objects are not supported if the axis 'kwarg' is used.

Returns

This function returns four types of outputs which are as follows:

unique: ndarray

In this output, a ndarray will be shown that contain sorted unique values.

unique_indices: ndarray(optional)

In this output, a ndarray will be shown that contains the indices of the first occurrences of the unique values in the original array. This output is only provided if return_index is True.

unique_inverse: ndarray(optional)

In this output, a ndarray will be shown that contains the indices to reconstruct the original array from the unique array. This output is only provided if return_inverse is True.

unique_counts: ndarray(optional)

In this output, a ndarray will be shown that contains the number of times each of the unique values comes up in the original array. This output is only provided if return_counts is True.

Example 1:

snippet
import numpy as np
a=np.unique([1,2,3,4,3,6,2,4])
a

Output:

Output
array([1, 2, 3, 4, 6])

In the above code

  • We have imported numpy with alias name np.
  • We have declared the variable 'a' and assigned the returned value of np.unique() function.
  • We have passed the number of elements in the function.
  • Lastly, we tried to print the value of 'a'.

In the output, a ndarray has been shown, which contains unique elements.

Example 2:

snippet
a=np.array([[1,2,2,3,9],[1,4,3,5,8]])
a
b=np.unique(a)
b

Output:

Output
array([[1, 2, 2, 3, 9], [1, 4, 3, 5, 8]]) array([1, 2, 3, 4, 5, 8, 9])

Example 3:

snippet
import numpy as np
a = np.array([[1, 1, 0], [1, 1, 0], [2, 3, 4],[5, 9, 8],[2, 3, 4]])
a
b=np.unique(a, axis=0)
b

Output:

Output
array([[1, 1, 0], [1, 1, 0], [2, 3, 4], [5, 9, 8], [2, 3, 4]]) array([[1, 1, 0], [2, 3, 4], [5, 9, 8]])

In the above code

  • We have imported numpy with alias name np.
  • We have created a multi-dimensional array 'a'.
  • We have declared the variable 'b' and assigned the returned value of np.unique() function.
  • We have passed the multi-dimensional array 'a' and axis as 0 in the function.
  • Lastly, we tried to print the value of 'b'.

In the output, a ndarray has been shown that contains unique rows of the source array 'a'.

Example 4:

snippet
import numpy as np
a = np.array([[1, 1, 0], [1, 1, 0], [2, 2, 4],[5, 5, 8],[2, 2, 4]])
a
b=np.unique(a, axis=1)
b

Output:

Output
array([[1, 1, 0], [1, 1, 0], [2, 2, 4], [5, 5, 8], [2, 2, 4]]) array([[0, 1], [0, 1], [4, 2], [8, 5], [4, 2]])
Note
Note: When we set axis as 1 then this function returns the unique columns from the source array.

Example 5: Use return_index

snippet
import numpy as np
a = np.array(['d', 'b', 'b', 'z', 'a'])
result, indices=np.unique(a,return_index=True)
result
indices
a[indices]

Output:

Output
array(['a', 'b', 'd', 'z'], dtype='|S1') array([4, 1, 0, 3], dtype=int64) array(['a', 'b', 'd', 'z'], dtype='|S1')

In the above code

  • We have imported numpy with alias name np.
  • We have created an array 'a'.
  • We have declared the variable 'result' and 'indices' and assigned the returned value of np.unique() function.
  • We have passed the array 'a' and set return_index to True in the function.
  • Lastly, we tried to print the value of 'result', 'indices', and array elements, which indicates the indices('a [indices]').

In the output, a ndarray has been shown that contains the indices of the original array that give unique values.

Example 6: Use return_inverse

We can reconstruct the input array from the unique values in the following way:

snippet
import numpy as np
a = np.array([1, 2, 6, 4, 5, 3, 2])
result, indices=np.unique(a,return_inverse=True)
result
indices
a[indices]

Output:

Output
array([1, 2, 3, 4, 5, 6]) array([0, 1, 5, 3, 4, 2, 1], dtype=int64) array([1, 2, 3, 4, 5, 6, 2])
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +