Sklearn:有没有办法调试管道?

时间:2016-01-15 00:40:50

标签: python python-2.7 scikit-learn

我为分类任务创建了一些管道,我想查看每个阶段存在/存储的信息(例如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))
])

2 个答案:

答案 0 :(得分:6)

我发现有时临时添加一个打印出您感兴趣的信息的调试步骤很有用。在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)

答案 1 :(得分:0)

您可以使用stepsnamed_steps属性遍历Pipeline()树。前者是元组('step_name', Step())的列表,而后者则为您提供从此列表构建的字典

可以使用transformer_list属性

以相同的方式探索

FeatureUnion()内容