【问题标题】:Sklearn: Is there any way to debug Pipelines?Sklearn:有没有办法调试管道?
【发布时间】:2016-04-20 12:56:19
【问题描述】:

我已经为分类任务创建了一些管道,我想检查每个阶段存在/存储的信息(例如 text_stats、ngram_tfidf)。我怎么能这样。

pipeline = Pipeline([
    ('features',FeatureUnion([
                ('text_stats', Pipeline([
                            ('length',TextStats()),
                            ('vect', DictVectorizer())
                        ])),
                ('ngram_tfidf',Pipeline([
                            ('count_vect', CountVectorizer(tokenizer=tokenize_bigram_stem,stop_words=stopwords)),
                            ('tfidf', TfidfTransformer())
                        ]))
            ])),   
    ('classifier',MultinomialNB(alpha=0.1))
])

【问题讨论】:

    标签: python python-2.7 scikit-learn


    【解决方案1】:

    您可以使用stepsnamed_steps 属性遍历您的Pipeline() 树。前者是一个元组列表('step_name', Step()),而后者给你一个从这个列表构造的字典

    FeatureUnion() 内容可以使用 transformer_list 属性以相同的方式探索

    【讨论】:

      【解决方案2】:

      我发现临时添加一个打印出您感兴趣的信息的调试步骤有时很有用。在 sklearn 示例 1 的示例之上构建,您可以这样做以打印出在调用分类器之前,前 5 行、形状或您需要查看的任何内容:

      from sklearn import svm
      from sklearn.datasets import samples_generator
      from sklearn.feature_selection import SelectKBest
      from sklearn.feature_selection import f_regression
      from sklearn.pipeline import Pipeline
      from sklearn.base import TransformerMixin, BaseEstimator
      
      class Debug(BaseEstimator, TransformerMixin):
      
          def transform(self, X):
              print(pd.DataFrame(X).head())
              print(X.shape)
              return X
      
          def fit(self, X, y=None, **fit_params):
              return self
      
      X, y = samples_generator.make_classification(n_informative=5, n_redundant=0, random_state=42)
      anova_filter = SelectKBest(f_regression, k=5)
      clf = svm.SVC(kernel='linear')
      anova_svm = Pipeline([('anova', anova_filter), ('dbg', Debug()), ('svc', clf)])
      anova_svm.set_params(anova__k=10, svc__C=.1).fit(X, y)
      
      prediction = anova_svm.predict(X)
      

      【讨论】:

        猜你喜欢
        • 2020-10-07
        • 2020-08-13
        • 1970-01-01
        • 2021-11-07
        • 2022-07-08
        • 2021-03-28
        • 2016-02-08
        • 2010-12-24
        • 2020-12-13
        相关资源
        最近更新 更多