'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.
I cannot use the hyperparameters optimisation library. Therefore I'm searching for a custom approach to enumerate hyper_params.
Solution 1:[1]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
