How to pass elegantly Sklearn's GridseachCV's best parameters to another model?

后端 未结 2 1110
暖寄归人
暖寄归人 2021-02-05 14:47

I have found a set of best hyperparameters for my KNN estimator with Grid Search CV:

>>> knn_gridsearch_model.best_params_
{\'algorithm\': \'auto\', \'m         


        
2条回答
  •  广开言路
    2021-02-05 15:35

    You can do that as follows:

    new_knn_model = KNeighborsClassifier()
    new_knn_model.set_params(**knn_gridsearch_model.best_params_)
    

    Or just unpack directly as @taras suggested:

    new_knn_model = KNeighborsClassifier(**knn_gridsearch_model.best_params_)
    

    By the way, after finish running the grid search, the grid search object actually keeps (by default) the best parameters, so you can use the object itself. Alternatively, you could also access the classifier with the best parameters through

    gs.best_estimator_
    

提交回复
热议问题