python – 使用ARMA的Statsmodel

前端之家收集整理的这篇文章主要介绍了python – 使用ARMA的Statsmodel前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这里有点新,但试图使用statsmodel ARMA预测工具.我从雅虎导入了一些股票数据并得到ARMA给我适合的参数.但是,当我使用预测代码时,我收到的是一个错误列表,我似乎无法弄清楚.不太确定我在这里做错了什么:

import pandas
import statsmodels.tsa.api as tsa
from pandas.io.data import DataReader

start = pandas.datetime(2013,1,1)
end = pandas.datetime.today()

data = DataReader('GOOG','yahoo')
arma =tsa.ARMA(data['Close'],order =(2,2))
results= arma.fit()
results.predict(start=start,end=end)

错误是:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
C:\Windows\system32\404             #elif 'mle' not in method or dynamic: # should be on a date

    405             start = _validate(start,k_ar,k_diff,self.data.dates,--> 406                               method)
    407             start = super(ARMA,self)._get_predict_start(start)
    408         _check_arima_start(start,method,dynamic)

D:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7.egg\statsmodels\tsa\arima_
model.pyc in _validate(start,dates,method)
    160     if isinstance(start,(basestring,datetime)):
    161         start_date = start
--> 162         start = _index_date(start,dates)
    163         start -= k_diff
    164     if 'mle' not in method and start < k_ar - k_diff:

D:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7.egg\statsmodels\tsa\base\d
atetools.pyc in _index_date(date,dates)
     37         freq = _infer_freq(dates)
     38         # we can start prediction at the end of endog

---> 39         if _idx_from_dates(dates[-1],date,freq) == 1:
     40             return len(dates)
     41

D:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7.egg\statsmodels\tsa\base\d
atetools.pyc in _idx_from_dates(d1,d2,freq)
     70         from pandas import DatetimeIndex
     71         return len(DatetimeIndex(start=d1,end=d2,---> 72                                  freq = _freq_to_pandas[freq])) - 1
     73     except ImportError,err:
     74         from pandas import DateRange

D:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7.egg\statsmodels\tsa\base\d
atetools.pyc in __getitem__(self,key)
     11         # being lazy,don't want to replace dictionary below

     12         def __getitem__(self,key):
---> 13             return get_offset(key)
     14     _freq_to_pandas = _freq_to_pandas_class()
     15 except ImportError,err:

D:\Python27\lib\site-packages\pandas\tseries\frequencies.pyc in get_offset(name)

    484     """
    485     if name not in _dont_uppercase:
--> 486         name = name.upper()
    487
    488         if name in _rule_aliases:

AttributeError: 'NoneType' object has no attribute 'upper'
最佳答案
对我来说看起来像个错误.我会调查一下.

https://github.com/statsmodels/statsmodels/issues/712

编辑:作为一种解决方法,您可以从DataFrame中删除DatetimeIndex并将其传递给numpy数组.它使得预测在日期方面变得有点棘手,但是当没有频率时使用日期进行预测已经相当棘手,因此只有开始和结束日期基本上没有意义.

import pandas
import statsmodels.tsa.api as tsa
from pandas.io.data import DataReader
import pandas

data = DataReader('GOOG','yahoo')
dates = data.index

# start at a date on the index
start = dates.get_loc(pandas.datetools.parse("1-2-2013"))
end = start + 30 # "steps"

# NOTE THE .values
arma =tsa.ARMA(data['Close'].values,2))
results= arma.fit()
results.predict(start,end)
原文链接:https://www.f2er.com/python/439495.html

猜你在找的Python相关文章