从 Scikit 中的管道检索中间特征 (Python)

retrieve intermediate features from a pipeline in Scikit (Python)

我使用的管道与给定的管道非常相似 in this example :

>>> text_clf = Pipeline([('vect', CountVectorizer()),
...                      ('tfidf', TfidfTransformer()),
...                      ('clf', MultinomialNB()),
... ])

我使用 GridSearchCV 在参数网格上找到最佳估计量。

但是,我想使用 CountVectorizer() 中的 get_feature_names() 方法获取训练集的列名。如果不在管道外实施 CountVectorizer() 这可能吗?

使用get_params()函数,您可以访问管道的各个部分及其各自的内部参数。这是访问 'vect'

的示例
text_clf = Pipeline([('vect', CountVectorizer()),
                     ('tfidf', TfidfTransformer()),
                     ('clf', MultinomialNB())]
print text_clf.get_params()['vect']

产量(对我来说)

CountVectorizer(analyzer=u'word', binary=False, 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)

在此示例中我没有将管道安装到任何数据,因此此时调用 get_feature_names() 将 return 出错。

仅供参考

The estimators of a pipeline are stored as a list in the steps attribute:
>>>

>>> clf.steps[0]
('reduce_dim', PCA(copy=True, n_components=None, whiten=False))

and as a dict in named_steps:
>>>

>>> clf.named_steps['reduce_dim']
PCA(copy=True, n_components=None, whiten=False)

来自 http://scikit-learn.org/stable/modules/pipeline.html