×

numpy.unique() in Python

numpy.unique() in Python

The numpy.unique() function finds the unique elements of an array and returns the sorted unique elements for the specified array. 

Syntax

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

Parameter

ar : This parameter returns an Input array.If the aaray is not 1-D, this will be flattened.

return_index : This is an optional parameter which takes Boolean values. If Boolean value ‘True’ is passed, it returns the indices of the parameter ‘ar’ (along the specified axis, if provided, or in the flattened array) that result in the unique array.

return_inverse : If Boolean value ‘True’ is passed, it returns the indices of the unique array that can be used to reconstruct the parameter ‘ar’.

return_counts : If Boolean value ‘True’ is passed, it returns the number of times each unique item appears in ar.

axis : The axis parameter is used to operate on.

Return

This function returns the sorted unique elements of an array. There are three optional outputs in addition to the unique elements:

  • It returns the indices of the input array that give the unique values
  • the indices of the unique array that reconstructs the input array
  • the number of times each unique value derives up in the input array

Example 1

# Python program explaining
# numpy.unique() function
import numpy as np
inp_arr = np.array([5,8,6,2,7,5,2,8,12,5])
print ('Input array:')
print (inp_arr)
print ('\nUnique values for the Input array:' )
unq = np.unique(inp_arr)
print (unq)
print ('\nUnique array and Indices array:')
u,indices = np.unique(inp_arr, return_index = True)
print (indices)
print ('\nIndices of unique array:')
u,indices = np.unique(inp_arr,return_inverse = True)
print (u)
print ('Indices are:')
print (indices)
print ('\nReconstruct the original array using indices:')
print (u[indices])

Output

Input array:
[ 5  8  6  2  7  5  2  8 12  5]
Unique values for the Input array:
[ 2  5  6  7  8 12]
Unique array and Indices array:
[3 0 2 4 1 8]
Indices of unique array:
[ 2  5  6  7  8 12]
Indices are:
[1 4 2 0 3 1 0 4 5 1]
Reconstruct the original array using indices:
[ 5  8  6  2  7  5  2  8 12  5]

Related Topics

numpy.tile() in Python

numpy.tile() in Python The numpy.tile() function constructs an array by repeating the parameter ‘A’  the number of times as specified by the ‘reps’ parameter. Syntax numpy.tile(A, reps) Parameter The numpy.tile() function consists of two parameters, which...

1 minute read.

numpy.meshgrid() in Python

numpy.meshgrid() in Python The meshgrid() function of Python numpy class returns the coordinate matrices from coordinate vectors. Syntax numpy.meshgrid(*xi, **kwargs) Parameter The numpy.meshgrid() function consists of four parameters which are as follow: x1, x2,…, xn: This parameter signifies...

2 minutes read.

numpy.full_like() in Python

numpy.full_like() in Python The full_like() method of Python numpy class returns a full array with the same shape and type as a given array. Syntax numpy.full_like(a, fill_value, dtype=None, order='K', subok=True) Parameter shape :This parameter represents the number of rowsorder...

1 minute read.

numpy.ndarray.flatten() in Python

numpy.ndarray.flatten() in Python The numpy.ndarray.flatten() returns a copy of the array collapsed into 1-dimension. Syntax ndarray.flatten(order='C') Parameter The numpy. ndarray.flatten() method consists of one parameter, which is as follows: order : This parameter can be either...

1 minute read.

numpy.tril() in Python

numpy.tril() in Python The tril() function of Python numpy class returns a copy of an array with the elements above the k-th diagonal zeroed. Syntax numpy.tril(m, k=0) Parameter a: It represents the input array k: This parameter represents...

1 minute read.

numpy.transpose() in Python

numpy.transpose() in Python The numpy.transpose() permutes the dimensions of an array. Syntax numpy.transpose(a, axes=None) Parameter a : This parameter represents an input array. axes : It is an optional parameter which by default, reverses the dimensions, otherwise permutes the axes according...

1 minute read.

numpy.ones() in Python

numpy.ones() in Python The ones() method of Python numpy class returns a new array of given shape and type, filled with ones. Syntax numpy.ones(shape, dtype=None, order='C') Parameter The numpy.ones() method consists of three parameters, which are as...

1 minute read.

numpy.squeeze() in Python

numpy.squeeze() in Python The numpy.squeeze() function removes single-dimensional entries from the shape of an array. Syntax numpy.squeeze(a, axis=None) Parameter The numpy.squeeze() function has three parameters which are as follows: a: It represents the Input data. axis: This parameter signifies an int or...

1 minute read.

numpy.logspace() in Python

numpy.logspace() in Python The logspace() function of Python numpy class returns the equal number of spaces over every interval on a log scale. Syntax numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0) Parameter start: It represents the start of the interval range. stop:...

1 minute read.

Python NumPy Tutorial

NumPy is a scientific library in Python Programming Language. It provides objects and routines for fast operations on arrays, random simulations, statistical operations, sorting, etc. Numpy has an ndarray object which...

2 minutes read.

numpy.identity() in Python

numpy.identity() in Python The identity() method of Python numpy class returns an identity matrix i.e., a square matrix with ones on the main diagonal. Syntax numpy.identity(n, dtype=None) Parameters The numpy. identity()  method consists of two parameters,...

1 minute read.

numpy.asanyarray() in Python with Example

numpy.asanyarray() in Python The asanyarray() function of Python numpy class converts the input to an ndarray, but pass ndarray subclasses through. Syntax numpy.asanyarray(a, dtype=None, order=None) Parameter arr : This parameter includes the input data, in any form that...

1 minute read.

numpy.reshape() in Python

numpy.reshape() in Python The numpy.reshape() function gives a new shape to an array without changing its data. Syntax numpy.reshape(a, newshape, order='C') Parameter The numpy.reshape() method consists of three parameters, which are as follows: array : It represents our...

1 minute read.

numpy.atleast_2d() in Python

numpy.atleast_2d() in Python The numpy.atleast_2d() function converts the inputs as arrays with at least two dimensions. Syntax numpy.atleast_2d(*arys) Parameter arys1, arys2, … : This parameter represents one or more array-like sequences where the non-array inputs are converted...

1 minute read.

numpy.delete() in Python

numpy.delete() in Python The numpy.delete() function returns a new array with sub-arrays along an axis deleted. For 1-D array, this function returns those entries which are not returned by arr[obj]. Syntax numpy.delete(arr, obj, axis=None) Parameter arr: This parameter returns...

2 minutes read.

numpy.ndarray.T in Python

numpy.ndarray.T in Python The numpy.ndarray.T attribute makes a Transpose of an array having a dimension greater than or equal to 2. Syntax ndarray.T() Parameter NA Return This attribute returns the transpose of an array Example 1 # Python Program explaining # numpy.ndarray.T()...

1 minute read.

numpy.geomspace() in Python

numpy.geomspace() in Python The geomspace() function of Python numpy class returns the numbers spaced equally on a log scale (a geometric progression). This method is similar to numpy.logspace() but with endpoints specified directly. Syntax numpy.geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0) Parameter start: This...

1 minute read.

numpy.zeros in Python

numpy.zeros in Python The zeros() method of Python numpy class returns a new array of given shape and type, filled with zeros. Syntax numpy.zeros(shape, dtype=float, order='C') Parameter The numpy.zeros() method consists of three parameters, which are as...

1 minute read.

numpy.expand_dims() in Python

numpy.expand_dims() in Python The numpy.expand_dims() function expands the shape of an array. It Inserts a new axis that appears at the axis position in the expanded array shape. Syntax numpy.expand_dims(a, axis) Parameter The numpy.expand_dims() function has two parameters...

1 minute read.

Numpy.asmatrix() in Python with Example

Numpy.asmatrix() in Python The asmatrix() function returns the specified input as a matrix. It does not make a copy if the input is already a matrix or an ndarray. Syntax numpy.asmatrix(data, dtype=None) Parameter data  :This parameter...

1 minute read.