【发布时间】:2018-05-09 03:43:13
【问题描述】:
This question 有很多关于如何获得移动平均线的有用答案。 我已经尝试了 numpy 卷积和 numpy cumsum 两种方法,并且在示例数据集上都运行良好,但在我的真实数据上生成了一个较短的数组。
数据以0.01 分隔。示例数据集长度为50,真实数据数万。所以它一定是导致问题的窗口大小,我不太明白函数中发生了什么。
这是我定义函数的方式:
def smoothMAcum(depth,temp, scale): # Moving average by cumsum, scale = window size in m
dz = np.diff(depth)
N = int(scale/dz[0])
cumsum = np.cumsum(np.insert(temp, 0, 0))
smoothed=(cumsum[N:] - cumsum[:-N]) / N
return smoothed
def smoothMAconv(depth,temp, scale): # Moving average by numpy convolution
dz = np.diff(depth)
N = int(scale/dz[0])
smoothed=np.convolve(temp, np.ones((N,))/N, mode='valid')
return smoothed
然后我实现它:
scale = 5.
smooth = smoothMAconv(dep,data, scale)
但是print len(dep), len(smooth)
返回81071 80572
如果我使用其他功能,也会发生同样的情况。 如何获得与数据长度相同的平滑数组?
为什么它在小数据集上起作用?即使我尝试不同的比例(并为示例和数据使用相同的比例),示例中的结果与原始数据具有相同的长度,但在实际应用程序中却不同。
我考虑了nan 值的影响,但如果我在示例中有nan,它没有任何区别。
如果没有完整的数据集,如果可能的话,问题出在哪里?
【问题讨论】:
-
可以通过模拟数据(例如我的答案中的随机数组)包含一个具有大数据集的可重现示例。