【发布时间】:2020-06-23 03:10:04
【问题描述】:
我有以下示例:我希望将每个序列列表列表转换为一个热编码器。
例如,我有一个包含 2 个句子的列表。我首先将这些句子转换为序列列表。
然后对于序列列表的每个列表,然后我根据每个单词将序列转换为一个热门。
from nltk.tokenize import word_tokenize
from itertools import chain
from keras.preprocessing.sequence import pad_sequences
a = ['hi', 'oh thanks i m fine this is an evening in my timezone']
a_tokens = [word_tokenize(word) for word in a]
tokens_dict = {word:i for i, word in enumerate(set(chain.from_iterable(a_tokens)))}
tokens_sequence = [[tokens_dict[word_t] for word_t in word] for word in a_tokens]
当前输出:
[[4], [2, 5, 3, 1, 8, 7, 9, 0, 12, 10, 11, 6]]
预期输出:
[[[0,0,0,0,0,0,0,0,0,0,0,0,1]],
[[-12 0s but 1 for the repsective word-],
[-12 0s but 1 for the repsective word-],
[-12 0s but 1 for the repsective word-],
[-12 0s but 1 for the repsective word-],
[-12 0s but 1 for the repsective word-],
[-12 0s but 1 for the repsective word-],
[-12 0s but 1 for the repsective word-],
[-12 0s but 1 for the repsective word-],
[-12 0s but 1 for the repsective word-],
[-12 0s but 1 for the repsective word-],
[-12 0s but 1 for the repsective word-],
[-12 0s but 1 for the repsective word-]]]
【问题讨论】:
标签: python scikit-learn one-hot-encoding