首页 > 解决方案 > 检查“已安装的 sk-learn”管道仍会导致“尚未安装 TFIdfVectorizer”

问题描述

这是我对 sk-learn 管道的不安全感。每当我在 sk-learn 中创建一个流水线并用这个流水线做一些预测时,我似乎遇到了一个问题,即我实际上无法检查流水线的中间步骤。预测有效,我得到了我的分数,但是如果我想获得实例的“特征重要性”,或者检查 tf-idf 矢量化器的特征是什么,则声称该管道不适合(尽管它只是最近用于推理,我已经要求对其进行培训)。

举个例子,.fit()这里调用 Scikit-learn 文档中的以下片段可用于预测,但是当我想检查管道的 tfidf 时,它声称同样的不合适的问题。

pipeline = Pipeline([
    # Extract the subject & body
    ('subjectbody', SubjectBodyExtractor()),

    # Use ColumnTransformer to combine the features from subject and body
    ('union', ColumnTransformer(
        [
            # Pulling features from the post's subject line (first column)
            ('subject', TfidfVectorizer(min_df=50), 0),

            # Pipeline for standard bag-of-words model for body (second column)
            ('body_bow', Pipeline([
                ('tfidf', TfidfVectorizer()),
                ('best', TruncatedSVD(n_components=50)),
            ]), 1),

            # Pipeline for pulling ad hoc features from post's body
            ('body_stats', Pipeline([
                ('stats', TextStats()),  # returns a list of dicts
                ('vect', DictVectorizer()),  # list of dicts -> feature matrix
            ]), 1),
        ],

        # weight components in ColumnTransformer
        transformer_weights={
            'subject': 0.8,
            'body_bow': 0.5,
            'body_stats': 1.0,
        }
    )),

    # Use a SVC classifier on the combined features
    ('svc', LinearSVC(dual=False)),
], verbose=True)

在数据上拟合管道之后(如链接中所做的那样),当我尝试使用

pipeline.named_steps.union.transformers[1][1].named_steps['tfidf'].get_feature_names() 

它声称“未安装或提供词汇表”。

那么,这是我对管道的误解吗?我们不应该访问中间步骤吗?或者可能需要设置一个设置?

标签: pythonmachine-learningscikit-learndata-science

解决方案


您需要通过.transformers_ So访问变压器pipeline.named_steps.union.transformers_[1][1].named_steps['tfidf'].get_feature_names()


推荐阅读