3

I have the following array:

[[    6.           105.             2.             8.09841881]
[    6.           105.             4.             9.34220351]
[    6.           105.             6.             9.97663435]
[    6.          1001.             2.             9.57108242]
[    6.          1001.             4.            12.22355794]
[    6.          1001.             6.            13.57295942]
[   12.          1001.             2.            12.37474466]
[   12.          1001.             4.            17.45334004]
[   12.          1001.             6.            19.88499289]
[   18.          1007.             2.            16.09076561]
[   18.          1007.             4.            23.43742275]
[   18.          1007.             6.            27.73041646]]

And I have tried to extract only the rows with the first element being a six via

print ma.MaskedArray(a, mask=(np.ones_like(a)*(a[:,0]==6.0)).T)

which I got from the question " mask a 2D numpy array based on values in one column". However, I get

File "./Prova.py", line 170, in <module>
print ma.MaskedArray(a, mask=(np.ones_like(a)*(a[:,0]==6.0)).T)
ValueError: operands could not be broadcast together with shapes (12,4) (12)

do you have a clue of why this doesn't work?

The question might be stupid, but please bear with me since I just started programming python. :-)

2
  • You might need to insert a np.newaxis into your mask. Commented Apr 2, 2013 at 20:19
  • What would that do, and how can I do that? Commented Apr 2, 2013 at 20:21

1 Answer 1

4

Setting up some test data to work on:

>>> a = np.arange(12*4).reshape((12,4))

First, we "allocate" space for our mask array:

>>> mask = np.empty(a.shape,dtype=bool)

Now we can't assign into it from the first column of a == 6 directly because they're not the proper shape:

>>> mask[:,:] = a[:,0] == 6
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (12,4) (12) 

But we can broadcast our first column of a up to the correct shape by simply inserting a newaxis so that it becomes a 2-D array:

>>> mask[:,:] = (a[:,0] == 6)[:,np.newaxis]

As we can see, our mask is now correct.

>>> mask
array([[ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [False, False, False, False],
       [False, False, False, False],
       [False, False, False, False],
       [False, False, False, False],
       [False, False, False, False],
       [False, False, False, False],
       [False, False, False, False]], dtype=bool)

Now we just make our masked array and sit back and enjoy :).

>>> ma.MaskedArray(a,mask=mask)
masked_array(data =
 [[-- -- -- --]
 [-- -- -- --]
 [-- -- -- --]
 [-- -- -- --]
 [-- -- -- --]
 [20 21 22 23]
 [24 25 26 27]
 [28 29 30 31]
 [32 33 34 35]
 [36 37 38 39]
 [40 41 42 43]
 [44 45 46 47]],
             mask =
 [[ True  True  True  True]
 [ True  True  True  True]
 [ True  True  True  True]
 [ True  True  True  True]
 [ True  True  True  True]
 [False False False False]
 [False False False False]
 [False False False False]
 [False False False False]
 [False False False False]
 [False False False False]
 [False False False False]],
       fill_value = 999999)
Sign up to request clarification or add additional context in comments.

7 Comments

I don't believe the .T does anything, since the first line is a 1d array.
It keeps not working. I get: File "./Prova.py", line 171, in <module> mask = (np.ones_like(a)*(a[:,0]==6.0)).T ValueError: operands could not be broadcast together with shapes (12,4) (12)
@FerdinandoRandisi -- See my update. It seems to be working now.
@askewchan -- You're right. I wasn't trying too hard to parse that expression to figure out what was going on. Ultimately, it was a bit too compact for my blood.
It does work now! One last question though: what about the ones_like? Is it necessary?
|

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.