我正在阅读一本关于 Scikit-learn 的教程书,其中一个部分包含以下代码块:
from sklearn.feature_extraction.text import CountVectorizer
corpus = ['The dog ate a sandwich, the wizard transfigured a sandwich, and I ate a sandwich']
vectorizer = CountVectorizer(stop_words='english')
print vectorizer.fit_transform(corpus).todense()
当我运行它时,我得到这个:
[[2 1 3 1 1]]
当我应该得到这两个时:
[[2 1 3 1 1]]
{u'sandwich': 2, u'wizard': 4, u'dog': 1, u'transfigured': 3, u'ate': 0}
如何更改代码以获取被向量化的每个单词的实际单词和数量,而不仅仅是向量本身?
最佳答案
模型拟合后,您将访问 .vocabulary_
属性:
>>> vectorizer.vocabulary_
{'ate': 0, 'dog': 1, 'sandwich': 2, 'transfigured': 3, 'wizard': 4}
关于python - 如何显示每个被向量化的单词的实际数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37706396/