← 返回题库
初级

简单预测方法

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
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()
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码