I am looking for an efficient way of turning
JavaScript
x
3
1
a=np.array([[0,1,1,0],
2
[0,1,1,1]])
3
into
JavaScript
1
3
1
b=np.array([[0b0110],
2
[0b0111]])
3
or into
JavaScript
1
3
1
b=np.array([[6],
2
[7]])
3
The only thing I found so far is np.packbits but this only works with 8bit numbers. However my arrays have shape of around (20e6,20)
I could of course do it by hand:
JavaScript
1
6
1
a=np.array([[0,1,1,0],[0,1,1,1]])
2
c=2**np.arange(np.shape(a)[-1])[::-1]
3
b=a.dot(c)
4
b
5
Out: array([6, 7])
6
But I assume that there is a faster way. Especially if the conversion directly to a binary array is possible.
Thanks for help!
Advertisement
Answer
np.packbits
does that. However, the bits of resulting array are stored in block of np.uint8
items. This means that a.shape[1] <= 8
must be true. Moreover, it packs bits on the right (in bigendian mode) so you need to shift the values. Here is the resulting code:
JavaScript
1
2
1
np.packbits(a, bitorder='big', axis=1) >> (8-a.shape[1])
2
If you need the code to work with a.shape[1] > 8
, then you can write a Numba code based on this previous post.