np.array(list(itertools.product([0,1], repeat = n**2))).reshape(-1,n,n)
creates an array (2^(n^2),n,n) .
There may be some kind of numpy grid function that does the same thing, but my recollection from other discussions is that itertools.product pretty fast.
g=(np.array(x).reshape(n,n) for x in itertools.product([0,1], repeat = n**2))
is a generator that produces nxn arrays one at a time:
g.next() # array([[0, 0],[0, 0]])
Or to create the same three-dimensional array:
np.array(list(g))
source share