首页 > 解决方案 > 在 Python 中使用 XGboost_Regressor 可以获得非常好的训练性能,但预测效果很差

问题描述

我一直在尝试在 python 中使用 XGBregressor。这是迄今为止我使用过的最好的 ML 技术之一。但是,在某些数据集中,我的训练 R 平方非常高,但它在预测或测试方面的表现确实很差。我尝试使用 gamma、深度和二次采样来降低模型的复杂性或确保它没有过度拟合,但训练和测试之间仍然存在巨大差异。我想知道是否有人可以帮助我:

下面是我正在使用的代码:

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30,random_state=100)

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(X_train)


xgb = xgboost.XGBRegressor(colsample_bytree=0.7,
                 gamma=0,                 
                 learning_rate=0.01,
                 max_depth=1,
                 min_child_weight=1.5,
                 n_estimators=100000,                                                                    
                 reg_alpha=0.75,
                 reg_lambda=0.45,
                 subsample=0.8,
                 seed=1000) 

这是训练与测试中的表现:

训练:MAE:0.10 R^2:0.99

测试:MAE:1.47 R^2:-0.89

标签: pythonmachine-learningregressionxgboost

解决方案


XGBoost 倾向于过度拟合数据,因此减少 n_estimators 和 n_depth 并使用训练损失和验证损失之间没有太大差异的特定迭代。


推荐阅读