1

I am writing a program where I use different methods to fit a dataset, and in the final step I want to take a distribution over the models, and then test it against a validation set to pick the optimal distribution. In order to do so, I need lists that sum up to 1 (the total weight of all the models). In the case of 3 models, I use the following code:

Grid = np.arange(0,1.1,0.1)
Dists = [[i,j,k] for i in Grid for j in Grid for k in Grid if i+j+k==1]

I am now looking for a way to generalize this to arbitrary number of models, say d, without specifying what d is beforehand. I have looked at np.tensordot and np.outer, but couldnt figure out a way to make this work. Any ideas would be appreciated. cheers, Leo

1 Answer 1

2

You are looking for itertools.product:

from itertools import product
Dists = [list(p) for p in product(Grid, repeat=3) if sum(p) == 1]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.