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

15,064

Solution 1

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_

Solution 2

I just want to point out that using the grid.best_parameters and pass them to a new model by unpacking like:

my_model = KNeighborsClassifier(**grid.best_params_)

is good and all and I personally used it a lot.
However, as you can see in the documentation here, if your goal is to predict something using those best_parameters, you can directly use the grid.predict method which will use these best parameters for you by default.

example:

y_pred = grid.predict(X_test)

Hope this was helpful.

Share:
15,064

Related videos on Youtube

Hendrik
Author by

Hendrik

Updated on June 18, 2022

Comments

  • Hendrik
    Hendrik almost 2 years

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

    >>> knn_gridsearch_model.best_params_
    {'algorithm': 'auto', 'metric': 'manhattan', 'n_neighbors': 3}
    

    So far, so good. I want to train my final estimator with these new-found parameters. Is there a way to feed the above hyperparameter dict to it directly? I tried this:

    >>> new_knn_model = KNeighborsClassifier(knn_gridsearch_model.best_params_)
    

    but instead the hoped result new_knn_model just got the whole dict as the first parameter of the model and left the remaining ones as default:

    >>> knn_model
    KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
               metric_params=None, n_jobs=1,
               n_neighbors={'n_neighbors': 3, 'metric': 'manhattan', 'algorithm': 'auto'},
               p=2, weights='uniform')
    

    Disappointing indeed.

    • taras
      taras almost 7 years
      you have to unpack the dict with **knn_gridsearch_model.best_params_
  • Hendrik
    Hendrik almost 7 years
    Thank you. As for your latter tip, I need to recycle the best parameters because I want to train a new model on the combined train+test sets at the end.
  • Max Power
    Max Power over 6 years
    I do this by using the last answer here, gs.best_estimator_, I'd advise that as the best approach of the three here.