Pandas:使用rolling_mean()与最大信息准则作为平滑函数?

1 投票
1 回答
559 浏览
提问于 2025-04-17 17:56

我想用 pd.rolling_mean() 这个函数来做平滑处理,同时保留最多的信息。这意味着在计算边界点时,会根据可用的信息来进行不同的处理。下面是一个窗口大小为3,居中对齐的例子:

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. 写一个函数,根据窗口大小来替换边界条件?

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]

撰写回答