你可以给我一个 时间序列预测的python源码吗
时间: 2024-06-12 21:04:54 浏览: 147
以下是一个基于ARIMA模型的时间序列预测的Python源代码示例:
```
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.arima_model import ARIMA
# 导入数据
data = pd.read_csv('data.csv', index_col='date', parse_dates=True)
data = data.resample('MS').mean()
# 可视化数据
plt.figure(figsize=(10, 5))
plt.plot(data)
plt.xlabel('Year')
plt.ylabel('Sales')
plt.title('Monthly Sales')
plt.show()
# 拆分数据集
train_size = int(len(data) * 0.8)
train_data, test_data = data[:train_size], data[train_size:]
# ARIMA模型拟合
model = ARIMA(train_data, order=(1, 1, 1))
model_fit = model.fit(disp=0)
# 预测未来值
forecast = model_fit.forecast(steps=len(test_data))[0]
# 可视化预测结果
plt.figure(figsize=(10, 5))
plt.plot(train_data, label='Train')
plt.plot(test_data, label='Test')
plt.plot(test_data.index, forecast, label='Forecast')
plt.xlabel('Year')
plt.ylabel('Sales')
plt.title('Monthly Sales Prediction')
plt.legend()
plt.show()
# 计算误差指标
mse = np.mean((forecast - test_data.values) ** 2)
rmse = np.sqrt(mse)
mape = np.mean(np.abs((forecast - test_data.values) / test_data.values)) * 100
print('RMSE: %.2f' % rmse)
print('MAPE: %.2f%%' % mape)
```
其中,data.csv文件是一个包含时间序列数据的CSV文件,'date'列是时间戳,'Sales'列是要预测的值。该代码示例拟合了一个ARIMA(1,1,1)模型,并将未来的预测值可视化出来。最后,计算了一些误差指标,如RMSE和MAPE,以评估模型的预测性能。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)