在 Python 中使用 XGboost_Regressor 会导致非常好的训练性能但预测效果较差

Using XGboost_Regressor in Python results in very good training performance but poor in prediction

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

下面是我使用的代码:

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

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

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

这里的问题是过拟合。您需要调整一些参数(Source).

  • set n_estimators to 80-200 if the size of data is high (of the order of lakh), 800-1200 is if it is medium-low
  • learning_rate: between 0.1 and 0.01
  • subsample: between 0.8 and 1
  • colsample_bytree: number of columns used by each tree. Values from 0.3 to 0.8 if you have many feature vectors or columns , or 0.8 to 1 if you only few feature vectors or columns.
  • gamma: Either 0, 1 or 5

因为max_depth你已经取的很低了,所以你可以尝试调整上面的参数。此外,如果您的数据集非常小,那么训练和测试的差异是意料之中的。您需要检查在训练和测试数据中是否存在良好的数据分割。例如,在测试数据中,输出列的是和否百分比是否几乎相等。

您需要尝试各种选择。 xgboost 和随机森林肯定会为较少的数据提供过拟合模型。你可以试试:-

1.Naive 贝叶斯。它适用于较少的数据集,但它认为所有特征向量的权重相同。

逻辑回归 - 尝试调整正则化参数并查看您的召回分数最高的地方。这里面的其他东西是 calsss weight = balanced.

具有交叉验证的逻辑回归 - 这也适用于小数据。我之前也说过的最后一件事,检查你的数据,看看它是否偏向于一种结果。就像如果 70 例中有 50 例结果是肯定的,它是有很大偏差的,你可能无法获得高精度。