Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get intermediate data state in scikit-learn Pipeline

Tags:

Given the following example:

from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.decomposition import NMF from sklearn.pipeline import Pipeline import pandas as pd  pipe = Pipeline([     ("tf_idf", TfidfVectorizer()),     ("nmf", NMF()) ])  data = pd.DataFrame([["Salut comment tu vas", "Hey how are you today", "I am okay and you ?"]]).T data.columns = ["test"]  pipe.fit_transform(data.test) 

I would like to get intermediate data state in scikit learn pipeline corresponding to tf_idf output (after fit_transform on tf_idf but not NMF) or NMF input. Or to say things in another way, it would be the same than to apply

TfidfVectorizer().fit_transform(data.test) 

I know pipe.named_steps["tf_idf"] ti get intermediate transformer, but I can't get data, only parameters of the transformer with this method.

like image 569
thibaultbl Avatar asked Feb 12 '18 09:02

thibaultbl


1 Answers

As @Vivek Kumar suggested in the comment and as I answered here, I find a debug step that prints information or writes intermediate dataframes to csv useful:

from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.decomposition import NMF from sklearn.pipeline import Pipeline import pandas as pd from sklearn.base import TransformerMixin, BaseEstimator   class Debug(BaseEstimator, TransformerMixin):      def transform(self, X):         print(X.shape)         self.shape = shape         # what other output you want         return X      def fit(self, X, y=None, **fit_params):         return self  pipe = Pipeline([     ("tf_idf", TfidfVectorizer()),     ("debug", Debug()),     ("nmf", NMF()) ])  data = pd.DataFrame([["Salut comment tu vas", "Hey how are you today", "I am okay and you ?"]]).T data.columns = ["test"]  pipe.fit_transform(data.test) 

Edit

I now added a state to the debug transformer. Now you can access the shape as in the answer by @datasailor with:

pipe.named_steps["debug"].shape 
like image 89
Marcus V. Avatar answered Sep 25 '22 03:09

Marcus V.