Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create all possible combinations of parameters in a dictionaryP

I have the following dictionary:

hyper_params = {'penalty': ['l1', 'l2'], 'class_weight': [None, 'balanced'], 'max_iter': [500, 1000]}

I need to train LogisticRegression of sklearn on all possible combinations of parameters from hyper_params, e.g.:

from sklearn.linear_model import LogisticRegression

LogisticRegression(penalty='l1', class_weight=None, max_iter=500)
LogisticRegression(penalty='l2', class_weight=None, max_iter=500)
etc.

How can I create all possible combinations of these 3 parameters, so that I can pass them as **args_comination to LogisticRegression.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I cannot use the hyperparameters optimisation library. Therefore I’m searching for a custom approach to enumerate hyper_params.

>Solution :

Based on All combinations of a list of lists you can achieve this by itertools.product(). So:

import itertools

hyper_params = {
    'penalty': ['l1', 'l2'],
    'class_weight': [None, 'balanced'],
    'max_iter': [500, 1000]
}


a = hyper_params.values()
combinations = list(itertools.product(*a))

for c in combinations:
    LogisticRegression(penalty=c[0], class_weight=c[1], max_iter=c[2])

Or if those are positional arguments in your LogisticRegression function you can even do:

for c in combinations:
    LogisticRegression(*c)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading