ageron / handson-ml2

A series of Jupyter notebooks that walk you through the fundamentals of Machine Learning and Deep Learning in Python using Scikit-Learn, Keras and TensorFlow 2.
Apache License 2.0
27.88k stars 12.75k forks source link

[Chapter 10] 'RandomizedSearchCV' object has no attribute 'best_estimator_' #223

Open simbol01 opened 4 years ago

simbol01 commented 4 years ago

Help please I got this error while trying to save my model after fine-tuning with RandomizedSearchCV. model = rnd_search_cv.bestestimator.model But I got the above error, any suggestions please?

danielbrito91 commented 3 years ago

I got the same error

ageron commented 3 years ago

Thanks for your feedback. The best_estimator_ attribute ends with an underscore. In Scikit-Learn, this means that it's a learned parameter, so it's only available after the fit() function finishes successfully. If there was an error during training, or if you interrupted training, then this attribute will not be present.

Note that the RandomizedSearchCV's fit() function will first look for the best hyperparameter values and then (if refit=True, which is the default) it will train a final model on the whole training set (instead of a cross-validation subset as during the hyperparameter search). So if the function was interrupted after the hyperparameter search but before the end of training the final model, it might be possible to have best_params_ defined, perhaps also best_score_, but not best_estimator_.

I hope this helps.

ageron commented 3 years ago

Note: just to make sure, I just ran the Colab Notebook for this chapter, and best_estimator_ worked fine, so I think the hypothesis above is correct (the fit() function was probably interrupted before it finished).