简体   繁体   English

如何将数组“翻译”为 label?

[英]How to "translate" array to label?

After predicting a certain image I got the following classes:在预测了某个图像后,我得到了以下类:

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

I now want to translate the classes to the corresponding letters numbers.我现在想将课程翻译成相应的字母数字。 To onehot encode my classes before I used this code (in order to see which class stands for which letter/number:在我使用此代码之前对我的课程进行 onehot 编码(以便查看哪个 class 代表哪个字母/数字:

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

That means: class 1 is equal to number 1 , class 10 to A and so on.这意味着: class 1等于数字1 , class 10等于A等等。 How can I invert this and get the array to a new label?我如何反转它并将数组获取到新的 label?

Thanks a lot in advance.非常感谢。

Not sure I understand the problem, but this might work?不确定我是否理解这个问题,但这可能有效吗?

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

output: output:

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

If you want it as a string:如果你想要它作为一个字符串:

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

output: output:

'1AS8266'
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]))

I got the following output:我得到以下 output:

1BK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM