python numpy配列とone-hot符号化相互変換

1180 ワード

a=[0,0,1,0,1,0,1]

result=[]
for i, x in enumerate(a):
    if x==1:
        result.append(i)

print(result)

 
python numpy配列とone-hot符号化相互変換
2018年09月18日19:54:20姚賢読数:92ラベル:one-hot符号化one-hot配列変換one-hotkeras更多
個人分類:機械学習
本文はブロガーのオリジナル文章で、ブロガーの許可を得ずに転載してはならない.https://blog.csdn.net/u011311291/article/details/82763093
import numpy as np
from keras.utils import to_categorical

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 7]
data = array(data)
print(data)
# [1 2 3 4 5 6 7 8 9 7]

#   np     one-hot
one_hots = to_categorical(data)
print(one_hots)
# [[ 0.  1.  0.  0.  0.  0.  0.  0.  0.  0.]
#  [ 0.  0.  1.  0.  0.  0.  0.  0.  0.  0.]
#  [ 0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]
#  [ 0.  0.  0.  0.  1.  0.  0.  0.  0.  0.]
#  [ 0.  0.  0.  0.  0.  1.  0.  0.  0.  0.]
#  [ 0.  0.  0.  0.  0.  0.  1.  0.  0.  0.]
#  [ 0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]
#  [ 0.  0.  0.  0.  0.  0.  0.  0.  1.  0.]
#  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  1.]
#  [ 0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]]

#  one-hot     np  
data = [argmax(one_hot)for one_hot in one_hots]
print(data)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 7]