My problem is similar to this answered question: stackoverflow.com/questions/42660752/how-to-create-all-possible-combinations-of-formulas-using-patsy-for-model-select. The accepted answer to that question uses a nested for
loop comprising calls to itertools.permutations
and itertools.product
, which assumes that all possible combinations of all parameters are desired. In my case that leads to an infeasibly large number of combinations with majority redundancy, since my variables are arranged in groups, of which up to one (0-1) parameter should be selected for any given combination. (I'm also using statsmodels
with patsy
.)
Looking into itertools.product
, (eg iq.opengenus.org/combination-one-element-list-python/), all combinations could be easily generated using one entry from each group, eg:
pars1 = ["par1a", "par1b"]
pars2 = ["par2a", "par2b"]
pars3 = ["par3a", "par3b"]
operators = '+ : *'.split()
all_combs = [expr for expr in itertools.product(*[pars1, operators, pars2, operators, pars3])]
But this leaves out combinations where no entry from one or more of the parameter lists is included in the output expression (which would also require the corresponding operator from operators
to be omitted). For example, how to also include all combinations like:
par1a + par2a
etc? That is, compactly, ie, without having to do it using a staged approach of building up the output list with repeats of the above code block using fewer of the parameter groups.