Misha
Misha

Reputation: 377

Python mask for 2D array

I have generated a mask in the following manner-

mask_v_co = numpy.ones((numRows_v_co, numCols_v_co)).astype(numpy.uint8)
counter = 0
for i in range(numRows_v_co):
 for j in range(numCols_v_co):
  if Data_v_co[i,j] < 0:
    counter += 1          # Counting missing observation
    mask_v_co[i,j] = 0

How can I generate a mask using numpy masked array module where 0 indicating invalid entries (wherever Data_v_co[i,j] < 0) and 1 to indicate valid entries?

Upvotes: 0

Views: 464

Answers (1)

wflynny
wflynny

Reputation: 18551

Couldn't you just do something like the following?

import numpy as np

mask = np.ones_like(Data_v_co, dtype='int8')
mask[Data_v_co < 0] = 0

# count zeros
counter = np.prod(mask.shape) - mask.sum()

Upvotes: 1

Related Questions