unique_inverse¶
- unique_inverse(x: array, /) Tuple[array, array] ¶
Returns the unique elements of an input array
x
and the indices from the set of unique elements that reconstructx
.Data-dependent output shape
The shapes of two of the output arrays for this function depend on the data values in the input array; hence, array libraries which build computation graphs (e.g., JAX, Dask, etc.) may find this function difficult to implement without knowing array values. Accordingly, such libraries may choose to omit this function. See Data-dependent output shapes section for more details.
Note
Uniqueness should be determined based on value equality (see
equal()
). For input arrays having floating-point data types, value-based equality implies the following behavior.As
nan
values compare asFalse
,nan
values should be considered distinct.As complex floating-point values having at least one
nan
component compare asFalse
, complex floating-point values havingnan
components should be considered distinct.As
-0
and+0
compare asTrue
, signed zeros should not be considered distinct, and the corresponding unique element will be implementation-dependent (e.g., an implementation could choose to return-0
if-0
occurs before+0
).
As signed zeros are not distinct, using
inverse_indices
to reconstruct the input array is not guaranteed to return an array having the exact same values.- Parameters:
x (array) – input array. If
x
has more than one dimension, the function must flattenx
and return the unique elements of the flattened array.- Returns:
out (Tuple[array, array]) – a namedtuple
(values, inverse_indices)
whosefirst element must have the field name
values
and must be a one-dimensional array containing the unique elements ofx
. The array must have the same data type asx
.second element must have the field name
inverse_indices
and must be an array containing the indices ofvalues
that reconstructx
. The array must have the same shape asx
and have the default array index data type.
Note
The order of unique elements is not specified and may vary between implementations.
Notes
Changed in version 2022.12: Added complex data type support.
Changed in version 2023.12: Clarified flattening behavior.