lundi 15 avril 2019

What is a neat way to evaluate a Python function with different parameter combinations?

I'm having a function that I want to evaluate with different parameter combinations, e.g.

params1 = [1, 6, 100]
params2 = [True, False]
params3 = ["blue", "red", "green"]

Here is my straight forward attempt to do this:

res = np.zeros((len(params1), len(params2), len(params3)))

for i1, p1 in enumerate(params1):
    for i2, p2 in enumerate(params2):
        for i3, p3 in enumerate(params3):
            res = myfunc(p1,p2,p3)

This works, but adapting it to more complex scenarios is not only cumbersome but more importantly makes the code messy and thus increases the chance of mistakes.

My next approach to handle this in a more structured way would be to write a recursive function that would starts one for loop in one parameter and then inside this loop call itself.

But since my problem seems like a quite general problem, I wonder if there is a better (e.g. more basic or neat) way to address this.

Thanks for any suggestions.

Aucun commentaire:

Enregistrer un commentaire