Answers for "1d array to one hot"

1

one hot encoding numpy

labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)
Posted by: Guest on March-12-2020
0

1d array to one hot

>>> a = np.array([1, 0, 3])
>>> b = np.zeros((a.size, a.max()+1))
>>> b[np.arange(a.size),a] = 1
>>> b
array([[ 0.,  1.,  0.,  0.],
       [ 1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  1.]])
Posted by: Guest on May-02-2020

Python Answers by Framework

Browse Popular Code Answers by Language