unique_all¶
- unique_all(x: array, /) Tuple[array, array, array, array]¶
Returns the unique elements of an input array
x, the first occurring indices for each unique element inx, the indices from the set of unique elements that reconstructx, and the corresponding counts for each unique element inx.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, et cetera) can 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.
- Parameters:
x (array) – input array. If
xhas more than one dimension, the function must flattenxand return the unique elements of the flattened array.- Returns:
out (Tuple[array, array, array, array]) – a namedtuple
(values, indices, inverse_indices, counts)whosefirst element must have the field name
valuesand 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
indicesand must be an array containing the indices (first occurrences) of a flattenedxthat result invalues. The array must have the same shape asvaluesand must have the default array index data type.third element must have the field name
inverse_indicesand must be an array containing the indices ofvaluesthat reconstructx. The array must have the same shape asxand must have the default array index data type.fourth element must have the field name
countsand must be an array containing the number of times each unique element occurs inx. The order of the returned counts must match the order ofvalues, such that a specific element incountscorresponds to the respective unique element invalues. The returned array must have same shape asvaluesand must have the default array index data type.
Notes
The order of unique elements returned by this function is unspecified and thus implementation-defined. As a consequence, element order may vary between implementations.
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
nanvalues compare asFalse,nanvalues should be considered distinct.As complex floating-point values having at least one
nancomponent compare asFalse, complex floating-point values havingnancomponents should be considered distinct.As
-0and+0compare asTrue, signed zeros should not be considered distinct, and the corresponding unique element may be implementation-defined (e.g., an implementation may choose to return-0if-0occurs before+0).
As signed zeros are not distinct, using
inverse_indicesto reconstruct the input array is not guaranteed to return an array having the exact same values.Each
nanvalue and each complex floating-point value having anancomponent should have a count of one, while the counts for signed zeros should be aggregated as a single count.
Changed in version 2022.12: Added complex data type support.
Changed in version 2023.12: Clarified flattening behavior and required the order of
countsmatch the order ofvalues.