生成单热编码的字符串表示(Generate a string representation of a one

编程入门 行业动态 更新时间:2024-10-25 01:30:51
生成单热编码的字符串表示(Generate a string representation of a one-hot encoding)

在Python中,我需要生成一个将字母映射到该字母的预定义“ 单热 ”表示的dict 。 举例来说, dict应该是这样的:

{ 'A': '1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0', 'B': '0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0', # ... }

每个字母表中有一位(表示为字符)。 因此,每个字符串将包含25个零和一个1. 1的位置由字母表中相应字母的位置决定。

我想出了一些产生这种情况的代码:

# Character set is explicitly specified for fine grained control _letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" n = len(_letters) one_hot = [' '.join(['0']*a + ['1'] + ['0']*b) for a, b in zip(range(n), range(n-1, -1, -1))] outputs = dict(zip(_letters, one_hot))

是否有更高效/更清洁/更pythonic的方式来做同样的事情?

In Python, I need to generate a dict that maps a letter to a pre-defined "one-hot" representation of that letter. By way of illustration, the dict should look like this:

{ 'A': '1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0', 'B': '0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0', # ... }

There is one bit (represented as a character) per letter of the alphabet. Hence each string will contain 25 zeros and one 1. The position of the 1 is determined by the position of the corresponding letter in the alphabet.

I came up with some code that generates this:

# Character set is explicitly specified for fine grained control _letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" n = len(_letters) one_hot = [' '.join(['0']*a + ['1'] + ['0']*b) for a, b in zip(range(n), range(n-1, -1, -1))] outputs = dict(zip(_letters, one_hot))

Is there a more efficient/cleaner/more pythonic way to do the same thing?

最满意答案

我觉得这更可读:

from string import ascii_uppercase one_hot = {} for i, l in enumerate(ascii_uppercase): bits = ['0']*26; bits[i] = '1' one_hot[l] = ' '.join(bits)

如果您需要更一般的字母表,只需枚举字符串,并用['0']*len(alphabet)替换['0']*26 。

I find this to be more readable:

from string import ascii_uppercase one_hot = {} for i, l in enumerate(ascii_uppercase): bits = ['0']*26; bits[i] = '1' one_hot[l] = ' '.join(bits)

If you need a more general alphabet, just enumerate over a string of the characters, and replace ['0']*26 with ['0']*len(alphabet).

更多推荐

本文发布于:2023-08-04 11:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415934.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   Generate   representation   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!