GridSearchCV 是否存储所有参数组合的所有分数?
Does GridSearchCV store all the scores for all parameter combinations?
GridSearchCV 使用 'scoring' 到 select 最佳估计器。训练 GridSearchCV 后,我想查看每个组合的分数。 GridSearchCV 是否存储每个参数组合的所有分数?如果它确实如何获得分数?谢谢
这是我在另一个 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)
是的,正如 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
allscores=model.cv_results_['mean_test_score']
print(allscores)
GridSearchCV 使用 'scoring' 到 select 最佳估计器。训练 GridSearchCV 后,我想查看每个组合的分数。 GridSearchCV 是否存储每个参数组合的所有分数?如果它确实如何获得分数?谢谢
这是我在另一个 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)
是的,正如 docs 中所述:
grid_scores_
: list of named tuplesContains 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 settingsmean_validation_score
, the mean score over the cross-validation foldscv_validation_scores
, the list of scores for each fold
allscores=model.cv_results_['mean_test_score']
print(allscores)