如何用统计分析创建移动平均函数?

问题描述 投票:2回答:2
def adf(ts):

# Determing rolling statistics
rolmean = pd.rolling_mean(ts, window=12)
rolstd = pd.rolling_std(ts, window=12)
#Plot rolling statistics:
orig = plt.plot(ts, color='blue',label='Original')
mean = plt.plot(rolmean, color='red', label='Rolling Mean')
std = plt.plot(rolstd, color='black', label = 'Rolling Std')
plt.legend(loc='best')
plt.title('Rolling Mean & Standard Deviation')
plt.show(block=False)

# Calculate ADF factors
adftest = adfuller(ts, autolag='AIC')
adfoutput = pd.Series(adftest[0:4], index=['Test Statistic','p-value','# of Lags Used',
                                          'Number of Observations Used'])
for key,value in adftest[4].items():
    adfoutput['Critical Value (%s)'%key] = value
return adfoutput**

上面我创建了计算MA窗口5的函数。但是当我运行以下代码时我得到错误..

df['priceModLogMA12'] = pd.rolling_mean(df.priceModLog, window = 5)**
AttributeError: module 'pandas' has no attribute 'rolling_mean'
python pandas statistics time-series moving-average
2个回答
2
投票

我以为我们应该用

rolmean = ts.rolling(window=12).mean()

代替

rolmean = pd.rolling_mean(ts, window=12)

由于不推荐使用pd.rolling_mean

编辑

只是改变

rolmean = pd.rolling_mean(ts, window=12)
rolstd = pd.rolling_std(ts, window=12)

rolmean = ts.rolling(window=12).mean()
rolstd = ts.rolling(window=12).std()

编辑

如果你正在谈论这一行改变它

df['priceModLogMA12'] = pd.rolling_mean(df.priceModLog, window = 5)

df['priceModLogMA12'] = df.priceModLog.rolling(window = 5).mean()

1
投票

在大熊猫中删除了rolling_mean。相反,你应该使用pandas.DataFrame.rolling然后应用mean()。看看here。您可以像这样编辑它:

ts.rolling(window=12).mean()
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.