unique_counts#
- ivy.unique_counts(x, /)[source]#
Return the unique elements of an input array
x
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, 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
nan
values compare asFalse
,nan
values 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
).
- Parameters:
x (
Union
[Array
,NativeArray
]) – input array. Ifx
has more than one dimension, the function must flattenx
and return the unique elements of the flattened array.- Return type:
- Returns:
ret – a namedtuple
(values, counts)
whosefirst element must have the field name
values
and 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
counts
and must be an array containing the number of times each unique element occurs inx
. The returned array must have same shape asvalues
and 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.Container
instances in place of any of the arguments.Examples
With
ivy.Array
input:>>> x = ivy.array([1,2,1,3,4,1,3]) >>> y = ivy.unique_counts(x) >>> print(y) Results(values=ivy.array([1, 2, 3, 4]), counts=ivy.array([3, 1, 2, 1]))
>>> x = ivy.asarray([[1,2,3,4],[2,3,4,5],[3,4,5,6]]) >>> y = ivy.unique_counts(x) >>> print(y) Results(values=ivy.array([1, 2, 3, 4, 5, 6]), counts=ivy.array([1, 2, 3, 3, 2, 1]))
>>> x = ivy.array([0.2,0.3,0.4,0.2,1.4,2.3,0.2]) >>> y = ivy.unique_counts(x) >>> print(y) Results(values=ivy.array([0.2 , 0.30000001, 0.40000001, 1.39999998, 2.29999995]), counts=ivy.array([3, 1, 1, 1, 1]))
With
ivy.Container
input:>>> x = ivy.Container(a=ivy.array([0., 1., 3. , 2. , 1. , 0.]), ... b=ivy.array([1, 2, 1, 3, 4, 1, 3])) >>> y = ivy.unique_counts(x) >>> print(y) { a: (list[2],<classivy.array.array.Array>shape=[4]), b: (list[2],<classivy.array.array.Array>shape=[4]) }
- Array.unique_counts(self)[source]#
ivy.Array instance method variant of ivy.unique_counts. This method simply wraps the function, and so the docstring for ivy.unique_counts also applies to this method with minimal changes.
- Parameters:
self (
Array
) – input array. Ifx
has more than one dimension, the function must flattenx
and return the unique elements of the flattened array.- Return type:
Tuple
[Array
,Array
]- Returns:
ret – a namedtuple
(values, counts)
whosefirst element must have the field name
values
and 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 namecounts
and must be an array containing the number of times each unique element occurs inx
. The returned array must have same shape asvalues
and must have the default array index data type.
Examples
>>> x = ivy.array([0., 1., 2. , 1. , 0.]) >>> y = x.unique_counts() >>> print(y) Results(values=ivy.array([0.,1.,2.]),counts=ivy.array([2,2,1]))
- Container.unique_counts(self, /, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#
ivy.Container instance method variant of ivy.unique_counts. This method simply wraps the function, and so the docstring for ivy.unique_counts also applies to this method with minimal changes.
- Parameters:
self (
Container
) – input container. Ifx
has more than one dimension, the function must flattenx
and 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, counts)
whosefirst element must have the field name
values
and 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 namecounts
and must be an array containing the number of times each unique element occurs inx
. The returned array must have same shape asvalues
and must have the default array index data type.
Examples
With
ivy.Container
instance method:>>> x = ivy.Container(a=ivy.array([0., 1., 3. , 2. , 1. , 0.]), ... b=ivy.array([1,2,1,3,4,1,3])) >>> y = x.unique_counts() >>> print(y) [{ a: ivy.array([0., 1., 2., 3.]), b: ivy.array([1, 2, 3, 4]) }, { a: ivy.array([2, 2, 1, 1]), b: ivy.array([3, 1, 2, 1]) }]