如何给"translate"数组添加标签?

How to "translate" array to label?

在预测了某个图像后,我得到了以下 classes:

np.argmax(classes, axis=2)
array([[ 1, 10, 27,  8,  2,  6,  6]])

我现在想将 classes 翻译成相应的字母数字。在使用此代码之前对我的 classes 进行 onehot 编码(以便查看哪个 class 代表哪个 letter/number:

def my_onehot_encoded(label):
    # define universe of possible input values
    characters = '0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ'
    # define a mapping of chars to integers
    char_to_int = dict((c, i) for i, c in enumerate(characters))
    int_to_char = dict((i, c) for i, c in enumerate(characters))
    # integer encode input data
    integer_encoded = [char_to_int[char] for char in label]
    # one hot encode
    onehot_encoded = list()
    for value in integer_encoded:
        character = [0 for _ in range(len(characters))]
        character[value] = 1
        onehot_encoded.append(character)

    return onehot_encoded

表示:class1等于数1,class10A等。我怎样才能反转它并将数组添加到新标签?

非常感谢。

def my_onehot_encoded(classes_array):
    # define universe of possible input values
    characters = '0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ'
    
    return "".join([characters[c] for c in classes_array])

print(my_onehot_encoded([1, 11, 20]))

我得到以下输出:

1BK

不确定我是否理解这个问题,但这可能有效吗?

import numpy as np
a = np.array([[ 1, 10, 27,  8,  2,  6,  6]])
characters = '0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ'
np.array(list(characters))[a]

输出:

array([['1', 'A', 'S', '8', '2', '6', '6']], dtype='<U1')

如果你想要它作为一个字符串:

"".join(np.array(list(characters))[a].flat)

输出:

'1AS8266'