unique_inverse#
- ivy.unique_inverse(x, /, *, axis=None)[source]#
Return 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 (i.e.,
x_i == x_j). 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
-0and+0compare 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-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.- Parameters:
x (
Union[Array,NativeArray]) – the array that will be inputted into the “unique_inverse” functionaxis (
Optional[int], default:None) – the axis to apply unique on. If None, the unique elements of the flattenedxare returned.
- Return type:
- Returns:
ret – a namedtuple
(values, inverse_indices)whose - first element must have the field namevaluesand must be an arraycontaining the unique elements of
x. The array must have the same data type asx.second 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.
Note
The order of unique elements is not specified and may vary between implementations.
This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.
Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts
ivy.Containerinstances in place of any of the arguments.Examples
With
ivy.Arrayinput:>>> x = ivy.array([4,5,3,2,4,1,3]) >>> y = ivy.unique_inverse(x) >>> print(y) Results(values=ivy.array([1, 2, 3, 4, 5]), inverse_indices=ivy.array([3, 4, 2, 1, 3, 0, 2]))
>>> x = ivy.array([0.5,0.3,0.8,0.2,1.2,2.4,0.3]) >>> y = ivy.ivy.unique_inverse(x) >>> print(y) Results(values=ivy.array([0.2, 0.3, 0.5, 0.8, 1.2, 2.4]), inverse_indices=ivy.array([2, 1, 3, 0, 4, 5, 1]))
- Array.unique_inverse(self)[source]#
ivy.Array instance method variant of ivy.unique_inverse. This method simply wraps the function, and so the docstring for ivy.unique_inverse also applies to this method with minimal changes.
- Parameters:
self (
Array) – input array. Ifxhas more than one dimension, the function must flattenxand return the unique elements of the flattened array.- Return type:
Tuple[Array,Array]- Returns:
ret – a namedtuple
(values, inverse_indices)whosefirst element must have the field name
valuesand must be an array containing the unique elements ofx. The array must have the same data type asx.second 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.
Examples
>>> x = ivy.array([0.3,0.4,0.7,0.4,0.2,0.8,0.5]) >>> y = x.unique_inverse() >>> print(y) Results(values=ivy.array([0.2, 0.3, 0.4, 0.5, 0.7, 0.8]), inverse_indices=ivy.array([1, 2, 4, 2, 0, 5, 3]))
- Container.unique_inverse(self, /, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#
ivy.Container instance method variant of ivy.unique_inverse. This method simply wraps the function, and so the docstring for ivy.unique_inverse also applies to this method with minimal changes.
- Parameters:
self (
Container) – input container. Ifxhas more than one dimension, the function must flattenxand return the unique elements of the flattened array.key_chains (
Optional[Union[List[str],Dict[str,str],Container]], default:None) – The key-chains to apply or not apply the method to. Default isNone.to_apply (
Union[bool,Container], default:True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default isTrue.prune_unapplied (
Union[bool,Container], default:False) – Whether to prune key_chains for which the function was not applied. Default isFalse.map_sequences (
Union[bool,Container], default:False) – Whether to also map method to sequences (lists, tuples). Default isFalse.
- Return type:
Container- Returns:
ret – a namedtuple
(values, inverse_indices)whosefirst element must have the field name
valuesand must be an array
containing the unique elements of
x. The array must have the same data type asx. - second element must have the field nameinverse_indicesandmust be an array containing the indices of
valuesthat reconstructx. The array must have the same shape asxand must have the default array index data type.
Examples
>>> x = ivy.Container(a=ivy.array([4.,8.,3.,5.,9.,4.]), ... b=ivy.array([7,6,4,5,6,3,2])) >>> y = x.unique_inverse() >>> print(y) [{ a: ivy.array([3., 4., 5., 8., 9.]), b: ivy.array([2, 3, 4, 5, 6, 7]) }, { a: ivy.array([1, 3, 0, 2, 4, 1]), b: ivy.array([5, 4, 2, 3, 4, 1, 0]) }]
>>> x = ivy.Container(a=ivy.array([1., 4., 3. , 5. , 3. , 7.]), ... b=ivy.array([3, 2, 6, 3, 7, 4, 9])) >>> y = ivy.ivy.unique_inverse(x) >>> print(y) [{ a: ivy.array([1., 3., 4., 5., 7.]), b: ivy.array([2, 3, 4, 6, 7, 9]) }, { a: ivy.array([0, 2, 1, 3, 1, 4]), b: ivy.array([1, 0, 3, 1, 4, 2, 5]) }]