Issue
I want to create a numpy.array
that consists of unique series of ones and zeros.
A row needs to have a length of 2*x
When x=2
the output is expected to look as follows:
[[1 0 1 0]
[1 0 0 1]
[0 1 1 0]
[0 1 0 1]]
When x=3
:
[[1 0 1 0 1 0]
[1 0 0 1 1 0]
[0 1 1 0 1 0]
[0 1 0 1 1 0]
[1 0 1 0 0 1]
[1 0 0 1 0 1]
[0 1 1 0 0 1]
[0 1 0 1 0 1]]
I tried np.meshgrid
and itertools.combinations
without any success sofar.
Please advice
Solution
use the following:
from itertools import product # or simply use np.emath.nx.itertools.product
x = 2
a = np.array(list(product(*[[0,1]]*x))))
np.column_stack([a, 1 - a])
array([[0, 0, 1, 1],
[0, 1, 1, 0],
[1, 0, 0, 1],
[1, 1, 0, 0]])
Answered By - Onyambu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.