2016-01-15 9 views
6

Ho creato alcune pipeline per l'attività di classificazione e voglio verificare quali informazioni sono presenti/memorizzate in ogni fase (ad esempio text_stats, ngram_tfidf). Come potrei farlo.Sklearn: Esiste un modo per eseguire il debug di Pipelines?

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

risposta

0

È possibile attraversare il vostro Pipeline() dell'albero utilizzando steps e named_steps attributi. Il primo è una lista di tuple ('step_name', Step()) mentre il secondo si dà un dizionario costruito da questa lista

FeatureUnion() contenuto potrebbe essere esplorata allo stesso modo mediante l'attributo transformer_list

2

trovo a volte utili per aggiungere temporaneamente un passo di debug che stampa le informazioni che ti interessano. Basandosi sull'esempio dall'esempio sklearn 1, puoi farlo ad esempio per stampare le prime 5 linee, la forma o qualsiasi altra cosa che devi vedere prima che il classificatore sia chiamato:

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)