将二进制(0 | 1)numpy转换为整数或二进制字符串?

ste*_*ten 8 python binary numpy bitstring

是否有将二进制(0 | 1)numpy数组转换为整数或二进制字符串的快捷方式?铁

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

np.packbits(b)
Run Code Online (Sandbox Code Playgroud)

但只适用于8位值..如果numpy是9个或更多元素,它会生成2个或更多8位值.另一种选择是返回一个0 | 1的字符串......

我现在做的是:

    ba = bitarray()
    ba.pack(b.astype(np.bool).tostring())
    #convert from bitarray 0|1 to integer
    result = int( ba.to01(), 2 )
Run Code Online (Sandbox Code Playgroud)

这很难看!

Div*_*kar 10

一种方法是使用dot-product2-powered范围阵列-

b.dot(2**np.arange(b.size)[::-1])
Run Code Online (Sandbox Code Playgroud)

样品运行 -

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
Run Code Online (Sandbox Code Playgroud)

或者,我们可以使用按位左移运算符来创建范围数组,从而获得所需的输出,如下所示 -

b.dot(1 << np.arange(b.size)[::-1])
Run Code Online (Sandbox Code Playgroud)

如果时间感兴趣 -

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
Run Code Online (Sandbox Code Playgroud)

逆过程

要检索后面的二进制数组,使用np.binary_repr非常久远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])
Run Code Online (Sandbox Code Playgroud)


Geo*_*son 6

通过使用矢量化矩阵乘法代码,我扩展了 @Divikar 的良好点积解决方案,使其在我的主机上运行速度提高了约 180 倍。一次运行一行的原始代码大约需要 3 分钟才能在我的 pandas 数据框中运行 10 万行、18 列。好吧,下周我需要从 100K 行升级到 20M 行,所以大约 10 小时的运行时间对我来说不够快。首先,新代码是矢量化的。这才是 python 代码中真正的变化。其次,matmult 通常在多核处理器上并行运行,而您却看不到它,具体取决于您的主机配置,尤其是当 OpenBLAS 或其他 BLAS 可供 numpy 在像 matmult 这样的矩阵代数上使用时。因此,如果有的话,它可以使用很多处理器和内核。

新的——非常简单——代码在我的主机上运行 100K 行 x 18 个二进制列,时间大约为 1 秒,这对我来说已经“完成了任务”:

'''
Fast way is vectorized matmult. Pass in all rows and cols in one shot.
'''
def BitsToIntAFast(bits):
  m,n = bits.shape # number of columns is needed, not bits.size
  a = 2**np.arange(n)[::-1]  # -1 reverses array of powers of 2 of same length as bits
  return bits @ a  # this matmult is the key line of code

'''I use it like this:'''
bits = d.iloc[:,4:(4+18)] # read bits from my pandas dataframe
gs = BitsToIntAFast(bits)
print(gs[:5])
gs.shape
...
d['genre'] = np.array(gs)  # add the newly computed column to pandas
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。