初级
简单预测方法
未完成
初级参考
完整示例代码供参考,建议自己理解后重新输入
def solve():
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(42)
n = 150
train_size = 120
time = np.arange(n)
ts = 0.05 * time + 10 * np.sin(2 * np.pi * time / 50) + np.random.normal(0, 3, n)
train, test = ts[:train_size], ts[train_size:]
naive_pred = np.full(len(test), train[-1])
window = 10
ma_pred = np.full(len(test), np.mean(train[-window:]))
plt.figure(figsize=(12, 6))
plt.plot(time[:train_size], train, 'b-', label='训练数据')
plt.plot(time[train_size:], test, 'g-', label='测试数据')
plt.plot(time[train_size:], naive_pred, 'r--', label='朴素预测')
plt.plot(time[train_size:], ma_pred, 'm--', label=f'{window}期移动平均预测')
plt.xlabel('时间')
plt.ylabel('值')
plt.title('时间序列预测')
plt.legend()
plt.grid(alpha=0.3)
plt.show()
👑
升级 VIP
解锁全部题目,畅通无阻地学习
- ✓ 解锁全部训练包所有题目
- ✓ 查看完整参考代码和提示
- ✓ 浏览器内直接运行 Python 代码
- ✓ 自动批改 + 进度追踪
30天
¥18
1年
¥99
2年
¥158
3年
¥199