【发布时间】:2019-08-06 13:07:36
【问题描述】:
我正在使用CountVectorizer 来获取字符串列表中的单词列表
from sklearn.feature_extraction.text import CountVectorizer
raw_text = [
'The dog hates the black cat',
'The black dog is good'
]
raw_text = [x.lower() for x in raw_text]
vocabulary = vectorizer.vocabulary_
vocabulary = dict((v, k) for k, v in vocabulary.iteritems())
vocabulary
在词汇表中,我有以下数据,这些数据是正确的
{0: u'black', 1: u'cat', 2: u'dog', 3: u'good', 4: u'hates', 5: u'is', 6: u'the'}
我现在想要获得的是“映射”到这些新值的原始句子,例如:
expected_output = [
[6, 2, 4, 6, 0, 1],
[6, 0, 2, 5, 3]
]
我尝试探索 Sklearn 文档,但我真的找不到任何似乎可以做到这一点的东西,而且我什至不知道我正在尝试执行的操作的正确术语,所以我在 Google 中找不到任何结果。
有没有办法达到这个效果?
【问题讨论】:
标签: python python-3.x python-2.7 machine-learning scikit-learn