numpy.unique1d

numpy.unique1d(ar1, return_index=False, return_inverse=False)

Find the unique elements of an array.

Parameters:

ar1 : array_like

This array will be flattened if it is not already 1-D.

return_index : bool, optional

If True, also return the indices against ar1 that result in the unique array.

return_inverse : bool, optional

If True, also return the indices against the unique array that result in ar1.

Returns:

unique : ndarray

The unique values.

unique_indices : ndarray, optional

The indices of the unique values. Only provided if return_index is True.

unique_inverse : ndarray, optional

The indices to reconstruct the original array. Only provided if return_inverse is True.

See also

numpy.lib.arraysetops
Module with a number of other functions for performing set operations on arrays.

Examples

>>> np.unique1d([1, 1, 2, 2, 3, 3])
array([1, 2, 3])
>>> a = np.array([[1, 1], [2, 3]])
>>> np.unique1d(a)
array([1, 2, 3])

Reconstruct the input from unique values:

>>> np.unique1d([1,2,6,4,2,3,2], return_index=True)
>>> x = [1,2,6,4,2,3,2]
>>> u, i = np.unique1d(x, return_inverse=True)
>>> u
array([1, 2, 3, 4, 6])
>>> i
array([0, 1, 4, 3, 1, 2, 1])
>>> [u[p] for p in i]
[1, 2, 6, 4, 2, 3, 2]

Previous topic

Set routines

Next topic

numpy.intersect1d

This Page

Quick search