How to graph grid scores from GridSearchCV?

前端 未结 10 1006
旧时难觅i
旧时难觅i 2021-01-30 03:19

I am looking for a way to graph grid_scores_ from GridSearchCV in sklearn. In this example I am trying to grid search for best gamma and C parameters for an SVR algorithm. My c

10条回答
  •  闹比i
    闹比i (楼主)
    2021-01-30 04:13

    I used grid search on xgboost with different learning rates, max depths and number of estimators.

    gs_param_grid = {'max_depth': [3,4,5], 
                     'n_estimators' : [x for x in range(3000,5000,250)],
                     'learning_rate':[0.01,0.03,0.1]
                    }
    gbm = XGBRegressor()
    grid_gbm = GridSearchCV(estimator=gbm, 
                            param_grid=gs_param_grid, 
                            scoring='neg_mean_squared_error', 
                            cv=4, 
                            verbose=1
                           )
    grid_gbm.fit(X_train,y_train)
    

    To create the graph for error vs number of estimators with different learning rates, I used the following approach:

    y=[]
    cvres = grid_gbm.cv_results_
    best_md=grid_gbm.best_params_['max_depth']
    la=gs_param_grid['learning_rate']
    n_estimators=gs_param_grid['n_estimators']
    
    for mean_score, params in zip(cvres["mean_test_score"], cvres["params"]):
        if params["max_depth"]==best_md:
            y.append(np.sqrt(-mean_score))
    
    
    y=np.array(y).reshape(len(la),len(n_estimators))
    
    %matplotlib inline
    plt.figure(figsize=(8,8))
    for y_arr, label in zip(y, la):
        plt.plot(n_estimators, y_arr, label=label)
    
    plt.title('Error for different learning rates(keeping max_depth=%d(best_param))'%best_md)
    plt.legend()
    plt.xlabel('n_estimators')
    plt.ylabel('Error')
    plt.show()
    

    The plot can be viewed here: Result

    Note that the graph can similarly be created for error vs number of estimators with different max depth (or any other parameters as per the user's case).

提交回复
热议问题