mardi 21 août 2018

numpy: replace nan with normal distribution of the moving window

I need to replace NaNs of 1D array with normal distribution of the moving window in numpy. I mask the array, calculate moving average and then use to replace NaNs:

ave[signal.mask] = np.random.normal(np.mean(ave[~signal.mask]), \
np.std(ave[~signal.mask]), len(ave[signal.mask])) 

It works ok for a small window, but for the large window it introduces too much smoothing as I replace signal with moving average. What I want is to replace only nans in the signal with normal distribution np.random.normal(mean, std) of the moving window, while the rest of the signal stays the same.

signal = np.ma.masked_array(signal,np.isnan(signal))
# moving average with window size = n
ave = np.cumsum(signal.filled(0))
ave[n:] = ave[n:] - ave[:-n]
counts = np.cumsum(~signal.mask)
counts[n:] = counts[n:] - counts[:-n]
ave[~signal.mask] /= counts[~signal.mask]
ave[signal.mask] = np.random.normal(np.mean(ave[~signal.mask]), \
np.std(ave[~signal.mask]), len(ave[signal.mask])) 

Maybe somebody could help me to fix this code. My tries did not compile. Thank you




Aucun commentaire:

Enregistrer un commentaire