jokeroor jokeroor - 7 months ago 56
Python Question

predict statsmodel argument Error

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

Answer

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)

The "3 given" refer to r, start and end. The "2 arguments" refer to the two required arguments, self and params.

Unfortunately the error message is misleading. Obviously, 3 is greater than 2. 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
Comments