mxnet.npx.one_hot

one_hot(data, depth=None, on_value=1.0, off_value=0.0, dtype='float32')

Returns a one-hot array.

The locations represented by indices take value on_value, while all other locations take value off_value.

one_hot operation with indices of shape (i0, i1) and depth of d would result in an output array of shape (i0, i1, d) with:

output[i,j,:] = off_value
output[i,j,indices[i,j]] = on_value
Parameters
  • indices (NDArray) – array of locations where to set on_value

  • depth (long, required) – Depth of the one hot dimension.

  • on_value (double, optional, default=1) – The value assigned to the locations represented by indices.

  • off_value (double, optional, default=0) – The value assigned to the locations not represented by indices.

  • dtype ({'bfloat16', 'float16', 'float32', 'float64', 'int32', 'int64', 'int8', 'uint8'},) – optional, default=’float32’ DType of the output

Returns

out – The output of this function.

Return type

NDArray or list of NDArrays

Example

>>> data = np.array([1,0,2,0])
>>> npx.one_hot(data, 3)
array([[0., 1., 0.],
       [1., 0., 0.],
       [0., 0., 1.],
       [1., 0., 0.]], dtype=float64)
>>> npx.one_hot(data, 3, on_value=8, off_value=1, dtype='int32')
array([[1, 8, 1],
       [8, 1, 1],
       [1, 1, 8],
       [8, 1, 1]], dtype=int32)
>>> data = np.array([[1,0],[1,0],[2,0]])
>>> npx.one_hot(data, 3)
array([[[0., 1., 0.],
        [1., 0., 0.]],
       [[0., 1., 0.],
        [1., 0., 0.]],
       [[0., 0., 1.],
        [1., 0., 0.]]], dtype=float64)