gpt4 book ai didi

python-2.7 - 我正在尝试在 Python 中的 statsmodels 中运行 Dickey-Fuller 测试,但出现错误

转载 作者:行者123 更新时间:2023-12-04 16:42:29 28 4
gpt4 key购买 nike

我正在尝试在 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

最佳答案

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

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

然后,adfuller 将能够读取数据。

关于python-2.7 - 我正在尝试在 Python 中的 statsmodels 中运行 Dickey-Fuller 测试,但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43100441/

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