How to convert one-hot encodings into integers?

后端 未结 7 1521
夕颜
夕颜 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:12

    You can use numpy.argmax or tf.argmax. Example:

    import numpy as np  
    a  = np.array([[0,1,0,0],[1,0,0,0],[0,0,0,1]])
    print('np.argmax(a, axis=1): {0}'.format(np.argmax(a, axis=1)))
    

    output:

    np.argmax(a, axis=1): [1 0 3]
    

    You may also want to look at sklearn.preprocessing.LabelBinarizer.inverse_transform.

提交回复
热议问题