sonido
sonido

Reputation: 63

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

Upvotes: 1

Views: 123

Answers (2)

AboAmmar
AboAmmar

Reputation: 5559

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)

Upvotes: 0

BrokenBenchmark
BrokenBenchmark

Reputation: 19252

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

Upvotes: 1

Related Questions