{ } Raw JSON

bundles / scipy latest / scipy / _lib / array_api_extra / _delegation / one_hot

function

scipy._lib.array_api_extra._delegation:one_hot

source: /scipy/_lib/array_api_extra/_delegation.py :498

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

Aliases

  • scipy.differentiate.xpx.one_hot