第84步 时间序列建模实战:Xgboost回归建模

基于WIN10的64位系统演示

一、写在前面

这一期,我们介绍老朋友Xgboost回归。

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

《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个月的发病率数据。

第84步 时间序列建模实战:Xgboost回归建模_第1张图片

二、Xgboost回归

(1)参数解读

无论是回归还是分类,XgBoost的大部分参数都是通用的,但任务的不同性质意味着一些参数可能只在一个任务中有意义。

以下是一些关键参数的简要概述:

(a)目标函数:

回归:目标通常是预测一个连续的输出值,因此默认的目标函数是均方误差。

objective: reg:squarederror

分类:目标是预测类别。对于二分类问题,使用逻辑回归;对于多分类问题,使用多项式逻辑回归。

二分类:objective: binary:logistic

多分类:objective: multi:softprob or multi:softmax(需设置 num_class)(b)评估指标:

回归:常用的评估指标如下

rmse: 均方根误差

mae: 平均绝对误差

分类:常用的评估指标如下

error: 分类误差

logloss: 对数损失(用于二分类)

mlogloss: 多类别的对数损失(用于多分类)

auc: ROC曲线下的面积

(c)常用参数:

尽管大部分参数在回归和分类中都是相似的,但根据应用的不同,可能会对某些参数进行不同的调整。例如:

max_depth: 决策树的最大深度。

learning_rate: 学习率或步长。

subsample: 训练每棵树时使用的样本的比例。

colsample_bytree: 构建每棵树时使用的特征的比例。

n_estimators: 提升迭代的次数或树的数量。

(d)异同点:

相同点: 大部分参数(如learning_rate, depth, l2_leaf_reg等)在回归和分类任务中都是相同的,并且它们的含义和效果也是一致的。

不同点: 最主要的区别是在目标函数和评估指标上。如前所述,回归和分类任务分别使用不同的目标函数和评估指标。

(2)单步滚动预测

import pandas as pd
import numpy as np
from sklearn.metrics import mean_absolute_error, mean_squared_error
from xgboost import XGBRegressor
from sklearn.model_selection import GridSearchCV

# 读取数据
data = pd.read_csv('data.csv')

# 将时间列转换为日期格式
data['time'] = pd.to_datetime(data['time'], format='%b-%y')

# 创建滞后期特征
lag_period = 6
for i in range(lag_period, 0, -1):
    data[f'lag_{i}'] = data['incidence'].shift(lag_period - i + 1)

# 删除包含 NaN 的行
data = data.dropna().reset_index(drop=True)

# 划分训练集和验证集
train_data = data[(data['time'] >= '2004-01-01') & (data['time'] <= '2011-12-31')]
validation_data = data[(data['time'] >= '2012-01-01') & (data['time'] <= '2012-12-31')]

# 定义特征和目标变量
X_train = train_data[['lag_1', 'lag_2', 'lag_3', 'lag_4', 'lag_5', 'lag_6']]
y_train = train_data['incidence']
X_validation = validation_data[['lag_1', 'lag_2', 'lag_3', 'lag_4', 'lag_5', 'lag_6']]
y_validation = validation_data['incidence']

# 初始化 XGBRegressor 模型
xgboost_model = XGBRegressor()

# 定义参数网格
param_grid = {
    'n_estimators': [50, 100, 150],
    'learning_rate': [0.01, 0.05, 0.1, 0.5, 1],
    'max_depth': [4, 6, 8],
    'objective': ['reg:squarederror']
}

# 初始化网格搜索
grid_search = GridSearchCV(xgboost_model, param_grid, cv=5, scoring='neg_mean_squared_error')

# 进行网格搜索
grid_search.fit(X_train, y_train)

# 获取最佳参数
best_params = grid_search.best_params_

# 使用最佳参数初始化 XGBRegressor 模型
best_xgboost_model = XGBRegressor(**best_params)

# 在训练集上训练模型
best_xgboost_model.fit(X_train, y_train)

# 对于验证集,我们需要迭代地预测每一个数据点
y_validation_pred = []

for i in range(len(X_validation)):
    if i == 0:
        pred = best_xgboost_model.predict(np.array([X_validation.iloc[0]]))
    else:
        new_features = np.array([list(X_validation.iloc[i, 1:]) + [pred[0]]])
        pred = best_xgboost_model.predict(new_features)
    y_validation_pred.append(pred[0])

y_validation_pred = np.array(y_validation_pred)

# 计算验证集上的MAE, MAPE, MSE 和 RMSE
mae_validation = mean_absolute_error(y_validation, y_validation_pred)
mape_validation = np.mean(np.abs((y_validation - y_validation_pred) / y_validation))
mse_validation = mean_squared_error(y_validation, y_validation_pred)
rmse_validation = np.sqrt(mse_validation)

# 计算训练集上的MAE, MAPE, MSE 和 RMSE
y_train_pred = best_xgboost_model.predict(X_train)
mae_train = mean_absolute_error(y_train, y_train_pred)
mape_train = np.mean(np.abs((y_train - y_train_pred) / y_train))
mse_train = mean_squared_error(y_train, y_train_pred)
rmse_train = np.sqrt(mse_train)

print("Train Metrics:", mae_train, mape_train, mse_train, rmse_train)
print("Validation Metrics:", mae_validation, mape_validation, mse_validation, rmse_validation)

看结果:

第84步 时间序列建模实战:Xgboost回归建模_第2张图片

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

对于Xgboost回归,目标变量y_train不能是多列的DataFrame,所以你们懂的。

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

同上。

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

import pandas as pd
import numpy as np
from xgboost import XGBRegressor  # 导入XGBRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import mean_absolute_error, mean_squared_error

# 数据读取和预处理
data = pd.read_csv('data.csv')
data_y = pd.read_csv('data.csv')
data['time'] = pd.to_datetime(data['time'], format='%b-%y')
data_y['time'] = pd.to_datetime(data_y['time'], format='%b-%y')

n = 6

for i in range(n, 0, -1):
    data[f'lag_{i}'] = data['incidence'].shift(n - i + 1)

data = data.dropna().reset_index(drop=True)
train_data = data[(data['time'] >= '2004-01-01') & (data['time'] <= '2011-12-31')]
X_train = train_data[[f'lag_{i}' for i in range(1, n+1)]]
m = 3

X_train_list = []
y_train_list = []

for i in range(m):
    X_temp = X_train
    y_temp = data_y['incidence'].iloc[n + i:len(data_y) - m + 1 + i]
    
    X_train_list.append(X_temp)
    y_train_list.append(y_temp)

for i in range(m):
    X_train_list[i] = X_train_list[i].iloc[:-(m-1)]
    y_train_list[i] = y_train_list[i].iloc[:len(X_train_list[i])]

# 模型训练
param_grid = {
    'n_estimators': [50, 100, 150],
    'learning_rate': [0.01, 0.05, 0.1, 0.5, 1],
    'max_depth': [4, 6, 8],
    'objective': ['reg:squarederror']
}

best_xgboost_models = []

for i in range(m):
    grid_search = GridSearchCV(XGBRegressor(), param_grid, cv=5, scoring='neg_mean_squared_error')  # 使用XGBRegressor
    grid_search.fit(X_train_list[i], y_train_list[i])
    best_xgboost_model = XGBRegressor(**grid_search.best_params_)
    best_xgboost_model.fit(X_train_list[i], y_train_list[i])
    best_xgboost_models.append(best_xgboost_model)

validation_start_time = train_data['time'].iloc[-1] + pd.DateOffset(months=1)
validation_data = data[data['time'] >= validation_start_time]

X_validation = validation_data[[f'lag_{i}' for i in range(1, n+1)]]
y_validation_pred_list = [model.predict(X_validation) for model in best_xgboost_models]
y_train_pred_list = [model.predict(X_train_list[i]) for i, model in enumerate(best_xgboost_models)]

def concatenate_predictions(pred_list):
    concatenated = []
    for j in range(len(pred_list[0])):
        for i in range(m):
            concatenated.append(pred_list[i][j])
    return concatenated

y_validation_pred = np.array(concatenate_predictions(y_validation_pred_list))[:len(validation_data['incidence'])]
y_train_pred = np.array(concatenate_predictions(y_train_pred_list))[:len(train_data['incidence']) - m + 1]

mae_validation = mean_absolute_error(validation_data['incidence'], y_validation_pred)
mape_validation = np.mean(np.abs((validation_data['incidence'] - y_validation_pred) / validation_data['incidence']))
mse_validation = mean_squared_error(validation_data['incidence'], y_validation_pred)
rmse_validation = np.sqrt(mse_validation)
print("验证集:", mae_validation, mape_validation, mse_validation, rmse_validation)

mae_train = mean_absolute_error(train_data['incidence'][:-(m-1)], y_train_pred)
mape_train = np.mean(np.abs((train_data['incidence'][:-(m-1)] - y_train_pred) / train_data['incidence'][:-(m-1)]))
mse_train = mean_squared_error(train_data['incidence'][:-(m-1)], y_train_pred)
rmse_train = np.sqrt(mse_train)
print("训练集:", mae_train, mape_train, mse_train, rmse_train)

结果:

第84步 时间序列建模实战:Xgboost回归建模_第3张图片

三、数据

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

提取码:q41n

你可能感兴趣的:(《100,Steps,to,Get,ML》—JET学习笔记,回归,数据挖掘,人工智能,Xgboost回归)