Skip to content

Instantly share code, notes, and snippets.

@theorm
Created March 1, 2021 15:13
Show Gist options
  • Save theorm/9eceb6968ad4ee679da27f2121ecf43a to your computer and use it in GitHub Desktop.
Save theorm/9eceb6968ad4ee679da27f2121ecf43a to your computer and use it in GitHub Desktop.
import itertools
def dict_product(dicts):
"""
Cartesian product.
See https://stackoverflow.com/questions/5228158/cartesian-product-of-a-dictionary-of-lists
>>> list(dict_product(dict(number=[1,2], character='ab')))
[{'character': 'a', 'number': 1},
{'character': 'a', 'number': 2},
{'character': 'b', 'number': 1},
{'character': 'b', 'number': 2}]
"""
return (dict(zip(dicts, x)) for x in itertools.product(*dicts.values()))
parameters = {
'min_count': [1,2,3],
'size': [5,6,7],
'seed': [1],
# other parameters in the format above
}
for parameters_set in dict_product(parameters):
print('Running experiment with parameters set: ', parameters_set)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment