由于我们正在与one-hot encoded
array, argmax
可用于一次性获取索引1
对于每一行。因此,使用列表作为输入 -
[my_list[i] for i in y_test.argmax(1)]
Or with np.take
有数组输出 -
np.take(my_list,y_test.argmax(1))
跟...共事dict
并假设顺序键为0,1,..
, 我们可以有 -
np.take(label_dict.values(),y_test.argmax(1))
如果键本质上不是按顺序排列而是排序的 -
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')