from statsmodels.tsa.stattools import adfuller
def test_stationarity(timeseries):
#Determing rolling statistics
rolmean = pd.rolling_mean(timeseries, window=24) # 24 hours on each day
rolstd = pd.rolling_std(timeseries, window=24)
#Plot rolling statistics:
orig = plt.plot(timeseries, 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)
#Perform Dickey-Fuller test:
print ('Results of Dickey-Fuller Test:')
dftest = adfuller(timeseries, autolag='AIC')
dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
for key,value in dftest[4].items():
dfoutput['Critical Value (%s)'%key] = value
print (dfoutput)
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 20,10
test_stationarity(train_original['Count'])
答案 0 :(得分:1)
我将假定标题错误应为AttributeError: module 'pandas' has no attribute 'rolling_mean'
,因为我认为它已被弃用然后再删除。另外,pd.rolling
不在提供的代码中。在这种情况下,您应该使用rolling
。假设timeseries
是pd.Series
:
from statsmodels.tsa.stattools import adfuller
def test_stationarity(timeseries):
#Determing rolling statistics
rolmean = timeseries.rolling(window=24).mean() # 24 hours on each day
rolstd = timeseries.rolling(window=24).std()
答案 1 :(得分:1)
尝试执行 conda install -c conda-forge statsmodels,根据之前的语法,笔记本无法识别 adfuller。然后尝试 pip uninstall pandas 并执行 pip install pandas。对于类似情况,可以尝试链接 Numpy is installed but still getting error
中的说明答案 2 :(得分:0)
from statsmodels.tsa.stattools import adfuller
def test_stationarity(timeseries):
#determine rolling statistics
rolmean = pd.Series(timeseries).rolling(window=24).mean()#24 hours on each day
rolstd = pd.Series(timeseries).rolling(window=24).std()
#plot rolling statistics
orig = plt.plot(timeseries,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 and standard deviation')
plt.show(block = False)
#perform dickey fuller test
print('result of dickey fuller test:')
dftest = adfuller(timeseries,autolag = 'AIC')
dfoutput = pd.Series(dftest[0:4],index = ['Test statistics', 'p-value', '#lags used', 'number of observation used'])
for key,value in dftest[4].items():
dfoutput['critical value (%s)'%key] = value
print(dfoutput)
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 20,10
test_stationarity(train_original['Count'])