【问题标题】:TfidfVectorizer NotFittedErrorTfidfVectorizer NotFittedError
【发布时间】:2016-11-17 21:20:11
【问题描述】:

我正在使用 sklearn Pipeline 和 FeatureUnion 从文本文件创建特征,我想打印出特征名称。

首先,我将所有转换收集到一个列表中。

In [225]:components
Out[225]: 
[TfidfVectorizer(analyzer=u'word', binary=False, decode_error=u'strict',
         dtype=<type 'numpy.int64'>, encoding=u'utf-8', input=u'content',
         lowercase=True, max_df=0.85, max_features=None, min_df=6,
         ngram_range=(1, 1), norm='l1', preprocessor=None, smooth_idf=True,
         stop_words='english', strip_accents=None, sublinear_tf=True,
         token_pattern=u'(?u)[#a-zA-Z0-9/\\-]{2,}',
         tokenizer=StemmingTokenizer(proc_type=stem, token_pattern=(?u)[a-zA-Z0-9/\-]{2,}),
         use_idf=True, vocabulary=None),
 TruncatedSVD(algorithm='randomized', n_components=150, n_iter=5,
        random_state=None, tol=0.0),
 TextStatsFeatures(),
 DictVectorizer(dtype=<type 'numpy.float64'>, separator='=', sort=True,
         sparse=True),
 DictVectorizer(dtype=<type 'numpy.float64'>, separator='=', sort=True,
         sparse=True),
 TfidfVectorizer(analyzer=u'word', binary=False, decode_error=u'strict',
         dtype=<type 'numpy.int64'>, encoding=u'utf-8', input=u'content',
         lowercase=True, max_df=0.85, max_features=None, min_df=6,
         ngram_range=(1, 2), norm='l1', preprocessor=None, smooth_idf=True,
         stop_words='english', strip_accents=None, sublinear_tf=True,
         token_pattern=u'(?u)[a-zA-Z0-9/\\-]{2,}',
         tokenizer=StemmingTokenizer(proc_type=stem, token_pattern=(?u)[a-zA-Z0-9/\-]{2,}),
         use_idf=True, vocabulary=None)]

例如第一个组件是一个 TfidfVectorizer() 对象。

components[0]
Out[226]: 
TfidfVectorizer(analyzer=u'word', binary=False, decode_error=u'strict',
        dtype=<type 'numpy.int64'>, encoding=u'utf-8', input=u'content',
        lowercase=True, max_df=0.85, max_features=None, min_df=6,
        ngram_range=(1, 1), norm='l1', preprocessor=None, smooth_idf=True,
        stop_words='english', strip_accents=None, sublinear_tf=True,
        token_pattern=u'(?u)[#a-zA-Z0-9/\\-]{2,}',
        tokenizer=StemmingTokenizer(proc_type=stem, token_pattern=(?u)[a-zA-Z0-9/\-]{2,}),
        use_idf=True, vocabulary=None)

type(components[0])
Out[227]: sklearn.feature_extraction.text.TfidfVectorizer

但是当我尝试使用 TfidfVectorizer 方法 get_feature_names 时,它会抛出 NotFittedError

components[0].get_feature_names()
Traceback (most recent call last):

  File "<ipython-input-228-0160deb904f5>", line 1, in <module>
    components[0].get_feature_names()

  File "C:\Users\fheng\AppData\Local\Continuum\Anaconda\lib\site-packages\sklearn\feature_extraction\text.py", line 903, in get_feature_names
    self._check_vocabulary()

  File "C:\Users\fheng\AppData\Local\Continuum\Anaconda\lib\site-packages\sklearn\feature_extraction\text.py", line 275, in _check_vocabulary
    check_is_fitted(self, 'vocabulary_', msg=msg),

  File "C:\Users\fheng\AppData\Local\Continuum\Anaconda\lib\site-packages\sklearn\utils\validation.py", line 678, in check_is_fitted
    raise NotFittedError(msg % {'name': type(estimator).__name__})

**NotFittedError: TfidfVectorizer - Vocabulary wasn't fitted.**

【问题讨论】:

    标签: python scikit-learn pipeline


    【解决方案1】:

    你在pipelinefeatureUnion 中使用过这个列表吗?你有没有在他们身上调用fit() 方法?

    这个错误是你没有调用fit()(即训练模型)并且直接尝试访问这些值。

    【讨论】:

    • 谢谢。这就是问题所在。
    • @Felicia 如果您满意,请接受答案和/或关闭问题
    • 显然我需要更多的声誉来结束这个问题。如何接受答案?
    • 点击此帖子下方左侧的复选标记。 (我认为它就在箭头的正下方)。作为问题的作者,您会在每个答案的左侧看到一个复选标记的轮廓。它将位于上下投票箭头的正下方。 meta.stackexchange.com/questions/23138/…
    猜你喜欢
    • 2018-05-18
    • 2019-01-17
    • 2018-01-23
    • 2019-01-17
    • 2020-10-19
    • 1970-01-01
    • 2019-12-13
    • 2016-03-28
    • 2021-05-26
    相关资源
    最近更新 更多