简体   繁体   English

如何在numpy Python中找到1D二进制数组的十进制值

[英]How to find decimal value of 1D binary array in numpy Python

I have a boolean numpy array like this, 我有一个像这样的布尔numpy数组,

>>> np_arr
array([[1, 1, 1, 1, 0, 0, 1, 1],
       [1, 1, 1, 1, 0, 0, 1, 1],
       [1, 1, 1, 1, 0, 0, 1, 1],
       [1, 1, 1, 1, 0, 0, 1, 1],
       [1, 1, 1, 1, 0, 0, 1, 1],
       [1, 1, 1, 1, 0, 1, 0, 0],
       [1, 1, 1, 1, 0, 1, 0, 0],
       [1, 1, 1, 1, 0, 1, 0, 0]])

and another 1D array like this, 和另一个这样的一维数组,

>>> another_arr
array([128,  64,  32,  16,   8,   4,   2,   1])

I want to somehow do some and or addition to get only values where that 1 is present something like, 我想以某种方式做一些只得到值,其中即1存在类似,

>>> np_arr
array([[128,64,32,8, 0, 0, 2, 1],
       [128,64,32,8, 0, 0, 2, 1],
         ....................
       [128,64,32,8, 0,4, 0, 0],
        .....................)

So then I can then sum them to find the binary value of the each 1D array in the 2D array.. Or is some simple way to get decimal value numpy array as result? 那么我可以将它们相加以找到2D数组中每个1D数组的二进制值。或者是一些简单的方法来获得十进制值numpy数组作为结果?

This is one way. 这是一种方式。 It works because numpy broadcasts implicitly. 它起作用,因为numpy广播隐含。

import numpy as np

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

arr = np.array([128,  64,  32,  16,   8,   4,   2,   1])

arr2 = arr * mask

# array([[128,  64,  32,  16,   0,   0,   2,   1],
#        [128,  64,  32,  16,   0,   0,   2,   1],
#        [128,  64,  32,  16,   0,   0,   2,   1],
#        [128,  64,  32,  16,   0,   0,   2,   1],
#        [128,  64,  32,  16,   0,   0,   2,   1],
#        [128,  64,  32,  16,   0,   4,   0,   0],
#        [128,  64,  32,  16,   0,   4,   0,   0],
#        [128,  64,  32,  16,   0,   4,   0,   0]])

What you need is probably this: 你需要的可能是:

import numpy as np


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

np.packbits(ar, axis=-1)

Result: 结果:

array([[243],
       [243],
       [243],
       [243],
       [243],
       [244],
       [244],
       [244]], dtype=uint8)

here a quick and dirty way. 这是一个快速而肮脏的方式。

import numpy as np

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


a = np.array([17, 16, 12, 41, 0, 0, 5, 12])
for _ in ar:
    m = np.multiply(_, a)
    print(m)

here i've printed the values but you can append them in an array or use as you like. 这里我打印了值,但你可以将它们附加到一个数组中或者随意使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM