Set#
- class ivy.data_classes.array.set._ArrayWithSet[source]#
Bases:
ABC
- _abc_impl = <_abc._abc_data object>#
- unique_all(*, axis=None, by_value=True)[source]#
ivy.Array instance method variant of ivy.unique_all. This method simply wraps the function, and so the docstring for ivy.unique_all also applies to this method with minimal changes.
- Parameters:
self (
Array
) – input array.axis (
Optional
[int
], default:None
) – the axis to apply unique on. If None, the unique elements of the flattenedx
are returned.by_value (
bool
, default:True
) – If False, the unique elements will be sorted in the same order that they occur in ‘’x’’. Otherwise, they will be sorted by value.
- Return type:
Tuple
[Array
,Array
,Array
,Array
]- Returns:
ret – a namedtuple
(values, indices, inverse_indices, counts)
. The details can be found in the docstring for ivy.unique_all.
Examples
>>> x = ivy.randint(0, 10, shape=(2, 2), seed=0) >>> z = x.unique_all() >>> print(z) Results(values=ivy.array([1, 2, 5, 9]), indices=ivy.array([3, 2, 1, 0]), inverse_indices=ivy.array([[3, 2], [1, 0]]), counts=ivy.array([1, 1, 1, 1]))
- unique_counts()[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]))
- unique_inverse()[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. 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, inverse_indices)
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
inverse_indices
and must be an array containing the indices ofvalues
that reconstructx
. The array must have the same shape asx
and 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]))
- unique_values(*, out=None)[source]#
Return the unique elements of an input array x. .. admonition:: Data-dependent output shape
- class:
important
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. - Asnan
values compare asFalse
,nan
valuesshould 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 (ivy.Array or ivy.NativeArray) – Input array. If x has more than one dimension, the function must flatten x and return the unique elements of the flattened array.
out (ivy.Array, optional) – Optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ivy.Array – An array containing the set of unique elements in x. The returned array must have the same data type as x. .. note:
The order of unique elements is not specified and may vary between implementations.
- Raises:
TypeError – If x is not an instance of ivy.Array or ivy.NativeArray.
Examples
>>> import ivy >>> x = ivy.array([1, 2, 2, 3, 4, 4, 4]) >>> print(x.unique_values()) ivy.array([1, 2, 3, 4])
>>> x = ivy.array([[1, 2], [3, 4]]) >>> print(x.unique_values()) ivy.array([1, 2, 3, 4])
This should have hopefully given you an overview of the set submodule, if you have any questions, please feel free to reach out on our discord!