You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow for refit=callable in *SearchCV to balance score and model complexity #11269
GridSearchCV and RandomizedSearchCV currently allow for refit=my_scorer_name to select the model that maximises some chosen metric. But these scorers need to be calculated independent of other candidates' results.
To balance model complexity with cross-validated score, it is common to use an approach like choosing the model that is least complex (by some metric or ordering) but is within 1 standard deviation of the best score. (Variant approaches exist, and may relate to budget constraints etc.)
We could consider allowing a callable to be passed to refit:
"""... refit : boolean, string, or callable, default=True Refit an estimator using the best found parameters on the whole dataset. For multiple metric evaluation, this can be a string denoting the scorer maximised to find the best parameters for refitting the estimator at the end. Where there are considerations other than maximum model performance in choosing a best estimator, ``refit`` can be set to a function which returns the selected ``best_index_`` given the ``cv_results_``...."""
Yes, it would be passed results and would be expected to return a value for best_index_. best_params_ and best_score_ would still be computed by BaseSearchCV
@jnothman Thanks for the response! My idea is to add another condition after this line to prevent it raise valueError when self.refit is a callable func.
That's about right... but you should think of it in terms of documentation
(how do I describe the feature to users?) and tests (what do you need to
assert in order to ascertain that the implementation is likely correct?).
Reacted by Wenhao Zhang | Assistant Professor @ SJTU
deftest_refit_callable():
""" Test refit=callable, which adds flexibility in identifying the "best" estimator """defscore(cv_results):
""" Dummy custmized score function for testing refit=callable. Returns the first element in 'foo_param' candidates """returncv_results["param_foo_param"].data[0]
X=np.arange(4).reshape(2,2)
y=np.array([0,1])
clf=GridSearchCV(MockClassifier(), {'foo_param':[1,2,3]}, refit=score)
clf.fit(X,y)
assert.equal(clf.best_index_, 1)
it's much harder to review in a comment than in a pull request. an example
(in the examples directory) of getting the sparsest model within 1 sd of
the best would also be great.
@jnothman Hi, I went through the examples directory and couldn't find an example of getting the sparsest model with 1 sd of the best. The only example I found could be relative is in "examples/model_selection/plot_roc_crossval.py".
One more thing, if we use multi-metric scoring, the parameter refit will be set to a scorer key to refit an estimator with the best parameter setting. If refit is passed a callable, how this callable going to tell which scorer to use? Check this related code here
yes, an example would need to be created or extended.
and the function would need to incorporate knowledge of which metric or
metrics to optimise. with this interface it can use multiple
Activity
janvanrijn commentedon Jun 15, 2018
This would replace the following parts in the code?
What would be the parameters of this refit method? The results dictionary?
jnothman commentedon Jun 15, 2018
Yes, it would be passed
resultsand would be expected to return a value forbest_index_.best_params_andbest_score_would still be computed byBaseSearchCVjanvanrijn commentedon Jun 15, 2018
In my code projects I would probably still use inheritance (Java school) but this would facilitate this.
Btw, how would you handle a string value? Some metrics need to be maximized, whereas others need to be minimized.
jnothman commentedon Jun 15, 2018
Do you mean things like MSE need to be minimised? We have a convention that all our scorers return values to be maximised. See the docs for
scoring.wenhaoz-fengcai commentedon Jun 18, 2018
@janvanrijn Hi, are you working on this enhancement or is it open to others?
janvanrijn commentedon Jun 18, 2018
I am not working on this, but I am happy to provide input / help / unit tests if that would be needed.
wenhaoz-fengcai commentedon Jun 18, 2018
Thanks! I'm planning on working on this issue. Your inputs and help are greatly appreciated
jnothman commentedon Jun 18, 2018
wenhaoz-fengcai commentedon Jun 18, 2018
@jnothman Thanks for the response! My idea is to add another condition after this line to prevent it raise valueError when self.refit is a callable func.
Meanwhile, replace this line with something like
Please correct me if I'm wrong.
jnothman commentedon Jun 19, 2018
wenhaoz-fengcai commentedon Jun 20, 2018
@janvanrijn @jnothman
Here is a test case for callable
self.refitIs this something we want?
jnothman commentedon Jun 20, 2018
wenhaoz-fengcai commentedon Jun 23, 2018
@jnothman Hi, I went through the examples directory and couldn't find an example of getting the sparsest model with 1 sd of the best. The only example I found could be relative is in "examples/model_selection/plot_roc_crossval.py".
wenhaoz-fengcai commentedon Jun 23, 2018
One more thing, if we use multi-metric scoring, the parameter
refitwill be set to a scorer key to refit an estimator with the best parameter setting. If refit is passed a callable, how this callable going to tell which scorer to use? Check this related code herejnothman commentedon Jun 23, 2018