Does GridSearchCV store all the scores for all parameter combinations?

15,511

Solution 1

Yes it does, exactly as it is stated in the docs:

grid_scores_ : list of named tuples

Contains scores for all parameter combinations in param_grid. Each entry corresponds to one parameter setting. Each named tuple has the attributes:

  • parameters, a dict of parameter settings
  • mean_validation_score, the mean score over the cross-validation folds
  • cv_validation_scores, the list of scores for each fold

Solution 2

allscores=model.cv_results_['mean_test_score']
print(allscores)
Share:
15,511
Bin
Author by

Bin

Updated on September 26, 2022

Comments

  • Bin
    Bin over 1 year

    The GridSearchCV use 'scoring' to select best estimator. After train the GridSearchCV, I would like to see the score for each combination. Does GridSearchCV store all scores for each parameter combinations? If it does how to get the scores? Thanks.

    Here is an example code that I used in another post.

    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
    
    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)
    
  • Bin
    Bin over 8 years
    Thanks for another great answer from you. That's exactly what I am looking for.
  • rubik
    rubik about 7 years
    As of Sklearn 0.18.1, grid_scores_ has been deprecated in favor of cv_results_ which is more complete.
  • Sam
    Sam almost 6 years
    Can we also get access to the train results for each fold as to plot learning curves?
  • Blue
    Blue over 5 years
    Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
  • yogz123
    yogz123 about 4 years
    Is it possible to identify which scores map to which parameters (in the case of multiple scores)?
  • Ryan J McCall
    Ryan J McCall over 2 years
    GridSearchCV.cv_results_['params'] is an array of parameter combos tried. GridSearchCV.cv_results_['mean_test_score'] contains the corresponding test scores