How to convert one-hot encodings into integers?

后端 未结 7 1457
夕颜
夕颜 2021-01-07 16:22

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

相关标签:
7条回答
  • 2021-01-07 17:24

    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)

    0 讨论(0)
提交回复
热议问题