• 第81步 时间序列建模实战:Adaboost回归建模


    基于WIN10的64位系统演示

    一、写在前面

    这一期,我们介绍AdaBoost回归。

    同样,这里使用这个数据:

    《PLoS One》2015年一篇题目为《Comparison of Two Hybrid Models for Forecasting the Incidence of Hemorrhagic Fever with Renal Syndrome in Jiangsu Province, China》文章的公开数据做演示。数据为江苏省2004年1月至2012年12月肾综合症出血热月发病率。运用2004年1月至2011年12月的数据预测2012年12个月的发病率数据。

    3996febe5e5e429296a496bfdfecdfc6.png

     

    二、AdaBoost回归

    (1)代码解读

    sklearn.ensemble.AdaBoostRegressor(estimator=None, *, n_estimators=50, learning_rate=1.0, loss='linear', random_state=None, base_estimator='deprecated')

    咋一看,跟AdaBoostClassifier(用于分类,上传送门)参数也差不多,因此,我们列举出它们相同和不同的地方,便于对比记忆:

    共同的参数:

    base_estimator: 基估计器用于训练弱学习器。如果为 None,分类器默认使用决策树分类器,而回归器默认使用决策树回归器。

    n_estimators: 最大的弱学习器数量。

    learning_rate: 按指定的学习率缩小每个弱学习器的贡献。

    random_state: 随机数生成器的种子或随机数生成器。

    algorithm: 用于 AdaBoost 算法的执行版本。在分类器中是 {"SAMME", "SAMME.R"},在回归器中只有 "SAMME"。

    差异:

    AdaBoostClassifier 特有参数:

    algorithm: 可选的执行算法可以是 "SAMME" 或 "SAMME.R"。默认为 "SAMME.R"。其中 "SAMME.R" 是 "SAMME" 的实值版本,它通常表现得更好,因为它依赖于类别概率,而不是类别预测。

    AdaBoostRegressor 特有参数:

    loss: 在增加新的弱学习器时用于更新权重的损失函数。可选的值包括 'linear', 'square', 和 'exponential'。

    综上可见,虽然这两个类的大部分参数都很相似,但它们的主要区别在于分类器具有两种执行算法("SAMME" 和 "SAMME.R"),而回归器则添加了一个 loss 参数来定义更新权重时使用的损失函数。

     

    (2)单步滚动预测

    1. import pandas as pd
    2. import numpy as np
    3. from sklearn.metrics import mean_absolute_error, mean_squared_error
    4. from sklearn.ensemble import AdaBoostRegressor
    5. from sklearn.model_selection import GridSearchCV
    6. data = pd.read_csv('data.csv')
    7. # 将时间列转换为日期格式
    8. data['time'] = pd.to_datetime(data['time'], format='%b-%y')
    9. # 拆分输入和输出
    10. lag_period = 6
    11. # 创建滞后期特征
    12. for i in range(lag_period, 0, -1):
    13. data[f'lag_{i}'] = data['incidence'].shift(lag_period - i + 1)
    14. # 删除包含NaN的行
    15. data = data.dropna().reset_index(drop=True)
    16. # 划分训练集和验证集
    17. train_data = data[(data['time'] >= '2004-01-01') & (data['time'] <= '2011-12-31')]
    18. validation_data = data[(data['time'] >= '2012-01-01') & (data['time'] <= '2012-12-31')]
    19. # 定义特征和目标变量
    20. X_train = train_data[['lag_1', 'lag_2', 'lag_3', 'lag_4', 'lag_5', 'lag_6']]
    21. y_train = train_data['incidence']
    22. X_validation = validation_data[['lag_1', 'lag_2', 'lag_3', 'lag_4', 'lag_5', 'lag_6']]
    23. y_validation = validation_data['incidence']
    24. # 初始化AdaBoostRegressor模型
    25. adaboost_model = AdaBoostRegressor()
    26. # 定义参数网格
    27. param_grid = {
    28. 'n_estimators': [50, 100, 150],
    29. 'learning_rate': [0.01, 0.05, 0.1, 0.5, 1],
    30. 'loss': ['linear', 'square', 'exponential']
    31. }
    32. # 初始化网格搜索
    33. grid_search = GridSearchCV(adaboost_model, param_grid, cv=5, scoring='neg_mean_squared_error')
    34. # 进行网格搜索
    35. grid_search.fit(X_train, y_train)
    36. # 获取最佳参数
    37. best_params = grid_search.best_params_
    38. # 使用最佳参数初始化AdaBoostRegressor模型
    39. best_adaboost_model = AdaBoostRegressor(**best_params)
    40. # 在训练集上训练模型
    41. best_adaboost_model.fit(X_train, y_train)
    42. # 对于验证集,我们需要迭代地预测每一个数据点
    43. y_validation_pred = []
    44. for i in range(len(X_validation)):
    45. if i == 0:
    46. pred = best_adaboost_model.predict([X_validation.iloc[0]])
    47. else:
    48. new_features = list(X_validation.iloc[i, 1:]) + [pred[0]]
    49. pred = best_adaboost_model.predict([new_features])
    50. y_validation_pred.append(pred[0])
    51. y_validation_pred = np.array(y_validation_pred)
    52. # 计算验证集上的MAE, MAPE, MSE和RMSE
    53. mae_validation = mean_absolute_error(y_validation, y_validation_pred)
    54. mape_validation = np.mean(np.abs((y_validation - y_validation_pred) / y_validation))
    55. mse_validation = mean_squared_error(y_validation, y_validation_pred)
    56. rmse_validation = np.sqrt(mse_validation)
    57. # 计算训练集上的MAE, MAPE, MSE和RMSE
    58. y_train_pred = best_adaboost_model.predict(X_train)
    59. mae_train = mean_absolute_error(y_train, y_train_pred)
    60. mape_train = np.mean(np.abs((y_train - y_train_pred) / y_train))
    61. mse_train = mean_squared_error(y_train, y_train_pred)
    62. rmse_train = np.sqrt(mse_train)
    63. print("Train Metrics:", mae_train, mape_train, mse_train, rmse_train)
    64. print("Validation Metrics:", mae_validation, mape_validation, mse_validation, rmse_validation)

    看结果:

    bbf127dfa1a04a8f9b85a1c944417f52.png

     

    (3)多步滚动预测-vol. 1

    AdaBoostRegressor预期的目标变量y应该是一维数组,所以你们懂的。

     

    (4)多步滚动预测-vol. 2

    同上。

     

    (5)多步滚动预测-vol. 3

    1. import pandas as pd
    2. import numpy as np
    3. from sklearn.ensemble import AdaBoostRegressor
    4. from sklearn.model_selection import GridSearchCV
    5. from sklearn.metrics import mean_absolute_error, mean_squared_error
    6. # 数据读取和预处理
    7. data = pd.read_csv('data.csv')
    8. data_y = pd.read_csv('data.csv')
    9. data['time'] = pd.to_datetime(data['time'], format='%b-%y')
    10. data_y['time'] = pd.to_datetime(data_y['time'], format='%b-%y')
    11. n = 6
    12. for i in range(n, 0, -1):
    13. data[f'lag_{i}'] = data['incidence'].shift(n - i + 1)
    14. data = data.dropna().reset_index(drop=True)
    15. train_data = data[(data['time'] >= '2004-01-01') & (data['time'] <= '2011-12-31')]
    16. X_train = train_data[[f'lag_{i}' for i in range(1, n+1)]]
    17. m = 3
    18. X_train_list = []
    19. y_train_list = []
    20. for i in range(m):
    21. X_temp = X_train
    22. y_temp = data_y['incidence'].iloc[n + i:len(data_y) - m + 1 + i]
    23. X_train_list.append(X_temp)
    24. y_train_list.append(y_temp)
    25. for i in range(m):
    26. X_train_list[i] = X_train_list[i].iloc[:-(m-1)]
    27. y_train_list[i] = y_train_list[i].iloc[:len(X_train_list[i])]
    28. # 模型训练
    29. param_grid = {
    30. 'n_estimators': [50, 100, 150],
    31. 'learning_rate': [0.01, 0.05, 0.1, 0.5, 1],
    32. 'loss': ['linear', 'square', 'exponential']
    33. }
    34. best_ada_models = []
    35. for i in range(m):
    36. grid_search = GridSearchCV(AdaBoostRegressor(), param_grid, cv=5, scoring='neg_mean_squared_error')
    37. grid_search.fit(X_train_list[i], y_train_list[i])
    38. best_ada_model = AdaBoostRegressor(**grid_search.best_params_)
    39. best_ada_model.fit(X_train_list[i], y_train_list[i])
    40. best_ada_models.append(best_ada_model)
    41. validation_start_time = train_data['time'].iloc[-1] + pd.DateOffset(months=1)
    42. validation_data = data[data['time'] >= validation_start_time]
    43. X_validation = validation_data[[f'lag_{i}' for i in range(1, n+1)]]
    44. y_validation_pred_list = [model.predict(X_validation) for model in best_ada_models]
    45. y_train_pred_list = [model.predict(X_train_list[i]) for i, model in enumerate(best_ada_models)]
    46. def concatenate_predictions(pred_list):
    47. concatenated = []
    48. for j in range(len(pred_list[0])):
    49. for i in range(m):
    50. concatenated.append(pred_list[i][j])
    51. return concatenated
    52. y_validation_pred = np.array(concatenate_predictions(y_validation_pred_list))[:len(validation_data['incidence'])]
    53. y_train_pred = np.array(concatenate_predictions(y_train_pred_list))[:len(train_data['incidence']) - m + 1]
    54. mae_validation = mean_absolute_error(validation_data['incidence'], y_validation_pred)
    55. mape_validation = np.mean(np.abs((validation_data['incidence'] - y_validation_pred) / validation_data['incidence']))
    56. mse_validation = mean_squared_error(validation_data['incidence'], y_validation_pred)
    57. rmse_validation = np.sqrt(mse_validation)
    58. print("验证集:", mae_validation, mape_validation, mse_validation, rmse_validation)
    59. mae_train = mean_absolute_error(train_data['incidence'][:-(m-1)], y_train_pred)
    60. mape_train = np.mean(np.abs((train_data['incidence'][:-(m-1)] - y_train_pred) / train_data['incidence'][:-(m-1)]))
    61. mse_train = mean_squared_error(train_data['incidence'][:-(m-1)], y_train_pred)
    62. rmse_train = np.sqrt(mse_train)
    63. print("训练集:", mae_train, mape_train, mse_train, rmse_train)

    结果:

    79a7b427dc2a4478822c4093fcce4860.png

     

    三、数据

    链接:https://pan.baidu.com/s/1EFaWfHoG14h15KCEhn1STg?pwd=q41n

    提取码:q41n

     

  • 相关阅读:
    Springboot毕业设计毕设作品,微信校园疫情防控小程序设计与实现
    软件测试经典面试题:如何进行支付功能的测试?
    《PostgreSQL物化视图:创建、维护与应用》
    push apk到真机出现remote couldn‘t create file: Read-only file system
    5款开源、美观、强大的WPF UI组件库
    【Rust日报】2022-11-01 async-backtrace 发布
    systemd-journald日志管理服务详解
    解析/区分MOS管的三个引脚G、S、D(NMOS管和PMOS管)
    C++继承中的构造与析构
    VMware安装Ubuntu20(图文教程,超详细)
  • 原文地址:https://blog.csdn.net/qq_30452897/article/details/133486453