predict statsmodel argument Error

末鹿安然 提交于 2019-12-10 19:56:40

问题


I am trying to predict outofsample values for an array. Python code:

import pandas as pd
import numpy as np
from statsmodels.tsa.arima_model import ARIMA

    dates = pd.date_range('2012-07-09','2012-07-30')
    series = [43.,32.,63.,98.,65.,78.,23.,35.,78.,56.,45.,45.,56.,6.,63.,45.,64.,34.,76.,34.,14.,54.]
    res = pd.Series(series, index=dates)
    r = ARIMA(res,(1,2,0))
    pred = r.predict(start='2012-07-31', end='2012-08-31')

I am getting this error.I see I have given two argument but compiler return I have given 3.

Traceback (most recent call last):
  File "XXXXXXXXX/testfile.py", line 12, in <module>
    pred = r.predict(start='2012-07-31', end='2012-08-31')
TypeError: predict() takes at least 2 arguments (3 given)

Please help


回答1:


The call signature of ARIMA.predict is

predict(self, params, start=None, end=None, exog=None, dynamic=False)

Thus, when you call r.predict(start='2012-07-31', end='2012-08-31'), self gets bound to r, and values are bound to start and end but the required positional arument params does not get bound. That is why you get the error

TypeError: predict() takes at least 2 arguments (3 given)

Unfortunately the error message is misleading. The "3 given" refer to r, start and end. The "2 arguments" refer to the two required arguments, self and params. The problem is that the required positional argument params was not given.

To fix the problem, you need parameters. Usually you find those parameters by fitting:

r = r.fit()

before calling

pred = r.predict(start='2012-07-31', end='2012-08-31')

r.fit() returns a statsmodels.tsa.arima_model.ARIMAResultsWrapper which have the parameters "baked in" so calling ARIMAResultWrapper.fit does not require passing params.


import pandas as pd
import numpy as np
from statsmodels.tsa.arima_model import ARIMA

dates = pd.date_range('2012-07-09','2012-07-30')
series = [43.,32.,63.,98.,65.,78.,23.,35.,78.,56.,45.,45.,56.,6.,63.,45.,64.,34.,76.,34.,14.,54.]
res = pd.Series(series, index=dates)
r = ARIMA(res,(1,2,0))
r = r.fit()
pred = r.predict(start='2012-07-31', end='2012-08-31')
print(pred)

yields

2012-07-31   -39.067222
2012-08-01    26.902571
2012-08-02   -17.027333
...
2012-08-29     0.532946
2012-08-30     0.532447
2012-08-31     0.532780
Freq: D, dtype: float64


来源:https://stackoverflow.com/questions/36717603/predict-statsmodel-argument-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!