python - TfidfVectorizer NotFittedError

标签 python scikit-learn pipeline

我正在使用 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.**

最佳答案

您是否在pipelinefeatureUnion 中使用过此列表?您是否对它们调用了 fit() 方法?

此错误是您没有调用 fit()(即训练模型)并直接尝试访问值。

关于python - TfidfVectorizer NotFittedError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38382981/

相关文章:

machine-learning - 摘录 为什么在分类时选择标签?

python - OneHotEncoder 对属于同一类别的多个列进行处理

Python 瓶颈;确定文件比较功能的最佳 block 大小

python - 使用模块键盘输出重复

python - 填充图像以在 wxpython 中使用

collections - 是什么决定了Powershell管道是否将展开一个集合?

Azure DevOps Pipeline 条件脚本参数

python - 对于服务器端 python 代码,是否有比 virtualenv 更好的解决方案?

python - 在经过训练的文档集上计算查询字符串的 TF-IDF

powershell - 为什么我不能在 write-host 中使用 $_?