CountVectorizer不打印词汇 [英] CountVectorizer does not print vocabulary
问题描述
我已经安装了python 2.7,numpy 1.9.0,scipy 0.15.1和scikit-learn 0.15.2. 现在,当我在python中执行以下操作时:
I have installed python 2.7, numpy 1.9.0, scipy 0.15.1 and scikit-learn 0.15.2. Now when I do the following in python:
train_set = ("The sky is blue.", "The sun is bright.")
test_set = ("The sun in the sky is bright.",
"We can see the shining sun, the bright sun.")
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer()
print vectorizer
CountVectorizer(analyzer=u'word', binary=False, charset=None,
charset_error=None, decode_error=u'strict',
dtype=<type 'numpy.int64'>, encoding=u'utf-8', input=u'content',
lowercase=True, max_df=1.0, max_features=None, min_df=1,
ngram_range=(1, 1), preprocessor=None, stop_words=None,
strip_accents=None, token_pattern=u'(?u)\\b\\w\\w+\\b',
tokenizer=None, vocabulary=None)
vectorizer.fit_transform(train_set)
print vectorizer.vocabulary
None.
实际上它应该已经打印了以下内容:
Actually it should have printed the following:
CountVectorizer(analyzer__min_n=1,
analyzer__stop_words=set(['all', 'six', 'less', 'being', 'indeed', 'over',
'move', 'anyway', 'four', 'not', 'own', 'through', 'yourselves', (...) --->
For count vectorizer
{'blue': 0, 'sun': 1, 'bright': 2, 'sky': 3} ---> for vocabulary
以上代码来自博客: http://blog.christianperone.com/?p=1589
The above code are from the blog: http://blog.christianperone.com/?p=1589
请问我为什么会收到这样的错误,请您帮我.由于词汇没有正确索引,因此我无法继续理解TF-IDF的概念.我是python的新手,所以我们将不胜感激.
Could you please help me as to why I get such an error. Since the vocabulary is not indexed properly I am not able to move ahead in understanding the concept of TF-IDF. I am a newbie for python so any help would be appreciated.
弧
推荐答案
您缺少下划线,请尝试以下方式:
You are missing an underscore, try this way:
from sklearn.feature_extraction.text import CountVectorizer
train_set = ("The sky is blue.", "The sun is bright.")
test_set = ("The sun in the sky is bright.",
"We can see the shining sun, the bright sun.")
vectorizer = CountVectorizer(stop_words='english')
document_term_matrix = vectorizer.fit_transform(train_set)
print vectorizer.vocabulary_
# {u'blue': 0, u'sun': 3, u'bright': 1, u'sky': 2}
如果使用ipython shell,则可以使用制表符补全,并且可以更轻松地找到对象的方法和属性.
If you use the ipython shell, you can use tab completion, and you can find easier the methods and attributes of objects.
这篇关于CountVectorizer不打印词汇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!