【问题标题】:One hot Encoding at character Level字符级别的一种热编码
【发布时间】: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


【解决方案1】:

可以直接使用 Keras 提供的单例编码功能。像这样的:

import numpy as np
from keras.utils import np_utils
y_train_label = [7,4,11,11,14]
y_train_label_onehot = np_utils.to_categorical(y_train_label)
print('one_hot:',y_train_label_onehot)

结果:

【讨论】: