【问题标题】:I am trying to run Dickey-Fuller test in statsmodels in Python but getting error我正在尝试在 Python 的 statsmodels 中运行 Dickey-Fuller 测试,但出现错误
【发布时间】:2017-08-23 08:24:17
【问题描述】:

我正在尝试在 Python 的 statsmodels 中运行 Dickey-Fuller 测试,但出现错误 P 从 python 2.7 和 Pandas 版本 0.19.2 运行。数据集来自 Github,导入相同

enter code here

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

    print 'Results of Dickey-Fuller Test:'
        dftest = ts.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


    test_stationarity(tr)

给我以下错误:

Results of Dickey-Fuller Test:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-10ab4b87e558> in <module>()
----> 1 test_stationarity(tr)

<ipython-input-14-d779e1ed35b3> in test_stationarity(timeseries)
     19     #Perform Dickey-Fuller test:
     20     print 'Results of Dickey-Fuller Test:'
---> 21     dftest = ts.adfuller(timeseries, autolag='AIC' )
     22     #dftest = adfuller(timeseries, autolag='AIC')
     23     dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])

C:\Users\SONY\Anaconda2\lib\site-packages\statsmodels\tsa\stattools.pyc in adfuller(x, maxlag, regression, autolag, store, regresults)
    209 
    210     xdiff = np.diff(x)
--> 211     xdall = lagmat(xdiff[:, None], maxlag, trim='both', original='in')
    212     nobs = xdall.shape[0]  # pylint: disable=E1103
    213 

C:\Users\SONY\Anaconda2\lib\site-packages\statsmodels\tsa\tsatools.pyc in lagmat(x, maxlag, trim, original)
    322     if x.ndim == 1:
    323         x = x[:,None]
--> 324     nobs, nvar = x.shape
    325     if original in ['ex','sep']:
    326         dropidx = nvar

ValueError: too many values to unpack

【问题讨论】:

  • 您的问题不在于 dickey-fuller...您的问题在于元组没有您想象的那么多值。
  • 我猜timeseries 不是一个系列。不支持np.asarray(timeseries).ndim &gt; 1

标签: python-2.7 pandas jupyter-notebook


【解决方案1】:

我假设因为您使用的是 Dickey-Fuller 测试。您希望维护时间序列,即日期时间列作为索引。所以为了做到这一点。

tr=tr.set_index('Month') #I am assuming here the time series column name is Month ts = tr['othercoulumnname'] #Just use the other column name here it might be count or anything

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    只需将行更改为:

    dftest = adfuller(timeseries.iloc[:,0].values, autolag='AIC' )
    

    它会起作用的。 adfuller 需要一维数组列表。在您的情况下,您有一个数据框。因此,如上所述提及该列或编辑该行。

    【讨论】:

      【解决方案3】:

      tr 必须是一维数组,如您所见 here。我不知道你的情况是什么 tr 。假设您将 tr 定义为包含时间序列数据的数据框,您应该执行以下操作:

      tr = tr.iloc[:,0].values
      

      然后adfuller就可以读取数据了。

      【讨论】:

        猜你喜欢
        • 2017-07-01
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 2016-02-01
        • 2012-05-26
        • 2018-12-19
        • 2021-04-18
        相关资源
        最近更新 更多