In numpy I would like to make a 2d arrray (r, by 2**r) where the columns are all possible binary columns.
For example, if height of the columns is 5, the columns would be
[0,0,0,0,0], [0,0,0,0,1], [0,0,0,1,0], [0,0,0,1,1], [0,0,1,0,0], ...
My solution is
np.array(list(itertools.product([0,1],repeat = c))).T
This seems very ugly. Is there a more elegant way?
If I have to create a 2D array of 1s or 0s, I can use numpy. ones() or numpy. zeros() respectively.
To declare a 2D array, specify the type of elements that will be stored in the array, then ( [][] ) to show that it is a 2D array of that type, then at least one space, and then a name for the array. Note that the declarations below just name the variable and say what type of array it will reference.
If you only use the arange function, it will output a one-dimensional array. To make it a two-dimensional array, chain its output with the reshape function. First, 20 integers will be created and then it will convert the array into a two-dimensional array with 4 rows and 5 columns.
You can use some broadcasting
here, like so -
(((np.arange(2**r)[:,None] & 2**np.arange(r)[::-1]))>0).astype(int)
For r
between 0
and 8
, you can also use np.unpackbits
-
np.unpackbits(np.arange(2**r,dtype='uint8')[:,None], axis=1)[:,8-r:]
Runtime tests -
Case #1 (Original r = 5
):
In [217]: r = 5
In [218]: from itertools import product
In [219]: %timeit np.array(list(product([0,1], repeat=5)))
10000 loops, best of 3: 33.9 µs per loop
In [220]: %timeit np.unpackbits(np.arange(2**r,dtype='uint8')[:,None], axis=1)[:,8-r:]
100000 loops, best of 3: 10.6 µs per loop
In [221]: %timeit (((np.arange(2**r)[:,None] & 2**np.arange(r)[::-1]))>0).astype(int)
10000 loops, best of 3: 31.1 µs per loop
Case #2 (Larger r
):
In [242]: r = 15
In [243]: %timeit (((np.arange(2**r)[:,None] & 2**np.arange(r)[::-1]))>0).astype(int)
100 loops, best of 3: 6.6 ms per loop
In [244]: %timeit np.array(list(product([0,1], repeat=r)))
10 loops, best of 3: 77.5 ms per loop
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With