gpt4 book ai didi

Python 时间序列 Dickey-Fuller ValueError : too many values to unpack (expected 2)

转载 作者:行者123 更新时间:2023-11-30 22:20:22 29 4
gpt4 key购买 nike

我正在尝试执行时间序列分析,在此过程中我将执行迪基富勒测试来检查数据帧的平稳性。

我不断收到错误ValueError:解包值太多(预期为 2)。我已从数据框中删除了带有 NaN 的所有行。我唯一能想到的是 dftest[0:4] (在下面代码的第 4 行中)和 dftest[4] (在第 6 行中) 。我不知道这些值的含义,这可能会导致错误。我尝试使用 Shift Tab 来获取解释,但没有任何帮助。我也尝试过 dftest[0:1] ,但没有成功。仅供引用,我的数据框只有 2 列

from statsmodels.tsa.stattools import adfuller
def test_stationarity(homepriceTS):

#Determing rolling statistics
rolmean = pd.rolling_mean(homepriceTS, window=12)
rolstd = pd.rolling_std(homepriceTS, window=12)

#Plot rolling statistics:
orig = plt.plot(homepriceTS, 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(homepriceTS, 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
dfoutput

我一直在这里一步步跟随相当好的时间序列: https://www.analyticsvidhya.com/blog/2016/02/time-series-forecasting-codes-python/

最佳答案

您需要将适当的系列传递给test_stationarity()

如果您的时间序列采用以下格式:

ts.head()

value
month
2015-08-01 120
2015-09-01 130
2015-10-01 133
2015-11-01 178
2015-12-01 135
...

试试这个:test_stationarity(ts['value'])

这会将数据帧转换为正确的序列,并且是函数所期望的。或者您可以在传递给函数之前对其进行转换:

ts = ts['value']
ts.head()

month
2015-08-01 120
2015-09-01 130
2015-10-01 133
2015-11-01 178
2015-12-01 135
...
test_stationarity(ts)

虽然我不能确定这就是你的确切问题,因为没有示例数据,但我最近在测试时间序列时遇到了相同的错误消息,我可以肯定地说,传递未转换的时间序列会抛出相同的错误消息ValueError:解包值太多(预期为 2) 消息。

关于Python 时间序列 Dickey-Fuller ValueError : too many values to unpack (expected 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48855737/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com