title: How to extract the word features in trained model of MultinomialNB Pipeline in scikit-learn?
body: <pre><code># Note: The runnable code example is at the end of this question ####
  # Assume X_train contains cleaned sentence text as input data. Y_train are class labels. 
  # parameters stores the parameter to be tried by GridSearchCV
  
  text_clf_Pipline_MultinomialNB = Pipeline([('vect', CountVectorizer()),
                                             ('tfidf', TfidfTransformer()),
                                             ('clf', MultinomialNB()),                     
                                            ])
  gs_clf = GridSearchCV(text_clf_Pipline_MultinomialNB, parameters, n_jobs=-1)   
  gs_classifier = gs_clf.fit(X_train, y_train)
  </code></pre>
  
  <p>Now I can get the feature_log_prob_ from the gs_classifier based on <a href="http://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.MultinomialNB.html" rel="nofollow">sklearn.naive_bayes.MultinomialNB documentation</a>. Here is an example. </p>
  
  <p>My question is how to get the word correspond to each log probability? 
  The CountVectorizer() and TfidfTransformer() both did feature selection. 
  Where do the GridSearchCV Object stores the selected word/ngram features? How to get match them back to the probabilities? </p>
  
  <p>I have inspected the members of gs_classifier, while have not found the selected features. Thanks. </p>
  
  <p>The following is a runnable example:</p>
  
  <pre><code>from sklearn.feature_extraction.text import CountVectorizer
  from sklearn.feature_extraction.text import TfidfTransformer
  from sklearn.grid_search import GridSearchCV
  from sklearn.pipeline import Pipeline
  from sklearn.naive_bayes import MultinomialNB
  from inspect import getmembers
  
  X_train = ['qwe rtyuiop', 'asd fghj kl', 'zx cv bnm', 'qw erty ui op', 'as df ghj kl', 'zxc vb nm', 'qwe rt yu iop', 'asdfg hj kl', 'zx cvb nm',
            'qwe rt yui op', 'asd fghj kl', 'zx cvb nm', 'qwer tyui op', 'asd fg hjk l', 'zx cv b nm', 'qw ert yu iop', 'as df gh jkl', 'zx cvb nm',
             'qwe rty uiop', 'asd fghj kl', 'zx cvbnm', 'qw erty ui op', 'as df ghj kl', 'zxc vb nm', 'qwe rtyu iop', 'as dfg hj kl', 'zx cvb nm',
            'qwe rt yui op', 'asd fg hj kl', 'zx cvb nm', 'qwer tyuiop', 'asd fghjk l', 'zx cv b nm', 'qw ert yu iop', 'as df gh jkl', 'zx cvb nm']    
  
  y_train = ['1', '2', '3', '1', '1', '3', '1', '2', '3',
            '1', '2', '3', '1', '4', '1', '2', '2', '4', 
            '1', '2', '3', '1', '1', '3', '1', '2', '3',
            '1', '2', '3', '1', '4', '1', '2', '2', '4']    
  
  
  parameters = {  
                  'clf__alpha': (1e-1, 1e-2),
                   'vect__ngram_range': [(1,2),(1,3)],
                   'vect__max_df': (0.9, 0.98)
              }
  
  text_clf_Pipline_MultinomialNB = Pipeline([('vect', CountVectorizer()),
                                             ('tfidf', TfidfTransformer()),
                                             ('clf', MultinomialNB()),                     
                                            ])
  gs_clf = GridSearchCV(text_clf_Pipline_MultinomialNB, parameters, n_jobs=-1)   
  
  gs_classifier = gs_clf.fit(X_train, y_train)
  
  nbclf = getmembers(gs_classifier.best_estimator_)[2][1]['named_steps']['clf']
  nbclf.feature_log_prob_ 
  </code></pre>
  
  <p>Then the question are: How can I get the word feature list in trained model corresponds to the log probabilities? Also, for example, which probability in _log_prob_ output corresponds to the word 'qwe' for class '1'? </p>
  
  <hr>
  
  <p>Edit after get the answer:
  Andreas's answer works:</p>
  
  <pre><code>gs_classifier.best_estimator_.named_steps['vect'].get_feature_names() 
  </code></pre>
  
  <p>Similar to this, there is a better way to index into the GridSearchCV to get the trained classifier</p>
  
  <pre><code>nbclf = gs_classifier.best_estimator_.named_steps['clf']
  </code></pre>