【发布时间】:2020-02-29 20:00:39
【问题描述】:
我正在尝试在不平衡的数据集 (97% / 3%) 上拟合 XGBRegressor() 并评估结果,但是在生成正确的评估指标时遇到了问题。
我选择 SMOTE 对目标变量进行过采样。
X = multiSdata.filter(['col1', 'col2','col3','col4', 'col5','col6','col7','col8',
'col9','col10','col11','col12','col13','col14','col15','col16','col17',
'col18','col19','col20','col21','col22','col23','col24'])
# retain the original feature labels
feature_labels = pd.Series(X.columns.values)
X.head(5)
[![enter image description here][1]][1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.3, random_state=27)
print( "Predictor - Training : ", X_train.shape, "Predictor - Testing : ", X_test.shape, "Target - Training : ", y_train.shape, "Target - Testing : ", y_test.shape )
输出: 预测器 - 训练:(876742, 24) 预测器 - 测试:(375747, 24) 目标 - 训练:(876742,) 目标 - 测试:(375747,)
y_train.value_counts()
输出: 0 824518 1 52224 名称:目标,数据类型:int64
sm = SMOTE(random_state = 27, ratio = 1.0)
X_train, y_train = sm.fit_sample(X_train.values, y_train.values)
np.bincount(y_train)
输出: 数组([824518, 824518])
xgb = XGBRegressor(learning_rate =0.1,
n_estimators=1000,
max_depth=5,
min_child_weight=1,
gamma=0.1,
subsample=0.8,
colsample_bytree=0.8,
objective= 'binary:logistic',
nthread=4,
scale_pos_weight=1,
seed=21,
eval_metric = ['auc','error'])
SMOTE = xreg.fit(X_train, y_train)
X_test = X_test.as_matrix()
smote_pred = SMOTE.predict(X_test)
import xgboost as xgb
params = {'learning_rate' : 0.1,
'n_estimators':1000,
'max_depth':5,
'min_child_weight':1,
'gamma':0.1,'subsampl':0.8,'colsample_bytre':0.8, 'objectiv': 'binary:logistic',
'nthread':4,'scale_pos_weight':1,'seed':21,'eval_metric':['auc','error']}
xg_train = xgb.DMatrix(data=X_train, label=y_train);
cv_results = xgb.cv(params,xg_train,num_boost_round=10,nfold=5,early_stopping_rounds=10)
cv_results
我正在尝试使用交叉验证,但无法将其与 XGBRegressor 一起使用,而是使用 xgboost 并从 X_train 和 y_train 生成 DMatrix。不确定这是否会导致 100% 的准确率,这绝对是错误的。
如果有任何关于如何进一步解决模型无法产生正确预测的问题的建议,我们将不胜感激。
【问题讨论】:
-
如果可能,请提供一个工作示例,包括导入和一些示例数据。如果您想使用 sklearn 进行交叉验证,您可能需要考虑他们的梯度提升实现
sklearn.ensemble.GradientBoostingClassifier问题可能是使用SMOTE.predict(X_test)而不是.predict_proba()? -
很难说在这种特殊情况下是什么导致了问题。这很可能是泄漏。您可能忘记从训练数据中删除标签。
-
@AdarshChavakula 我希望是这样。我检查了目标是否被错误地留下,但它没有被包括在内。
-
您是否也尝试过通过将 scale_pos_weight 设置为 sum(negative instances) / sum(positive instances) 来进行平衡?