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 it to replace NaNs.
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. I am new to python and IT.
PS Please do not down vote, I am very new to all this. I got some student following me and down voting my questions as soon as they arise. If I can "hide" my questions from a particular person/negative follower please let me know. Thank you
Aucun commentaire:
Enregistrer un commentaire