'All possible combinations in a binary image

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)?



Solution 1:[1]

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]]

Solution 2:[2]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BrokenBenchmark
Solution 2