Home > Enterprise >  All possible combinations in a binary image
All possible combinations in a binary image

Time:05-01

I'm trying to create all possible combinations of 0 and 1 in an array that have the shape (n, 10). For example, if we assume an arbitrary combination like this: np.array([0, 0, 1, 1, 0, 0, 1, 1, 0, 0]), how can I generate all possible combinations (which will result in 2^10=1024 arrays)?

CodePudding user response:

Yes, you can use itertools.product() with the repeat parameter to generate the desired output:

import numpy as np
from itertools import product

np.array(list(product([0, 1], repeat=10)))

This outputs:

[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 1]
 [0 0 0 ... 0 1 0]
 ...
 [1 1 1 ... 1 0 1]
 [1 1 1 ... 1 1 0]
 [1 1 1 ... 1 1 1]]

CodePudding user response:

You can use permutations from the itertools module:

import numpy as np 
import itertools

list = np.array([0, 0, 1, 1, 0, 0, 1, 1, 0, 0])
combs = itertools.permutations(list)

for i in combs:
    print(i)
  • Related