【问题标题】:retrieve intermediate features from a pipeline in Scikit (Python)从 Scikit (Python) 中的管道中检索中间特征
【发布时间】:2016-01-10 05:00:18
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: python scikit-learn pipeline


    【解决方案1】:

    使用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()会返回一个错误。

    【讨论】:

      【解决方案2】:

      仅供参考

      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

      【讨论】:

        猜你喜欢
        • 2016-05-24
        • 2016-06-23
        • 1970-01-01
        • 2016-11-30
        • 2019-02-14
        • 2021-07-15
        • 2016-01-27
        • 2014-07-30
        • 2016-08-09
        相关资源
        最近更新 更多