2

I have a boolean numpy array like this,

>>> 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,

>>> 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?

3
  • 2
    It sounds like you're asking for multiplication. Commented Feb 11, 2018 at 11:49
  • 1
    Should those 8s in your expected output be 16s? And it sounds like you want numpy.packbits. No need to multiply and sum. Commented Feb 11, 2018 at 12:24
  • 1
    @MateenUlhaq HAHAHA that was so simple... what is wrong with me sometimes :( Commented Feb 11, 2018 at 12:56

3 Answers 3

5

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)
Sign up to request clarification or add additional context in comments.

Comments

4

This is one way. It works because numpy broadcasts implicitly.

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]])

Comments

0

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.

3 Comments

Why is this better than a simple ar*a?
@MarkDickinson i guess for direct multiplication ar*a is the best solution but if you want to handle each row, do some operation on it (like sum) and then append it to an array then in my opinion this approach is better, in contrast to last part of the question
If you wanted to sum each row and accumulate the sums in an array you'd do (ar * a).sum(axis=1). It's usually better to avoid explicit for loops when working with NumPy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.