3

One question about mask 2-d np.array data.

For example:

  1. one 2-d np.array value in the shape of 20 x 20.
  2. An index t = [(1,2),(3,4),(5,7),(12,13)]

How to mask the 2-d array value by the (y,x) in index?

Usually, replacing with np.nan are based on the specific value like y[y==7] = np.nan

On my example, I want to replace the value specific location with np.nan.

For now, I can do it by:

  1. Creating a new array value_mask in the shape of 20 x 20
  2. Loop the value and testify the location by (i,j) == t[k]
  3. If True, value_mask[i,j] = value[i,j] ; In verse, value_mask[i,j] = np.nan

My method was too bulky especially for hugh data(3 levels of loops).
Are there some efficiency method to achieve that? Any advice would be appreciate.

2 Answers 2

4

You are nearly there.

You can pass arrays of indices to arrays. You probably know this with 1D-arrays.

With a 2D-array you need to pass the array a tuple of lists (one tuple for each axis; one element in the lists (which have to be of equal length) for each array-element you want to chose). You have a list of tuples. So you have just to "transpose" it.

t1 = zip(*t) 

gives you the right shape of your index array; which you can now use as index for any assignment, for example: value[t1] = np.NaN

(There are lots of nice explanation of this trick (with zip and *) in python tutorials, if you don't know it yet.)

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

Comments

2

You can use np.logical_and

arr = np.zeros((20,20)) 

You can select by location, this is just an example location.

arr[4:8,4:8] = 1 

You can create a mask the same shape as arr

mask = np.ones((20,20)).astype(bool) 

Then you can use the np.logical_and.

mask = np.logical_and(mask, arr == 1) 

And finally, you can replace the 1s with the np.nan

arr[mask] = np.nan 

2 Comments

your location is just meant as an example, but you can always select only "rectangles" by hardcoding the slices this way, so it won't be general enough...
@Ilja I use the arr[4:8,4:8] as part of the np.logical_and solution I am presenting in my answer to the OP's question. That's what I mean by example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.