将一键编码的目标值映射到正确的标签名称 [英] Mapping one-hot encoded target values to proper label names
问题描述
我有一个枚举的标签名称列表,并创建了一个字典:
I have a list of label names which I enuemrated and created a dictionary:
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'}
现在,我正在尝试将dict值map
/apply
清除到目标文件中,该目标文件格式为一键编码.
Now I'm trying to cleaning map
/apply
the dict value to my target which is in an one-hot-encoded form.
y_test[0]
array([ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.])
y_test[0].map(label_dict) should return:
'cat'
我在玩
(lambda key,value: value for y_test[0] == 1)
但无法提出任何具体建议
but couldn't come up with any concrete
谢谢.
推荐答案
由于我们正在使用one-hot encoded
数组,因此argmax
可用于为每行从1
获取一个索引.因此,使用列表作为输入-
Since we are working with one-hot encoded
array, argmax
could be used to get the index for one off 1
for each row. Thus, using the list as input -
[my_list[i] for i in y_test.argmax(1)]
或使用np.take
具有数组输出-
np.take(my_list,y_test.argmax(1))
要使用dict
并将连续键假定为0,1,..
,我们可以使用-
To work with dict
and assuming sequential keys as 0,1,..
, we could have -
np.take(label_dict.values(),y_test.argmax(1))
如果键本质上不是按顺序排列的,而是排序的-
If the keys are not essentially in sequence but sorted -
np.take(label_dict.values(), np.searchsorted(label_dict.keys(),y_test.argmax(1)))
样品运行-
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')
这篇关于将一键编码的目标值映射到正确的标签名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!