首页 > 解决方案 > 为什么计算 XGBoost 时我的学习曲线没有意义?

问题描述

我使用表格数据和大约 2000 行来解决回归问题。

在数据标准化、偏度修改、异常值去除和超参数调整之后,我的 XGBoost 模型给了我不错的结果(我猜?):训练集上的 r2 得分为 0.86,测试集上为 0.85。

现在,我想知道需要多少数据才能获得良好的性能。所以我选择绘制学习曲线。

通过使用 LinearRegression() 作为 learning_curve 函数中的估计量,这是我获得的那种图。

线性回归的学习曲线

似乎是合法的。

现在我对 XGBoost 进行同样的处理。作为超参数,我使用超参数调整找到的 XGB 回归量(完整代码在文章末尾提供)。我最终得到了这个情节:

XGBoost 的学习曲线

验证分数没有意义:它应该从 1 降低到 0,85 左右...如果我使用另一种评分方法(例如,'neg_mean_squared_error'),我会得到完全相同的演变。

我不认为我的代码有问题,它更多地涉及我对整个过程的理解。有什么提示吗?

最后,这里是代码:

from xgboost.sklearn import XGBRegressor
from sklearn.model_selection import learning_curve
from sklearn.linear_model import LinearRegression

train_sizes = [1, 2, 3, 10, 25, 50, 100, 200, 500, 800, 1100, 1400]
features = ['Product Quantity', 'Departure SLP', 'Departure TMP', 'Departure W/C', 'Departure AGE', 'Travel Time', 'Outside Temp at Load Time', 'Humidity at Load Time']
target = 'Arrival SLP'

xgr =XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,
             colsample_bynode=1, colsample_bytree=0.9697304149116983,
             gamma=2.684899398839369, gpu_id=-1, importance_type='gain',
             interaction_constraints='', learning_rate=0.2595302873251097,
             max_delta_step=0, max_depth=29,
             min_child_weight=33.522965527801304,
             monotone_constraints='()', n_estimators=24, n_jobs=8,
             num_parallel_tree=1, random_state=0, reg_alpha=1.8409864324165623,
             reg_lambda=6.009121364172622, scale_pos_weight=1,
             subsample=0.8948522801808735, tree_method='exact',
             validate_parameters=1, verbosity=None)

train_sizes, train_scores, validation_scores = learning_curve(
    estimator = xgr,
    X = loads_df[features],
    y = loads_df[target], train_sizes = train_sizes, cv = 5,
    scoring = 'r2',
    shuffle=True)

train_scores_mean =train_scores.mean(axis = 1)
validation_scores_mean =validation_scores.mean(axis = 1)
print('Mean training scores\n\n', pd.Series(train_scores_mean, index = train_sizes))
print('\n', '-' * 20)
print('\nMean validation scores\n\n',pd.Series(validation_scores_mean, index = train_sizes))

plt.style.use('seaborn')
plt.plot(train_sizes, train_scores_mean, label = 'Training error')
plt.plot(train_sizes, validation_scores_mean, label = 'Validation error')
plt.ylabel('r2 score', fontsize = 14)
plt.xlabel('Training set size', fontsize = 14)
plt.title('Learning curves for XGBoost', fontsize = 18, y = 1.03)
plt.legend()
plt.ylim(0,1)

感谢您的时间 !

标签: pythonlinear-regressionxgboost

解决方案


推荐阅读