Introduction to numpy.unique()
Numpy unique() gets utilized in order to identify the exclusive elements present in an array. The function returns the exclusive elements in a sorted manner for the given array. Additionally, three outputs can be derived when running the numpy.unique() which can return the following:
- The function can return the absolute times the unique elements contained in an array which has been input by the user.
- The function can return the respective indices representative of the unique values contained in the array which has been input by the user.
- The function can return the respective indices of the unique elements for the array which will help in the reconstruction of the array originally entered by the user.
Syntax and Parameters
Syntax and Parameters of numpy.unique( ) are given below:
numpy.unique(ar, return_index = False ,return _ inverse = False, return_counts = False, axis = None)
- ar: array_like: The input array entered – It will be flattened in the case it is not one dimensional.
- return_index: bool,(optional): If the parameter is true the indices of the input array ar would be returned which results to form a unique array with no repetitive elements.
- return_ inverse: bool, (optional): If the parameter entered is true, the indices of the input array ar would be returned which results to form a unique array used for reconstructing the array ar.
- return_ counts: bool, (optional) – found in version 1.9.0: New in version 1.9.0. If the parameter entered is true, the number of instances when the unique elements have been repeated in the input array ar is returned.
- unique: ndarray: The sorted unique elements of the input array ar.
- unique_indices: ndarray, (optional): When the return_index is validated as true the returned value contains the indices of primary occurrence of the distinct values which were present in the input array (which has to be flattened)
- unique a _ a inversea: a ndarray, (optional): The indices are retuened which can be used for reconstruction of the originally input array which can be constructed using the distinct values.
- unique a _ a countsa: a ndarray, a (optional) present in the newer version 1.9.0: The number of times each of the unique values comes up in the original array. Only provided if return_counts is True.
Example of numpy.unique()
Example of using numpy.unique() function are:
import numpy as np a1= np.array([5, 2, 6, 2, 7, 5, 6, 8, 2, 9]) print '1st a array to be entered:' print a1 print 'The unique values of the 1st Array:' u1 = np.unique (a1) print u1 print 'Displaying the Unique array & Indices array:' i1 = np.unique(a1, return_index= True) print i1 print 'Number corresponding to the index:' print a1 print 'Indices of unique array:' u1,indices = np.unique(1, return_inverse=True) print u1 print 'The indices for the given array are:' print indices print 'Reconstruction of the original array with the use of indices:' print u1[indices] print 'Return the count of repetitions of unique elements:' u1,indices = np.unique(a1, return_counts = True) print u1 print indices
Numpy provides for an exclusive function which is a very essential and constantly used function required for most data-intensive code work. In such cases, the verbosity of the code enlarges in the repetition of codes affecting the computation speed especially when the data set the id of large dimension. This function serves as a boon in such times and eases out by providing for one-liner coding solution.
This is a guide to numpy.unique( ). Here we discuss the definition, syntax, and parameters of numpy.unique( ) along with an example and its code implementation. You may also look at the following articles to learn more –