1

I have troubles understanding the behaviour of numpy masked array.

Here is the snippet that puzzles me for two reasons:

arr = numpy.ma.array([(1,2),(3,4)],dtype=[("toto","int"),("titi","int")])
arr[0][0] = numpy.ma.masked
  1. when doing this nothing happens, no mask is applied on the element [0][0]
  2. changing the data to [[1,2],[3,4]] (instead of [(1,2),(3,4)]), I get the following error: TypeError: expected a readable buffer object

It seems that I misunderstood completely how to setup (and use) masked array.

Could you tell me what is wrong with this code ?

thanks

EDIT: without specifying the dtypes, it works like expected

1 Answer 1

1

The purpose of a masked array is to tell for any operation that some elements of the array are invalid to be used, i.e. masked.

For example, you have an array:

a = np.array([[2, 1000], [3, 1000]])

And you want to ignore any operations with the elements >100. You create a masked array like:

b = np.ma.array(a, mask=(a>100))

You can perform some operations in both arrays to see the differences:

a.sum()
# 2005
b.sum()
# 5

a.prod()
# 6000000
b.prod()
# 6

As you see, the masked items are ignored...

Sign up to request clarification or add additional context in comments.

1 Comment

@PellegriniEric I've seen you haven't accepted any answer. Just to let you know, you can do it by clicking on the arrow at the left-hand side of the answer...

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.