我需要用numpy中的局部正态分布替换一维数组的NaN。我选择一个窗口,计算该窗口的均值和标准差,然后使用正态分布替换NaN,而其余信号保持不变。
import numpy as np
def replace_nan(signal, window = 5):
"""
calculate moving average and std of signal window without nan
replaces nan values with normal distribution (mean, std)
"""
# add padding in case signal starts/ends with nan
signal = np.pad(signal, (window, window), 'mean', stat_length = 2*window)
for k in range(window,len(signal)-window):
mean = np.nanmean(signal[k-window:k+window]) # window average
std = np.nanstd(signal[k-window:k+window]) # window std without nan
ind = np.where(np.isnan(signal[k-window:k+window]))[0]
print (ind)
signal[ind]= np.random.normal(mean, std)
signal = signal[window:len(signal)-window] #remove padding
return signal
#tester
signal = np.array([0.71034849, 0.17730998, 0.77577915, 0.38308111,
0.24278947, np.nan, np.nan, 0.68694097, 0.6684736 , 0.47310845, 0.22210945,
0.1189111, np.nan, np.nan, np.nan, 0.5573841 , 0.57531205, 0.74131346,
0.29088101, 0.5573841 , 0.57531205, 0.74131346, np.nan, np.nan, np.nan,
np.nan, 0.49534304, 0.18370482, 0.06089498, 0.22210945, 0.1189111])
signal = replace_nan(signal, 5)
print(signal)
我将 nans 替换为正态分布 np.random.normal() ,并为大小为 5 的移动窗口计算均值和标准差。当我选择信号窗口的那些 nans 来替换它们时,出现了问题。这应该很容易,我只是python的完整入门者。
鸿蒙传说
相关分类