one_hot#
- ivy.one_hot(indices, depth, /, *, on_value=None, off_value=None, axis=None, dtype=None, device=None, out=None)[source]#
Return a one-hot array. The locations represented by indices in the parameter indices take value on_value, while all other locations take value off_value.
- Parameters:
indices (
Union
[Array
,NativeArray
]) – Indices for where the ones should be scattered [batch_shape, dim]depth (
int
) – Scalar defining the depth of the one-hot dimension.on_value (
Optional
[Number
]) – Scalar defining the value to fill in output when indices[j] == i. (default:None
) Default:1
.off_value (
Optional
[Number
]) – Scalar defining the value to fill in output when indices[j] != i. (default:None
) Default:0
.axis (
Optional
[int
]) – Axis to scatter on. The default is-1
, a new inner-most axis is created. (default:None
)dtype (
Optional
[Union
[Dtype
,NativeDtype
]]) – The data type of the output tensor. (default:None
)device (
Optional
[Union
[Device
,NativeDevice
]]) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. Same as x if (default:None
) None.out (
Optional
[Array
]) – optional output array, for writing the result to. It must have a shape that the (default:None
) inputs broadcast to.
- Return type:
- Returns:
ret – Tensor of zeros with the same shape and type as a, unless dtype provided which overrides.
Examples
With
ivy.Array
inputs:>>> x = ivy.array([3, 1]) >>> y = 5 >>> z = x.one_hot(5) >>> print(z) ivy.array([[0., 0., 0., 1., 0.], ... [0., 1., 0., 0., 0.]])
>>> x = ivy.array([0]) >>> y = 5 >>> ivy.one_hot(x, y) ivy.array([[1., 0., 0., 0., 0.]])
>>> x = ivy.array([0]) >>> y = 5 >>> ivy.one_hot(x, 5, out=z) ivy.array([[1., 0., 0., 0., 0.]]) >>> print(z) ivy.array([[1., 0., 0., 0., 0.]])
With
ivy.Container
input:>>> x = ivy.Container(a=ivy.array([1, 2]), b=ivy.array([3, 1]), c=ivy.array([2, 3])) >>> y = 5 >>> z = x.one_hot(y) >>> print(z) { a: ivy.array([[0., 1., 0., 0., 0.], [0., 0., 1., 0., 0.]]), b: ivy.array([[0., 0., 0., 1., 0.], [0., 1., 0., 0., 0.]]), c: ivy.array([[0., 0., 1., 0., 0.], [0., 0., 0., 1., 0.]]) }
>>> x = ivy.Container(a=ivy.array([2]), b=ivy.array([]), c=ivy.native_array([4])) >>> y = 7 >>> z = x.one_hot(y) >>> print(z) { a: ivy.array([[0., 0., 1., 0., 0., 0., 0.]]), b: ivy.array([], shape=(0, 7)), c: ivy.array([[0., 0., 0., 0., 1., 0., 0.]]) }
- Array.one_hot(self, depth, /, *, on_value=None, off_value=None, axis=None, dtype=None, device=None, out=None)#
ivy.Array instance method variant of ivy.one_hot. This method simply wraps the function, and so the docstring for ivy.one_hot also applies to this method with minimal changes.
- Parameters:
self (
Array
) – input array containing the indices for which the ones should be scattereddepth (
int
) – Scalar defining the depth of the one-hot dimension.on_value (
Optional
[Number
]) – Value to fill in output whenindices[j] == i
. Default 1. (default:None
)off_value (
Optional
[Number
]) – Value to fill in output whenindices[j] != i
. Default 0. (default:None
)axis (
Optional
[int
]) – The axis to scatter on. The default is-1
which is the last axis. (default:None
)dtype (
Optional
[Union
[Dtype
,NativeDtype
]]) – The data type of the output array. If None, the data type of the on_value is (default:None
) used, or if that is None, the data type of the off_value is used. Default float32.device (
Optional
[Union
[Device
,NativeDevice
]]) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. (default:None
) Same as x if None.out (
Optional
[Array
]) – optional output array, for writing the result to. It must have a shape (default:None
) that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – Tensor of zeros with the same shape and type as a, unless dtype provided which overrides.
Examples
With
ivy.Array
inputs:>>> x = ivy.array([3, 1]) >>> y = 5 >>> z = x.one_hot(5) >>> print(z) ivy.array([[0., 0., 0., 1., 0.], ... [0., 1., 0., 0., 0.]])
>>> x = ivy.array([0]) >>> y = 5 >>> ivy.one_hot(x, y) ivy.array([[1., 0., 0., 0., 0.]])
>>> x = ivy.array([0]) >>> y = 5 >>> ivy.one_hot(x, 5, out=z) ivy.array([[1., 0., 0., 0., 0.]]) >>> print(z) ivy.array([[1., 0., 0., 0., 0.]])
- Container.one_hot(self, depth, /, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, *, on_value=None, off_value=None, axis=None, dtype=None, device=None, out=None)#
ivy.Container instance method variant of ivy.one_hot. This method simply wraps the function, and so the docstring for ivy.one_hot also applies to this method with minimal changes.
- Parameters:
self (
Container
) – Indices for where the ones should be scattered [batch_shape, dim]depth (
int
) – Scalar defining the depth of the one-hot dimension.on_value (
Optional
[Number
]) – Value to fill in output when indices[j] == i. If None, defaults to 1. (default:None
)off_value (
Optional
[Number
]) – Value to fill in output when indices[j] != i. If None, defaults to 0. (default:None
)axis (
Optional
[int
]) – Axis to scatter on. The default is-1
, a new inner-most axis is created. (default:None
)dtype (
Optional
[Union
[Dtype
,NativeDtype
]]) – The dtype of the returned tensor. If None, defaults to the on_value dtype (default:None
) or the off_value dtype. If both are None, defaults to float32.key_chains (
Optional
[Union
[List
[str
],Dict
[str
,str
]]]) – The key-chains to apply or not apply the method to. Default isNone
. (default:None
)to_apply (
bool
) – If True, the method will be applied to key_chains, otherwise key_chains will (default:True
) be skipped. Default isTrue
.prune_unapplied (
bool
) – Whether to prune key_chains for which the function was not applied. Default (default:False
) is False.map_sequences (
bool
) – Whether to also map method to sequences (lists, tuples). (default:False
) Default isFalse
.out (
Optional
[Container
]) – optional output container, for writing the result to. It must have a (default:None
) shape that the inputs broadcast to.
- Return type:
Container
- Returns:
ret – container with tensors of zeros with the same shape and type as the inputs, unless dtype provided which overrides.
Examples
With
ivy.Container
input:>>> x = ivy.Container(a=ivy.array([1, 2]), b=ivy.array([3, 1]), c=ivy.array([2, 3])) >>> y = 5 >>> z = x.one_hot(y) >>> print(z) { a: ivy.array([[0., 1., 0., 0., 0.], [0., 0., 1., 0., 0.]]), b: ivy.array([[0., 0., 0., 1., 0.], [0., 1., 0., 0., 0.]]), c: ivy.array([[0., 0., 1., 0., 0.], [0., 0., 0., 1., 0.]]) }
>>> x = ivy.Container(a=ivy.array([1, 2]), b=ivy.array([]), c=ivy.native_array([4])) >>> y = 5 >>> z = x.one_hot(y) >>> print(z) { a: ivy.array([[0., 1., 0., 0., 0.], [0., 0., 1., 0., 0.]]), b: ivy.array([], shape=(0, 5)), c: ivy.array([[0., 0., 0., 0., 1.]]) }