我正在使用 ARIMA 在 Python 中进行预测,以下是我的代码:import numpy as np import pandas as pd import matplotlib.pyplot as plt from statsmodels.tsa.seasonal import seasonal_decomposefrom sklearn import datasets, linear_modelfrom sklearn.model_selection import train_test_splitHSBC = pd.read_csv('HSBC.csv', index_col = 'Date', parse_dates = True)HSBC2 = HSBC['Close']result = seasonal_decompose(HSBC2, model='multiplicative', period = 1)from pmdarima import auto_arimaimport warningswarnings.filterwarnings("ignore")stepwise_fit = auto_arima(HSBC2, start_p = 1, start_q = 1, max_p = 3, max_q = 3, m = 12, start_P = 0, seasonal = True, d = None, D = 1, trace = True, error_action ='ignore', suppress_warnings = True, stepwise = True) train = HSBC2[0:173]test = HSBC2[173:248]model = SARIMAX(train, order = (0, 1, 1), seasonal_order =(0,1,1,12)) result = model.fit()start = len(train)end = len(train) + len(test) - 1prediction = result.predict(start,end, typ = 'levels').rename("Predictions") predictions.plot(legend = True) test.plot(legend = True)我很困惑为什么预测图的 x 轴变成数字,它应该是像测试图一样的日期。
1 回答
幕布斯7119047
TA贡献1794条经验 获得超8个赞
如果我没有错,这是由于您没有指定索引的频率。尝试这个:
HSBC.index = pd.date_range(freq='d', start=HSBC.index[0], periods=len(HSBC)
请注意,如果您的索引是每日间隔的,您应该频率='d'
编辑:
所以,答案就是改变 predict 方法的参数 start 和 end 参数,例如:
start = test['Date'].iloc[0]
end = test['Date'].iloc[-1]
prediction = result.predict(start,end,
typ = 'levels').rename("Predictions")
添加回答
举报
0/150
提交
取消