![](/img/trans.png)
[英]How to get constant term in AR Model with statsmodels and Python?
[英]AR model in statsmodels
我正在嘗試在Python中擬合時間序列自動回歸模型
輸入DF:
code test_col
2018-09-20 18:00:00 10
2018-09-20 19:00:00 20
2018-09-20 20:00:00 21
2018-09-20 21:00:00 17
2018-09-20 22:00:00 7
DF的索引:
DatetimeIndex(['2018-09-20 18:00:00'.......]
模型:
mod = AR(DF[test_col])
res = mod.fit(maxlag= 20, ic= 'aic')
last_hour = df.index[[len(df)-1]]
pred = res.predict(start=last_hour[0],end = last_hour[0] )
last_hour =>從我要預測的索引中獲取最新的時間戳
錯誤:
File "pandas/tslib.pyx", line 1280, in pandas.tslib._Timestamp.__sub__ (pandas/tslib.c:23914)
TypeError: descriptor '__sub__' requires a 'datetime.datetime' object but received a 'int'
我檢查了“ last_hour”的類型
print (type(last_hour))
<class 'pandas.tseries.index.DatetimeIndex'>
有關如何糾正此問題的任何建議。
將熊貓從V-19更新到23.解決了此問題。
我認為日期時間索引在數據框中的投放方式存在問題,因為以下代碼行對我有用:
import pandas as pd
from statsmodels.tsa.ar_model import AR
DF = pd.DataFrame({'code': ['2018-09-20 18:00:00', '2018-09-20 19:00:00', '2018-09-20 20:00:00', '2018-09-20 21:00:00', '2018-09-20 22:00:00'],
'test_col': [10, 20, 21, 17, 7]})
DF['code'] = pd.to_datetime(DF['code'])
DF = DF.set_index('code')
mod = AR(DF['test_col'])
res = mod.fit(maxlag= 2, ic= 'aic')
last_hour = DF.index[[len(DF)-1]]
pred = res.predict(start=last_hour[0],end = last_hour[0])
檢查last_hour對象給出
print(last_hour)
DatetimeIndex(['2018-09-20 22:00:00'], dtype='datetime64[ns]', name='code', freq=None)
可以嘗試的一件事是reset_index,然后將列轉換為datetime,然后再次將其設置為index。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.