如何修复“无法将输入转换为时间戳” ARIMA.predict

时间:2018-12-25 20:19:07

标签: python forecasting arima

我需要通过检查ARIMA模型的r2分数来对其进行检查。所以我需要做ARIMA.predict,但这是一个错误:

  

TypeError:无法转换输入[DatetimeIndex(['2014-08-10 06:00:00','2014-05-05 16:00:00',                 '2014-04-28 20:00:00','2014-03-27 21:00:00',                 '2012-08-26 09:00:00','2012-09-29 08:00:00',                 '2013-02-15 03:00:00','2013-02-28 09:00:00',                 '2014-06-27 06:00:00','2014-01-18 11:00:00',                 ...                 '2013-11-10 22:00:00','2013-03-18 21:00:00',                 '2013-09-09 00:00:00','2013-06-08 21:00:00',                 '2013-11-11 12:00:00','2014-07-07 05:00:00',                 '2014-07-27 12:00:00','2014-06-03 23:00:00',                 '2012-09-20 12:00:00','2012-12-18 22:00:00'],                dtype ='datetime64 [ns]',name ='Datetime',length = 3658,> freq = None)],类型为> Timestamp

这是我的代码:

dateparse = lambda dates: pd.datetime.strptime(dates, "%d-%m-%Y %H:%M")
train=pd.read_csv("D:/Coding/Datasets/train_traffic.csv", parse_dates= 
['Datetime'], index_col='Datetime',date_parser=dateparse)

X_train, X_test, y_train, y_test = ms.train_test_split(train.index, 
train.Count, test_size=0.20, random_state=5)

model = ARIMA(ts_log, order=(2, 1, 0), freq='H')  
AR = model.fit(disp=-1)
AR.predict(X_test)

数据示例和类型:在Excel中:2012年5月25日00:00。

不带参数的pd.read_csv:

Out:dtype('O') 
Out:'25-08-2012 00:00'

具有参数:

dateparse = lambda dates: pd.datetime.strptime(dates, "%d-%m-%Y %H:%M")
pd.read_csv("D:/Coding/Datasets/train_traffic.csv", parse_dates=['Datetime'], index_col='Datetime',date_parser=dateparse)

Out:dtype('<M8[ns]')
df.index[0]
Out:Timestamp('2012-08-25 00:00:00')

我也尝试过

pd.read_csv("D:/Coding/Datasets/train_traffic.csv", index_col='Datetime').index[0]

Out:'25-08-2012 00:00'
Out:dtype('O')

谢谢!

1 个答案:

答案 0 :(得分:0)

很难理解这个问题,确切的问题出在哪里,但我会尽力为您提供可复制的代码。

在此示例中,我使用了Daily total female births in California dataset

import pandas as pd
from statsmodels.tsa.arima_model import ARIMA
from sklearn.model_selection import TimeSeriesSplit

df = pd.read_csv("daily-total-female-births-in-cal.csv", nrows = 365)
df.set_index("Date", inplace = True)

train = df.iloc[0:300, :]
test = df.iloc[300:, :]

arima = ARIMA(train, order = (1,1,0), freq = 'D').fit(disp = 0)
prediction = arima.predict(test.index[0], test.index[-1], dynamic = True)

您不应将sklearn中的train_test_split()用于时间序列问题。您还应该从sklearn使用TimeSeriesSplit。