Reverse mask

I want to invert true / false in my mask array in numpy.

So, in the example below, I do not want to mask the second value in the data array, I want to mask the first and third value.

The following is an example. A masked array is created by a longer process than before. Therefore, I cannot change the mask array itself. Is there any other way to invert values?

import numpy data = numpy.array([[ 1, 2, 5 ]]) mask = numpy.array([[0,1,0]]) numpy.ma.masked_array(data, mask) 
+6
source share
1 answer
 import numpy data = numpy.array([[ 1, 2, 5 ]]) mask = numpy.array([[0,1,0]]) numpy.ma.masked_array(data, ~mask) #note this probably wont work right for non-boolean (T/F) values #or numpy.ma.masked_array(data, numpy.logical_not(mask)) 

eg

 >>> a = numpy.array([False,True,False]) >>> ~a array([ True, False, True], dtype=bool) >>> numpy.logical_not(a) array([ True, False, True], dtype=bool) >>> a = numpy.array([0,1,0]) >>> ~a array([-1, -2, -1]) >>> numpy.logical_not(a) array([ True, False, True], dtype=bool) 
+11
source

Source: https://habr.com/ru/post/945731/


All Articles