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:
numpy.unique(a, return_index=False, return_inverse=False, return_counts=False, axis=None)
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.
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.
import numpy as np a=np.unique([1,2,3,4,3,6,2,4]) a
Output:
In the above code
In the output, a ndarray has been shown, which contains unique elements.
a=np.array([[1,2,2,3,9],[1,4,3,5,8]]) a b=np.unique(a) b
Output:
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:
In the above code
In the output, a ndarray has been shown that contains unique rows of the source array 'a'.
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:
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:
In the above code
In the output, a ndarray has been shown that contains the indices of the original array that give unique values.
We can reconstruct the input array from the unique values in the following way:
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: