Given two arrays, for example [0,0,0]
and [1,1,1]
, it is already clear (see here) how to generate all the combinations, i.e., [[0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]]
. itertools
(combinations
or product
) and numpy.meshgrid
are the most common ways as far as I know.
However, I could't find any discussions on how to generate this combinations randomly, without repetitions.
An easy solution could be to generate all the combinations and then choose some of them randomly. For example:
# Three random combinations of [0,0,0] and [1,1,1]
comb = np.array(np.meshgrid([0,1],[0,1],[0,1])).T.reshape(-1,3)
result = comb[np.random.choice(len(comb),3,replace=False),:]
However, this is infeasible when the number of combinations is too big.
Is there a way to generate random combinations without replacement in Python (possibly with Numpy) without generating all the combinations?
Aucun commentaire:
Enregistrer un commentaire