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
Wrong documentation of gridsearch.ParameterSampler #7364
In the documentation of gridsearch.ParameterSampler it is stated:
Note that as of SciPy 0.12, the scipy.stats.distributions do not accept a custom RNG instance and always use the singleton RNG from numpy.random. Hence setting random_state will not guarantee a deterministic iteration whenever scipy.stats distributions are used to define the parameter search space.
This is not really true and misleading. As of SciPy 0.16 you can easily set the random state of a distribution with
Therefore, one should rather point out to the user that one needs to additionally set the random state as shown above in order to get deterministic results.
Well the documentation is not really wrong, as you couldn't do that in 0.12 ;)
We could either fix the documentation to say that since 0.16 this is the way you can get deterministic results.
Or we could somehow do this in ParameterSampler so that the user doesn't have to worry about it.
Do you want to attempt the second or the first?
@amueller Well, I just realized that on the current master branch in the model_selection documentation of ParameterSampler it states:
Note that before SciPy 0.16, the scipy.stats.distributions do not
accept a custom RNG instance and always use the singleton RNG from numpy.random. Hence setting random_state will not guarantee a
deterministic iteration whenever scipy.stats distributions are used to
define the parameter search space. Deterministic behavior is however
guaranteed from SciPy 0.16 onwards.
and in model_selection._search in line 248 it says:
items = sorted(self.param_distributions.items())
for _ in six.moves.range(self.n_iter):
params = dict()
for k, v in items:
if hasattr(v, "rvs"):
if sp_version < (0, 16):
params[k] = v.rvs()
else:
params[k] = v.rvs(random_state=rnd)
else:
params[k] = v[rnd.randint(len(v))]
yield params
So it seems like this is already done :-/ Sorry for causing this confusion... it seems that only the old module grid_search and the ParameterSampler therein has this problem but it is deprecated anyway and replace by model_selection as I just learned. Anyway in grid_search the problem stills exists as far as I can tell looking at line 248:
items = sorted(self.param_distributions.items())
for _ in six.moves.range(self.n_iter):
params = dict()
for k, v in items:
if hasattr(v, "rvs"):
params[k] = v.rvs()
else:
params[k] = v[rnd.randint(len(v))]
yield params
@amueller yes, I guess so too... and I was just feeling lucky to have found a low hanging fruit to fix ;-) Well, next time then and thanks for your fast replies.
Hi guys, it might still be worth updating the documentation here. I just spent an afternoon trying to figure out if there was a way to make RandomizedSearchCV give deterministic outputs with scipy before stumbling upon this issue. A simple note stating that it's possible to have deterministic outputs with the new sklearn.model_selection module in conjunction with scipy 0.16 onwards might save someone many hours of searching and debugging :)
Umm ... do you mean the warning? I was suggesting that you could add to this warning that random_state can in fact be set for scipy 0.16 onwards. It's not super obvious that this is possible
Activity
amueller commentedon Sep 8, 2016
Well the documentation is not really wrong, as you couldn't do that in 0.12 ;)
We could either fix the documentation to say that since 0.16 this is the way you can get deterministic results.
Or we could somehow do this in
ParameterSamplerso that the user doesn't have to worry about it.Do you want to attempt the second or the first?
also @mfeurer is probably interested in this.
mfeurer commentedon Sep 8, 2016
Passing the RNG of the ParameterSampler to the distribution would be an intuitive solution.
Thanks for pinging me, I'm definitely interested in that.
FlorianWilhelm commentedon Sep 8, 2016
@amueller Well, I just realized that on the current master branch in the
model_selectiondocumentation ofParameterSamplerit states:and in
model_selection._searchin line 248 it says:So it seems like this is already done :-/ Sorry for causing this confusion... it seems that only the old module
grid_searchand theParameterSamplertherein has this problem but it is deprecated anyway and replace bymodel_selectionas I just learned. Anyway ingrid_searchthe problem stills exists as far as I can tell looking at line 248:amueller commentedon Sep 8, 2016
Not sure it's worth the backport...
FlorianWilhelm commentedon Sep 8, 2016
@amueller yes, I guess so too... and I was just feeling lucky to have found a low hanging fruit to fix ;-) Well, next time then and thanks for your fast replies.
jasmainak commentedon Oct 4, 2016
Hi guys, it might still be worth updating the documentation here. I just spent an afternoon trying to figure out if there was a way to make
RandomizedSearchCVgive deterministic outputs with scipy before stumbling upon this issue. A simple note stating that it's possible to have deterministic outputs with the newsklearn.model_selectionmodule in conjunction with scipy 0.16 onwards might save someone many hours of searching and debugging :)amueller commentedon Oct 4, 2016
@jasmainak there is a note in red there under RandomizedSearchCV, right?
http://scikit-learn.org/stable/modules/grid_search.html#randomized-parameter-optimization
jasmainak commentedon Oct 4, 2016
Umm ... do you mean the warning? I was suggesting that you could add to this warning that
random_statecan in fact be set for scipy 0.16 onwards. It's not super obvious that this is possibleamueller commentedon Oct 4, 2016
oh whoops, misread, you are right. Do you want to fix that?
jasmainak commentedon Oct 4, 2016
see: #7577