运用ARIMA算法进行预测的代码
时间: 2024-01-26 08:02:29 浏览: 57
import pandas as pd
import numpy as np
from statsmodels.tsa.arima_model import ARIMA
import matplotlib.pyplot as plt
# 读取数据
data = pd.read_csv('data.csv', index_col='date', parse_dates=True)
data.index.freq = 'D'
# 拆分数据集
train_data = data[:'2019-12-31']
test_data = data['2020-01-01':]
# ARIMA模型
model = ARIMA(train_data, order=(1, 1, 1))
model_fit = model.fit(disp=0)
# 预测结果
forecast = model_fit.forecast(steps=len(test_data))[0]
forecast = pd.Series(forecast, index=test_data.index)
# 绘图
plt.plot(train_data, label='Train')
plt.plot(test_data, label='Test')
plt.plot(forecast, label='Forecast')
plt.legend()
plt.show()
阅读全文