基于WIN10的64位系统演示
一、写在前面
这一期,我们介绍LightGBM回归。
同样,这里使用这个数据:
《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个月的发病率数据。
二、LightGMB回归
(1)参数解读
LightGBM 可以处理分类和回归任务,大多数参数在这两种任务之间是通用的,但也有一些参数是特定于任务类型的。下面是这两种任务在参数设置方面的异同:
(a)相同之处:
核心参数:如 boosting_type、num_boost_round、learning_rate 等。
学习控制参数:这些控制决策树的结构和拟合方式。例如,max_depth, num_leaves, min_data_in_leaf, feature_fraction, bagging_fraction, lambda_l1, lambda_l2 等。
IO 参数:控制输入输出的参数,如 max_bin 和 min_data_in_bin。
其他参数:如 verbosity, boost_from_average 等。
(b)不同之处:
目标函数 (objective 或 application 参数):
分类:可以选择 binary(二分类)或者 multiclass(多分类)。对于多分类还有一个参数 num_class 来指定类别数。
回归:通常选择 regression。还有其他的回归目标如 regression_l1,huber,fair,等。
(c)度量指标 (metric 参数):
分类:例如 binary_logloss, binary_error, multi_logloss, multi_error 等。
回归:例如 l2 (MSE), l1 (MAE), mape 等。
(d)类别权重 (class_weight 参数):
分类:当数据集的类别不均衡时,可以使用这个参数为各个类别设置不同的权重。
回归:这个参数通常不适用。
(e)其他特定参数:
分类:例如,scale_pos_weight 可以用于处理非常不平衡的二分类问题。
回归:通常不需要这些特定的参数。
综上所述,尽管分类和回归任务在 LightGBM 的参数上有很多相似之处,但它们的目标函数和评价标准是不同的,需要根据具体任务来进行调整。
(2)单步滚动预测
import pandas as pd
import numpy as np
from sklearn.metrics import mean_absolute_error, mean_squared_error
from lightgbm import LGBMRegressor
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']
# 初始化 LGBMRegressor 模型
lgbm_model = LGBMRegressor()
# 定义参数网格
param_grid = {
'n_estimators': [50, 100, 150],
'learning_rate': [0.01, 0.05, 0.1, 0.5, 1],
'num_leaves': [31, 50, 75],
'boosting_type': ['gbdt', 'dart', 'goss']
}
# 初始化网格搜索
grid_search = GridSearchCV(lgbm_model, param_grid, cv=5, scoring='neg_mean_squared_error')
# 进行网格搜索
grid_search.fit(X_train, y_train)
# 获取最佳参数
best_params = grid_search.best_params_
# 使用最佳参数初始化 LGBMRegressor 模型
best_lgbm_model = LGBMRegressor(**best_params)
# 在训练集上训练模型
best_lgbm_model.fit(X_train, y_train)
# 对于验证集,我们需要迭代地预测每一个数据点
y_validation_pred = []
for i in range(len(X_validation)):
if i == 0:
pred = best_lgbm_model.predict([X_validation.iloc[0]])
else:
new_features = list(X_validation.iloc[i, 1:]) + [pred[0]]
pred = best_lgbm_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_lgbm_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)
看结果:
(3)多步滚动预测-vol. 1
对于LGBMRegressor,目标变量y_train不能是多列的DataFrame,所以你们懂的。
(4)多步滚动预测-vol. 2
同上。
(5)多步滚动预测-vol. 3
import pandas as pd
import numpy as np
from lightgbm import LGBMRegressor # 导入LGBMRegressor
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],
'boosting_type': ['gbdt', 'dart', 'goss'],
'num_leaves': [31, 63, 127]
}
best_lgbm_models = []
for i in range(m):
grid_search = GridSearchCV(LGBMRegressor(), param_grid, cv=5, scoring='neg_mean_squared_error') # 使用LGBMRegressor
grid_search.fit(X_train_list[i], y_train_list[i])
best_lgbm_model = LGBMRegressor(**grid_search.best_params_)
best_lgbm_model.fit(X_train_list[i], y_train_list[i])
best_lgbm_models.append(best_lgbm_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_lgbm_models]
y_train_pred_list = [model.predict(X_train_list[i]) for i, model in enumerate(best_lgbm_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)
结果:
三、数据
链接:https://pan.baidu.com/s/1EFaWfHoG14h15KCEhn1STg?pwd=q41n
提取码:q41n