如何“翻译”数组来标记?

发布于 2025-01-11 11:57:10 字数 948 浏览 1 评论 0原文

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

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

我现在想将这些类转换为相应的字母数字。在使用此代码之前对我的类进行onehot编码(以便查看哪个类代表哪个字母/数字:

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

这意味着:类1等于数字1,类10A 等等,我如何反转它并将数组转换为新标签

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:

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. How can I invert this and get the array to a new label?

Thanks a lot in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心意如水 2025-01-18 11:57:10

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

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'

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:

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:

'1AS8266'
生寂 2025-01-18 11:57:10
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
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:

1BK
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文