bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / _external / array_api_extra / _delegation / one_hot
function
scipy._external.array_api_extra._delegation:one_hot
source: build-install/usr/lib/python3.14/site-packages/scipy/_external/array_api_extra/_delegation.py:872
Signature
def one_hot(x: Array, /, num_classes: int, *, dtype: DType | None = None, axis: int = -1, xp: ArrayNamespace | None = None) → ArraySummary
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)⚠
Traceback (most recent call last):
File "<doctest scipy._external.array_api_extra._delegation:one_hot[0]>", line 1, in <module>
xpx.one_hot(xp.asarray([1, 2, 0]), 3)
^^^
NameError: name 'xpx' is not defined
Aliases
scipy.differentiate.xpx.one_hot