[scikit-learn] Specifying exceptions to ParameterGrid

Jaidev Deshpande deshpande.jaidev at gmail.com
Sat Nov 26 02:04:46 EST 2016


On Fri, 25 Nov 2016 at 20:24 Roman Yurchak <rth.yurchak at gmail.com> wrote:

> On 24/11/16 09:00, Jaidev Deshpande wrote:
> >
> >     well, `param_grid` in GridSearchCV can also be a list of
> dictionaries,
> >     so you could directly specify the cases you are interested in
> (instead
> >     of the full grid - exceptions), which might be simpler?
> >
> >
> > Actually now that I think of it, I don't know if it will be necessarily
> > simpler. What if I have a massive grid and only few exceptions?
> > Enumerating the complement of that small subset would be much more
> > expensive than specifying the exceptions.
> The solution indicated by Raghav is most concise if that works for you.
>
> Otherwise, in general, if you want to define the parameters as the full
> grid with a few exceptions, without changing the GirdSearchCV API, you
> could always try something like,
>
> ```
> from sklearn.model_selection import GridSearchCV, ParameterGrid
> from sklearn.neural_network import MLPClassifier
>
> grid_full = {'solver': ['sgd', 'adam'],
>              'learning_rate': ['constant', 'invscaling', 'adaptive']}
>
> def exception_handler(args):
>     # custom function shaping the domain of valid parameters
>     if args['solver'] == 'adam' and args['learning_rate'] != 'constant':
>         return False
>     else:
>         return True
>
> def wrap_strings(args):
>     # all values of dicts provided to GridSearchCV must be lists
>     return {key: [val] for key, val in args.items()}
>
> grid_tmp = filter(exception_handler, ParameterGrid(grid_full))
> grid = [wrap_strings(el) for el in grid_tmp]
>
> gs = GridSearchCV(MLPClassifier(random_state=42),
>                   param_grid=grid)
> ```
> That's quite similar to what you were suggesting in the original post.
>

Yes, also a lot more concise I guess. This way I just have to keep writing
an exception handler instead of subclassing.

Thanks!


> _______________________________________________
> scikit-learn mailing list
> scikit-learn at python.org
> https://mail.python.org/mailman/listinfo/scikit-learn
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-learn/attachments/20161126/d053bb5e/attachment-0001.html>


More information about the scikit-learn mailing list