1

I need to test a boolean in an array, and based on the answer, apply an elementwise operation on a matrix. I seem to be getting the boolean answer for the ROW and not for the individual element itself. how do i test and get the answer for each individual element?

i have an matrix of probabilities

probs = np.array([[0.1, 0.2, 0.3, 0.3, 0.7],
                  [0.1, 0.2, 0.3, 0.3, 0.7],
                  [0.7, 0.2, 0.6, 0.1, 0.0]])

and a matrix of test arrays

tst = ([False, False, True, True, False],
       [True, False, True, False, False],
       )
t = np.asarray(tst).astype('bool')

and this segment of code i have written which outputs the answer, but obviously test the entire row, as everything is FALSE.

for row in tst:
    mat = []
    for row1 in probs:
        temp = []
        if row == True:
            temp.append(row1)
        else: temp.append(row1-1)
        mat.append(temp)

mat
Out[42]: 
[[array([-0.9, -0.8, -0.7, -0.7, -0.3])],
 [array([-0.9, -0.8, -0.7, -0.7, -0.3])],
 [array([-0.3, -0.8, -0.4, -0.9, -1. ])]]

i need the new matrix to be

[[-0.9, -0.8, 0.3, 0.3, -0.3],
 [-0.9, -0.8, 0.3, 0.3, -0.3],
 [-0.3, -0.8, 0.6, 0.1, -1]

for the 1st array in tst. Thanks very much for any assistance!

2 Answers2

0

You need to keep the values as-is if test is True and substract 1 otherwise

Your loop doesn't work because you're comparing a list with a boolean. After that you're adding the whole row minus 1 (substracting 1 to all elements)

My solution: substract the boolean row to the values row, but inverting True and False (if True, don't substract, if False, substract):

for row in tst:
    mat = []
    for row1 in probs:
        mat.append(row1-[not v for v in row])

    print(np.asarray(mat))

prints (for each iteration) (note that you have 2 results since you're combining 2 truth tables with your matrix):

[[-0.9 -0.8  0.3  0.3 -0.3]
 [-0.9 -0.8  0.3  0.3 -0.3]
 [-0.3 -0.8  0.6  0.1 -1. ]]
[[ 0.1 -0.8  0.3 -0.7 -0.3]
 [ 0.1 -0.8  0.3 -0.7 -0.3]
 [ 0.7 -0.8  0.6 -0.9 -1. ]]

(I'm not a numpy expert at all, sorry if this is clumsy, comments welcome)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

You don't need need a loop here. You have an array and an corresponding mask array.

probs[np.invert(tst)]-=1.

The mask will give you back the true values. You wan't the false values so invert the tst array.

# This would be a longer version, if you are not familiar with the synthax above
probs[np.invert(tst)]=probs[np.invert(tst)]-1.

If you want to create a new numpy Array (you created a list of numpy-arrays in your code) it will work for example this way.

# copy the numpy array
mat=np.copy(probs)
mat[np.invert(tst)]=probs[np.invert(tst)]-1

I would recommend you to take a look at a view beginners tutorials first, programming will be much easier if you know for example the difference between lists and numpy-arrays and how to handle them.

https://www.scipy.org/scipylib/faq.html#what-advantages-do-numpy-arrays-offer-over-nested-python-lists https://docs.scipy.org/doc/numpy-dev/user/quickstart.html

or a short explanation

Python List vs. Array - when to use?

Community
  • 1
  • 1
max9111
  • 6,272
  • 1
  • 16
  • 33
  • =) yes i really should, unfortunately this is due in 2 days. I am in the deep end going in with no python exp, and very little prog exp. and YES! creating a new array was what i needed. Thanks. – kinda.anonymous May 06 '17 at 21:56