I have a numpy array data set with shape (100,10). Each row is a one-hot encoding. I want to transfer it into a nd-array with shape (100,) such that I transferred each vecto
What I do in these cases is something like this. The idea is to interpret the one-hot vector as an index of a 1,2,3,4,5... array.
# Define stuff
import numpy as np
one_hots = np.zeros([100,10])
for k in range(100):
one_hots[k,:] = np.random.permutation([1,0,0,0,0,0,0,0,0,0])
# Finally, the trick
ramp = np.tile(np.arange(0,10),[100,1])
integers = ramp[one_hots==1].ravel()
I prefer this trick because I feel np.argmax
and other suggested solutions may be slower than indexing (although indexing may consume more memory)