python实现用近24个月的数据预测未来24个月的数据时间序列
实现时间序列预测可以使用Python中的多种库和算法,比如ARIMA、SARIMA、LSTM等。下面是一个使用ARIMA模型进行时间序列预测的示例代码:
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.arima.model import ARIMA
# 读取数据
data = pd.read_csv('data.csv', parse_dates=['date'], index_col='date')
# 按照月份进行重采样
data_monthly = data.resample('M').sum()
# 拆分训练集和测试集
train = data_monthly[:-24]
test = data_monthly[-24:]
# 训练ARIMA模型
model = ARIMA(train, order=(1, 1, 1))
model_fit = model.fit()
# 预测未来24个月的数据
forecast = model_fit.forecast(steps=24)[0]
# 可视化结果
plt.plot(train, label='Train')
plt.plot(test, label='Test')
plt.plot(test.index, forecast, label='Forecast')
plt.legend()
plt.show()
在代码中,首先读取时间序列数据,并按照月份进行重采样。然后将数据分为训练集和测试集,其中训练集包括过去的数据,测试集包括未来的数据。接下来,使用ARIMA模型拟合训练集的数据,并使用该模型进行未来24个月的预测。最后,将训练集、测试集和预测结果进行可视化展示。
请确保将data.csv替换为实际的数据文件名,并根据需要调整ARIMA模型的参数
原文地址: http://www.cveoy.top/t/topic/iVRo 著作权归作者所有。请勿转载和采集!