Reputation: 16997
I have a 2D numpy array that I need to mask based on a condition so that I can apply an operation to the masked array then revert the masked values back to the original.
For example:
import numpy as np
array = np.random.random((3,3))
condition = np.random.randint(0, 2, (3,3))
masked = np.ma.array(array, mask=condition)
masked += 2.0
But how can I change the masked values back to the original and "remove" the mask after applying a given operation to the masked array?
The reason why I need to do this is that I am generating a boolean array based on a set of conditions and I need to modify the elements of the array that satisfy the condition.
I could use boolean indexing to do this with a 1D array, but with the 2D array I need to retain its original shape ie. not return a 1D array with only the values satisfying the condition(s).
Upvotes: 5
Views: 16771
Reputation: 22459
The accepted answer doesn't answer the question. Assigning the mask to False
works in some cases but with many algorithms that do not support masked arrays (e.g. scipy.linalg.lstsq()
) you will experience an error like this:
ValueError: masked arrays are not supported
You can test if you array has still the attribute mask like this:
import numpy as np
array = np.random.random((3,3))
condition = np.random.randint(0, 2, (3,3))
masked = np.ma.array(array, mask=condition)
masked += 2.0
masked.mask = False
hasattr(masked, 'mask')
>> True
The only way to really get rid of the mask is by assigning a variable only to the data of the masked array:
masked = masked.data
hasattr(masked, 'mask')
>> False
Upvotes: 10
Reputation: 249153
You already have it: it's called array
!
This is because while masked
makes sure you only increment certain values in the matrix, the data is never actually copied. So once your code executes, array
has the elements at condition
incremented, and the rest remain unchanged.
Upvotes: 8