This is a pre-release version (2.0.0.dev0+git20260907.41eeb59).Go to latest (1.18.0)
{ } Raw JSON

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) → 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 : array

An array with integral dtype whose values are between 0 and num_classes - 1.

num_classes : int

Number of classes in the one-hot dimension.

dtype : DType, optional

The dtype of the return value. Defaults to the default float dtype (usually float64).

axis : int, optional

Position in the expanded axes where the new axis is placed. Default: -1.

xp : array_namespace, optional

The standard-compatible namespace for x. Default: infer.

Returns

: array

An array having the same shape as x except for a new axis at the position given by axis having size num_classes. If axis is unspecified, it defaults to -1, which appends a new axis.

If x < 0 or x >= num_classes, then the result is undefined, may raise an exception, or may even cause a bad state. x is 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