bundles / scipy latest / scipy / _lib / array_api_extra / _delegation / one_hot
function
scipy._lib.array_api_extra._delegation:one_hot
Signature
def one_hot ( x : Array , / , num_classes : int , * , dtype : DType | None = None , axis : int = -1 , xp : ModuleType | None = None ) → Array Summary
One-hot encode the given indices.
Extended Summary
Each index in the input x is encoded as a vector of zeros of length num_classes with the element at the given index set to one.
Parameters
x: arrayAn array with integral dtype whose values are between
0andnum_classes - 1.num_classes: intNumber of classes in the one-hot dimension.
dtype: DType, optionalThe dtype of the return value. Defaults to the default float dtype (usually float64).
axis: int, optionalPosition in the expanded axes where the new axis is placed. Default: -1.
xp: array_namespace, optionalThe standard-compatible namespace for
x. Default: infer.
Returns
: arrayAn array having the same shape as
xexcept for a new axis at the position given byaxishaving sizenum_classes. Ifaxisis unspecified, it defaults to -1, which appends a new axis.If
x < 0orx >= num_classes, then the result is undefined, may raise an exception, or may even cause a bad state.xis not checked.
Examples
import array_api_extra as xpx import array_api_strict as xp xpx.one_hot(xp.asarray([1, 2, 0]), 3)⚠
Aliases
-
scipy.differentiate.xpx.one_hot