Python时间序列DickeyFuller值错误:太多的值无法解包(应为2)

2024-06-10 21:12:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图进行时间序列分析,在这个过程中,我在执行Dickey-Fuller测试来检查数据帧的平稳性。在

我一直收到错误ValueError: too many values to unpack (expected 2)。我已经从数据帧中删除了任何带有NaN的行。我唯一能想到的是dftest[0:4](在下面代码的第4行)和{}(在第6行)。我不知道这些值意味着什么,这可能会导致错误。我试着用shift tab来解释,但没有任何帮助。我也试过dftest[0:1],但没用。我的数据框只有两列

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/


Tags: plotvalue时间plt序列labelcolorpd
3条回答

您需要将一个适当的序列传递给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'])

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

^{pr2}$

虽然我不能确定这是您的问题,因为没有示例数据,但我最近在测试时间序列时遇到了相同的错误消息,我可以肯定地说,传递未转换的时间序列将抛出相同的ValueError: too many values to unpack (expected 2)消息。在

我正在添加我所做的小代码更改:

导入少数库

 import statsmodels.api as sm
    from statsmodels.tsa.stattools import adfuller, kpss
    def test_stationarity(timeseries):

为了迪基·富勒的测试

^{pr2}$

希望这能有所帮助。在

线

for key,value in dftest[4].items():

在我看来,代码中唯一需要解包值的地方。在这种情况下,dftest[4].items()必须是这些赋值右边的项目之一:

>>> k,v = 1,2
>>> k,v = 2,[3,4,5]
>>> k,v = [1,2], [3,4,5]
>>> k,v = [1,2], {2: 3}

如果我处于您的位置,我会在for循环之前打印dftest[4].items(),看看它是什么样的结构。(我猜这是一张单子)或者,再看看医生。在

相关问题 更多 >