Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mask 2D numpy array

I want to apply mask on 2D numpy array. But it does not work correctly. Suppose I have

val(lat, lon)  ---> my 2D array (20, 30)


Mask_lat = np.ma.masked_array(lat, mask=latmask)  ---> masked lat (5,)


Mask_lon = np.ma.masked_array(lon, mask =lonmask)   ---> masked lon (8,)


Maks_val = np.ma.masked_array(val, mask=mask_lat_lon) ---> ?

I do not know how can I pass a correct mask_lat_lon to have masked val (5,8). I would appreciate if one guides me.

Thank you in advance.

like image 541
zhr Avatar asked Jan 18 '16 17:01

zhr


People also ask

How do I mask a 2D Numpy array?

To mask rows and/or columns of a 2D array that contain masked values, use the np. ma. mask_rowcols() method in Numpy. The function returns a modified version of the input array, masked depending on the value of the axis parameter.

How do I mask an array in Numpy?

From the Reference Guide : A masked array is the combination of a standard numpy. ndarray and a mask. A mask is either nomask , indicating that no value of the associated array is invalid, or an array of booleans that determines for each element of the associated array whether the value is valid or not.

Is Numpy array a 2D array?

Python numpy 2d array reshape Here we can see how to reshape the 2-dimensional array in Python. In this example, we can easily use the numpy. reshape() method and this function will shape the array without updating its element.

How do you create a mask in Python?

To create a boolean mask from an array, use the ma. make_mask() method in Python Numpy. The function can accept any sequence that is convertible to integers, or nomask. Does not require that contents must be 0s and 1s, values of 0 are interpreted as False, everything else as True.


1 Answers

If I understand your question correctly, you have two 1D arrays that represent y and x (lat and long) positions in a 2D array. You want to mask a region based on the x/y position in the 2D array.

The key part to understand is that mask for a 2D array is also 2D.

For example, let's mask a single element of a 2D array:

import numpy as np

z = np.arange(20).reshape(5, 4)
mask = np.zeros(z.shape, dtype=bool)

mask[3, 2] = True

print z
print np.ma.masked_array(z, mask)

This yields:

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]]

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 -- 15]
 [16 17 18 19]]

In your case, you have two 1D x and y arrays that you need to create a 2D mask from. For example:

import numpy as np

x = np.linspace(-85, -78, 4)
y = np.linspace(32, 37, 5)
z = np.arange(20).reshape(5, 4)

xmask = (x > -82.6) & (x < -80)
ymask = (y > 33) & (y < 35.6)

print xmask
print ymask

We'd then need to combine them into a single 2D mask using broadcasting:

mask = xmask[np.newaxis, :] & ymask[:, np.newaxis]

Slicing with newaxis (or None, they're the same object) adds a new axis at that position, turning the 1D array into a 2D array. It you have seen this before, it's useful to take a quick look at what xmask[np.newaxis, :] and ymask[:, np.newaxis] look like:

In [14]: xmask
Out[14]: array([False, False,  True, False], dtype=bool)

In [15]: ymask
Out[15]: array([False,  True,  True, False, False], dtype=bool)

In [16]: xmask[np.newaxis, :]
Out[16]: array([[False, False,  True, False]], dtype=bool)

In [17]: ymask[:, np.newaxis]
Out[17]:
array([[False],
       [ True],
       [ True],
       [False],
       [False]], dtype=bool)

mask will then be (keep in mind that True elements are masked):

In [18]: xmask[np.newaxis, :] & ymask[:, np.newaxis]
Out[18]:
array([[False, False, False, False],
       [False, False,  True, False],
       [False, False,  True, False],
       [False, False, False, False],
       [False, False, False, False]], dtype=bool)

Finally, we can create a 2D masked array from z based on this mask:

arr = np.masked_array(z, mask)

Which gives us our final result:

 [[ 0  1  2  3]
  [ 4  5 --  7]
  [ 8  9 -- 11]
  [12 13 14 15]
  [16 17 18 19]]
like image 196
Joe Kington Avatar answered Oct 12 '22 00:10

Joe Kington