Pandas:使用具有最大信息标准的rolling_mean()作为平滑函数?

时间:2013-03-05 01:27:12

标签: python pandas

我想使用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]

在熊猫中实现这一目标的最佳途径是什么?

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

1 个答案:

答案 0 :(得分: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]