masked-array

Calculate moving average in numpy array with NaNs

空扰寡人 提交于 2019-12-06 12:00:02
I am trying to calculate the moving average in a large numpy array that contains NaNs. Currently I am using: import numpy as np def moving_average(a,n=5): ret = np.cumsum(a,dtype=float) ret[n:] = ret[n:]-ret[:-n] return ret[-1:]/n When calculating with a masked array: x = np.array([1.,3,np.nan,7,8,1,2,4,np.nan,np.nan,4,4,np.nan,1,3,6,3]) mx = np.ma.masked_array(x,np.isnan(x)) y = moving_average(mx).filled(np.nan) print y >>> array([3.8,3.8,3.6,nan,nan,nan,2,2.4,nan,nan,nan,2.8,2.6]) The result I am looking for (below) should ideally have NaNs only in the place where the original array, x, had

How can I mask elements of a record array in Numpy?

北战南征 提交于 2019-12-05 08:22:37
I understand how to create a masked array, and I would like to use masking in a record array so that I can access this data using named attributes. The masking seems to be "lost" when I create a record array from a masked array: >>> data = np.ma.array(np.ma.zeros(30, dtype=[('date', '|O4'), ('price', '<f8')]),mask=[i<10 for i in range(30)]) >>> data masked_array(data = [(--, --) (--, --) (--, --) (--, --) (--, --) (--, --) (--, --) (--, --) (--, --) (--, --) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0

numpy.ma (masked) array mean method has inconsitent return type

左心房为你撑大大i 提交于 2019-11-29 14:19:51
I noticed that the numpy masked-array mean method returns different types when it probably should not: import numpy as np A = np.ma.masked_equal([1,1,0], value=0) B = np.ma.masked_equal([1,1,1], value=0) # no masked values type(A.mean()) #numpy.float64 type(B.mean()) #numpy.ma.core.MaskedArray Other numpy.ma.core.MaskedArray methods seem to be consistent type( A.sum()) == type(B.sum()) # True type( A.prod()) == type(B.prod()) # True type( A.std()) == type(B.std()) # True type( A.mean()) == type(B.mean()) # False Can someone explain this? UPDATE: As pointed out in the comments C = np.ma.masked

How to properly mask a numpy 2D array?

好久不见. 提交于 2019-11-29 03:10:40
Say I have a two dimensional array of coordinates that looks something like x = array([[1,2],[2,3],[3,4]]) Previously in my work so far, I generated a mask that ends up looking something like mask = [False,False,True] When I try to use this mask on the 2D coordinate vector, I get an error newX = np.ma.compressed(np.ma.masked_array(x,mask)) >>>numpy.ma.core.MaskError: Mask and data not compatible: data size is 6, mask size is 3.` which makes sense, I suppose. So I tried to simply use the following mask instead: mask2 = np.column_stack((mask,mask)) newX = np.ma.compressed(np.ma.masked_array(x

How to properly mask a numpy 2D array?

故事扮演 提交于 2019-11-27 15:48:11
问题 Say I have a two dimensional array of coordinates that looks something like x = array([[1,2],[2,3],[3,4]]) Previously in my work so far, I generated a mask that ends up looking something like mask = [False,False,True] When I try to use this mask on the 2D coordinate vector, I get an error newX = np.ma.compressed(np.ma.masked_array(x,mask)) >>>numpy.ma.core.MaskError: Mask and data not compatible: data size is 6, mask size is 3.` which makes sense, I suppose. So I tried to simply use the