【发布时间】:2025-12-01 14:25:02
【问题描述】:
所以我关注了这篇文章中提供的一些示例:How to one-hot-encode sentences at the character level?
而且它们似乎在字符级别进行热编码。但是,我无法弄清楚在字符级别对包含整数的字符串进行热编码。
例如:
"hello" # h=7, e=4 l=11 o=14
应该是:
[[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 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, 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, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
在我上面提到的帖子的帮助下,我能够做到这一点。但是有人可以帮我对以下内容进行热编码:
"Hello0311"
非常感谢任何帮助和指导
【问题讨论】:
-
在接受的答案中提供的函数中,只需将
alphabet=string.ascii_lowercase替换为alphabet=string.digits+string.ascii_letters即可。 -
@squeamishossifrage 非常感谢!
标签: python scikit-learn keras