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

JavaScript

into

JavaScript

or into

JavaScript

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

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

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