如何打印Recle和Accuracy以及Sklearn中GridSearch中使用的参数?

问题描述 投票:3回答:1

我想打印精度,回想一下Grid中使用的每个参数,如何做到这一点。

我的Gridsearch代码

from sklearn.grid_search import GridSearchCV
rf1=RandomForestClassifier(n_jobs=-1, max_features='sqrt') 
#fit_rf1=rf.fit(X_train_res,y_train_res)

# Use a grid over parameters of interest
param_grid = { 
           "n_estimators" : [50, 100, 150, 200],
           "max_depth" : [2, 5, 10],
           "min_samples_leaf" : [10,20,30]}




from sklearn.metrics import make_scorer
from sklearn.metrics import precision_score,recall_score
scoring = {'precision': make_scorer(precision_score), 'Recall': make_scorer(recall_score)}
    CV_rfc = GridSearchCV(estimator=rf1, param_grid=param_grid, cv= 10,scoring=scoring)
    CV_rfc.fit(X_train_res, y_train_res)

我的预期输出

{'max_depth': 10, 'min_samples_leaf': 2, 'n_estimators': 50,'accuracy':.97,'recall':.89}
{'max_depth': 5, 'min_samples_leaf':10 , 'n_estimators': 100,'accuracy':.98,'recall':.92}
pandas machine-learning scikit-learn grid-search
1个回答
0
投票

如果您将scoring设置为得分者列表,您可以获得CV_rfc.cv_results_中每位得分手的平均得分。

例如:

from sklearn.datasets import make_classification
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
X, y = make_classification()
base_clf = RandomForestClassifier()
param_grid = { 
           "n_estimators" : [50, 100, 150, 200],}
CV_rf = GridSearchCV(base_clf, param_grid, scoring=['accuracy', 'roc_auc'], refit=False)
CV_rf.fit(X, y)

print(CV_rf.cv_results_)

你得到的输出如下:

{'mean_fit_time': array([ 0.05867839,  0.10268728,  0.15536443,  0.19937317]),
 'mean_score_time': array([ 0.00600123,  0.01033529,  0.0146695 ,  0.02000403]),
 'mean_test_accuracy': array([ 0.9 ,  0.91,  0.89,  0.91]),
 'mean_test_roc_auc': array([ 0.91889706,  0.94610294,  0.94253676,  0.94308824]),
 'mean_train_accuracy': array([ 1.,  1.,  1.,  1.]),
 'mean_train_roc_auc': array([ 1.,  1.,  1.,  1.]),
 [...]
 }

所以mean_test_[scoring]就是你追求的。请注意,您可以将cv_results_导入为Pandas DataFrame。这有助于提高可读性!

© www.soinside.com 2019 - 2024. All rights reserved.