【问题标题】:CountVectorizer does not print vocabularyCountVectorizer 不打印词汇表
【发布时间】:2015-05-07 19:41:03
【问题描述】:

我已经安装了 python 2.7、numpy 1.9.0、scipy 0.15.1 和 scikit-learn 0.15.2。 现在,当我在 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.

其实应该是这样打印出来的:

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

请您帮助我了解为什么会出现这样的错误。由于词汇表没有正确索引,我无法继续理解 TF-IDF 的概念。我是 python 的新手,所以任何帮助将不胜感激。

弧线。

【问题讨论】:

  • vectorizer.vocabulary_ 带下划线就是你想要的。 vectorizer.vocabulary 不是你想要的,如果你传入了 in,它就是词汇表,如果有的话(通常是 None)。

标签: python numpy scikit-learn scipy countvectorizer


【解决方案1】:

你少了一个下划线,试试这个方法:

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,可以使用tab补全,可以更方便的找到对象的方法和属性。

【讨论】:

  • 是的。谢谢。我将尝试使用 ipython shell,这样我就不会错过此类选项卡完成。我有 ipython shell 从来不知道这一点。感谢您提供信息。
  • 在上面我还问过我的停用词=None in CountVectorize 怎么会不应该这样。
  • stop_words 的默认值为无。如果您想使用内置的英语停用词,您可以像这样创建矢量化器:vectorizer = CountVectorizer(stop_words='english')。
  • 谢谢。我认为停用词是内置在函数中的。
  • 要澄清一下,vocabulary_ 函数是否按字母顺序生成索引词。即在上面的例子中,'blue' 得到 0 并且 1 被赋予 'bright' 按字母顺序排列的下一个术语?
【解决方案2】:

尝试使用vectorizer.get_feature_names() 方法。它按照在document_term_matrix 中出现的顺序给出列名。

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)
vectorizer.get_feature_names()
#> ['blue', 'bright', 'sky', 'sun']

【讨论】:

    猜你喜欢
    • 2015-12-16
    • 2016-09-25
    • 2020-01-15
    • 2019-08-29
    • 2019-03-14
    • 2017-09-21
    • 2018-12-12
    • 1970-01-01
    • 2020-02-09
    相关资源
    最近更新 更多