python - Numpy masked arrays - indicating missing values -


import numpy np import numpy.ma ma  """this operates expected 1 value masked""" = [0., 1., 1.e20, 9.] error_value = 1.e20 b = ma.masked_values(a, error_value) print b  """this not, values masked """ d = [0., 1., 'na', 9.] error_value = 'na' e = ma.masked_values(d, error_value) print e 

how can use 'nan', 'na', 'none', or similar value indicate missing data?

are getting data text file or similar? if so, i'd suggest using genfromtxt function directly specify masked value:

in [149]: f = stringio('0.0, 1.0, na, 9.0')  in [150]: = np.genfromtxt(f, delimiter=',', missing_values='na', usemask=true)  in [151]: out[151]: masked_array(data = [0.0 1.0 -- 9.0],              mask = [false false  true false],        fill_value = 1e+20) 

i think problem in example python list you're using initialize numpy array has heterogeneous types (floats , string). values coerced strings in numpy array, masked_values function uses floating point equality yielding strange results.

here's 1 way overcome creating array object dtype:

in [152]: d = np.array([0., 1., 'na', 9.], dtype=object)  in [153]: e = ma.masked_values(d, 'na')  in [154]: e out[154]: masked_array(data = [0.0 1.0 -- 9.0],              mask = [false false  true false],        fill_value = ?) 

you may prefer first solution since result has float dtype.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -