Skip to content
Advertisement

Converting a numpy array of zeros and ones into an array of binary numbers

I am looking for an efficient way of turning

    a=np.array([[0,1,1,0],
                [0,1,1,1]])

into

    b=np.array([[0b0110],
                [0b0111]])

or into

    b=np.array([[6],
                [7]])

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:

    a=np.array([[0,1,1,0],[0,1,1,1]])
    c=2**np.arange(np.shape(a)[-1])[::-1]
    b=a.dot(c)
    b
    Out: array([6, 7])

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:

np.packbits(a, bitorder='big', axis=1) >> (8-a.shape[1])

If you need the code to work with a.shape[1] > 8, then you can write a Numba code based on this previous post.

Advertisement