Skip to content

Wrong documentation of gridsearch.ParameterSampler #7364

Closed

Description

@FlorianWilhelm

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

from scipy import stats
dist = stats.norm(loc=-1., scale=2.)
dist.random_state = 42

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.

Activity

  1. amueller commented on Sep 8, 2016

    @amueller
    Member

    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?

    also @mfeurer is probably interested in this.

  2. mfeurer commented on Sep 8, 2016

    @mfeurer

    Passing the RNG of the ParameterSampler to the distribution would be an intuitive solution.

    Thanks for pinging me, I'm definitely interested in that.

  3. FlorianWilhelm commented on Sep 8, 2016

    @FlorianWilhelm
    ContributorAuthor

    @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
    
  4. amueller commented on Sep 8, 2016

    @amueller
    Member

    Not sure it's worth the backport...

  5. FlorianWilhelm commented on Sep 8, 2016

    @FlorianWilhelm
    ContributorAuthor

    @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.

  6. jasmainak commented on Oct 4, 2016

    @jasmainak
    Contributor

    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 :)

  7. jasmainak commented on Oct 4, 2016

    @jasmainak
    Contributor

    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

  8. amueller commented on Oct 4, 2016

    @amueller
    Member

    oh whoops, misread, you are right. Do you want to fix that?

  9. jasmainak commented on Oct 4, 2016

    @jasmainak
    Contributor

    see: #7577

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions