Convert binary (0|1) numpy to integer or binary-string?

前端 未结 5 1259
渐次进展
渐次进展 2021-02-19 20:29

Is there a shortcut to Convert binary (0|1) numpy array to integer or binary-string ? F.e.

b = np.array([0,0,0,0,0,1,0,1])   
  => b is 5

np.packbits(b)
         


        
5条回答
  •  北荒
    北荒 (楼主)
    2021-02-19 20:43

    One way would be using dot-product with 2-powered range array -

    b.dot(2**np.arange(b.size)[::-1])
    

    Sample run -

    In [95]: b = np.array([1,0,1,0,0,0,0,0,1,0,1])
    
    In [96]: b.dot(2**np.arange(b.size)[::-1])
    Out[96]: 1285
    

    Alternatively, we could use bitwise left-shift operator to create the range array and thus get the desired output, like so -

    b.dot(1 << np.arange(b.size)[::-1])
    

    If timings are of interest -

    In [148]: b = np.random.randint(0,2,(50))
    
    In [149]: %timeit b.dot(2**np.arange(b.size)[::-1])
    100000 loops, best of 3: 13.1 µs per loop
    
    In [150]: %timeit b.dot(1 << np.arange(b.size)[::-1])
    100000 loops, best of 3: 7.92 µs per loop
    

    Reverse process

    To retrieve back the binary array, use np.binary_repr alongwith np.fromstring -

    In [96]: b = np.array([1,0,1,0,0,0,0,0,1,0,1])
    
    In [97]: num = b.dot(2**np.arange(b.size)[::-1]) # integer
    
    In [98]: np.fromstring(np.binary_repr(num), dtype='S1').astype(int)
    Out[98]: array([1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1])
    

提交回复
热议问题