2016-01-15 64 views
6

我爲分類任務創建了一些管道,我想查看每個階段存在/存儲的信息(例如,text_stats,ngram_tfidf)。我怎麼能這樣做。Sklearn:有什麼方法可以調試管道?

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)) 
]) 

回答

0

可以使用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) 
相關問題