【发布时间】:2017-11-20 15:06:06
【问题描述】:
我试图用数字表示一组单词。到目前为止我有这个代码:
from sklearn.preprocessing import OneHotEncoder
import itertools
docs = ["select", "max", "income", "from", "data", "where", "revenue", "between", "20", "40"]
# split documents to tokens
tokens_docs = [doc.split(" ") for doc in docs]
# convert list of of token-lists to one flat list of tokens
# and then create a dictionary that maps word to id of word,
# like {A: 1, B: 2} here
all_tokens = itertools.chain.from_iterable(tokens_docs)
word_to_id = {token: idx for idx, token in enumerate(set(all_tokens))}
但是,有一个限制 - 当令牌本身已经是一个数字时,我需要分配与数字相同的值(在 word_to_id 字典中)。 有什么建议吗?
【问题讨论】:
标签: python nlp one-hot-encoding