Binary numpy array to list of integers?

前端 未结 5 1385
挽巷
挽巷 2020-11-27 07:08

I have a binary array, and I would like to convert it into a list of integers, where each int is a row of the array.

For example:

from numpy import *         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 08:07

    If you like working directly with bitwise math, this one should work pretty well.

    def bits2int(a, axis=-1):
        return np.right_shift(np.packbits(a, axis=axis), 8 - a.shape[axis]).squeeze()
    
    bits2int(a)
    Out: array([12,  4,  7, 15], dtype=uint8)
    

提交回复
热议问题