1

j'ai une liste de noms d'étiquettes que j'enuemrated et créé un dictionnaire:cartographie des valeurs cibles codées un chaud aux noms d'étiquettes appropriées

my_list = [b'airplane', 
b'automobile', 
b'bird', 
b'cat', 
b'deer', 
b'dog', 
b'frog', 
b'horse', 
b'ship', 
b'truck'] 

label_dict =dict(enumerate(my_list)) 


{0: b'airplane', 
1: b'automobile', 
2: b'bird', 
3: b'cat', 
4: b'deer', 
5: b'dog', 
6: b'frog', 
7: b'horse', 
8: b'ship', 
9: b'truck'} 

Maintenant, je suis en train de nettoyer map/apply la valeur dict à ma cible qui est sous une forme codée à chaud.

y_test[0] 

array([ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.]) 


y_test[0].map(label_dict) should return: 
'cat' 

Je jouais avec

(lambda key,value: value for y_test[0] == 1) 

mais ne pouvait pas venir avec une

béton

Merci.

Répondre

2

Puisque nous travaillons avec one-hot encoded tableau, argmax pourrait être utilisé pour obtenir l'index pour un off 1 pour chaque ligne. Ainsi, en utilisant la liste comme entrée -

[my_list[i] for i in y_test.argmax(1)] 

Ou avec np.take pour avoir la sortie de tableau -

np.take(my_list,y_test.argmax(1)) 

Pour travailler avec dict et en supposant des touches séquentielles 0,1,.., nous pourrions avoir -

np.take(label_dict.values(),y_test.argmax(1)) 

Si les touches ne sont pas essentiellement séquentielles mais triées -

np.take(label_dict.values(), np.searchsorted(label_dict.keys(),y_test.argmax(1))) 

run Exemple -

In [79]: my_list 
Out[79]: 
['airplane', 
'automobile', 
'bird', 
'cat', 
'deer', 
'dog', 
'frog', 
'horse', 
'ship', 
'truck'] 

In [80]: y_test 
Out[80]: 
array([[ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], 
     [ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], 
     [ 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.]]) 

In [81]: [my_list[i] for i in y_test.argmax(1)] 
Out[81]: ['cat', 'automobile', 'ship'] 

In [82]: np.take(my_list,y_test.argmax(1)) 
Out[82]: 
array(['cat', 'automobile', 'ship'], 
     dtype='|S10') 
3

nous pouvons utiliser le produit scalaire pour inverser le codage d'un chaud, si elle est vraiment UN -chaud.

Commençons par factoriser votre liste

f, u = pd.factorize(my_list) 

maintenant si vous avez un tableau que vous aimeriez retourner vos cordes avec

a = np.array([0, 0, 0, 1, 0, 0, 0, 0, 0, 0]) 

Ensuite, utilisez dot

a.dot(u) 

'cat' 

Supposons maintenant

y_test = np.array([ 
     [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], 
     [0, 1, 0, 0, 0, 0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0, 0, 0, 0, 1, 0] 
    ]) 

Puis

y_test.dot(u) 

array(['cat', 'automobile', 'ship'], dtype=object) 

Si ce n'est pas un chaud mais plutôt multi-chaud, vous pouvez joindre par des virgules

y_test = np.array([ 
     [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], 
     [0, 1, 0, 0, 0, 0, 0, 0, 0, 1], 
     [0, 0, 1, 0, 0, 0, 0, 0, 1, 0] 
    ]) 

[', '.join(u[y.astype(bool)]) for y in y_test] 


['cat', 'automobile, truck', 'bird, ship']