[[0. 0. 0. 0. 0. 1.]
[0. 1. 0. 0. 0. 0.]
[1. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 1.]]
import numpy as np
arr = np.array([5, 1, 0, 1, 2])
one_hot_encoded = np.zeros((arr.size, arr.max() + 1))
one_hot_encoded[np.arange(arr.size), arr] = 1
print(one_hot_encoded)
import numpy as np
arr = np.array([5, 1, 0, 1, 2])
n_values = np.max(arr) + 1
one_hot_encoded = np.eye(n_values)[arr]
print(one_hot_encoded)
[[0. 0. 0. 0. 0. 1.]
[0. 1. 0. 0. 0. 0.]
[1. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0.]]