Skip to main content
 首页 » 编程设计

python之在 Python 中使用 XGboost_Regressor 可获得非常好的训练性能但预测效果较差

2025年05月04日23leader

我一直在尝试在 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

请您参考如下方法:

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