1

我想使用 pd.rolling_mean() 作为保持最大信息标准的平滑函数。这意味着端点的计算方式取决于可用的信息。下面是一个 window=3, center=True 的示例:

For Example: Window = 3, Center = True
ts_smooth[0] = 1/2 * ts[0] + 1/2 * ts[1]
ts_smooth[0<n<N-1] = 1/3 * ts[n-1] + 1/3 * ts[n] + 1/3 * ts[n+1]
ts_smooth[N] = 1/2 * ts[N-1] + 1/2 * ts[N]

在 Pandas 中实现这一目标的最佳方法是什么?

  1. 计算中点的 rolling_mean()
  2. 写一个函数来根据窗口大小替换结束条件?
4

1 回答 1

0

你可以使用 shift 函数,像这样,

ts_shiftedPlus = ts.shift(1)
ts_shiftedMinus = ts.shift(-1)

ts_smooth = 1/3 * ts_shiftedMinus + 1/3 * ts + 1/3 * ts_shiftedPlus
ts_smooth.ix[0] = 1/2 * ts.ix[0] + 1/2 * ts.ix[1]
N = len(ts)
ts_smooth.ix[N] = 1/2 * ts.ix[N-1] + 1/2 * ts.ix[N]
于 2013-03-19T19:51:57.733 回答