Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "remove" mask from numpy array after performing operations?

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

like image 470
pbreach Avatar asked Jan 06 '15 03:01

pbreach


People also ask

How do I clean my NumPy array?

Using the NumPy function np. delete() , you can delete any row and column from the NumPy array ndarray . Specify the axis (dimension) and position (row number, column number, etc.). It is also possible to select multiple rows and columns using a slice or a list.

What does [: :] mean on NumPy arrays?

The [:, :] stands for everything from the beginning to the end just like for lists. The difference is that the first : stands for first and the second : for the second dimension. a = numpy. zeros((3, 3)) In [132]: a Out[132]: array([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]])

How do you remove the last element of a NumPy array in Python?

delete() method. It is used to delete the elements in a NumPy array based on the row number.


1 Answers

The accepted answer doesn't answer the question. Assigning the mask to False works in practice but many algorithms do not support masked arrays (e.g. scipy.linalg.lstsq()) and this method doesn't get rid of it. So you will experience an error like this:

ValueError: masked arrays are not supported

The only way to really get rid of the mask is assigning the variable only to the data of the masked array.

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 

Assigning the variable to the data using the MaskedArray data attribute:

masked = masked.data 
hasattr(masked, 'mask')
>> False
like image 61
G M Avatar answered Sep 24 '22 20:09

G M