我使用 XGBoost 构建预测模型:
setDT(train)
setDT(test)
labels <- train$Goal
ts_label <- test$Goal
new_tr <- model.matrix(~.+0,data = train[,-c("Goal"),with=F])
new_ts <- model.matrix(~.+0,data = test[,-c("Goal"),with=F])
labels <- as.numeric(labels)-1
ts_label <- as.numeric(ts_label)-1
dtrain <- xgb.DMatrix(data = new_tr,label = labels)
dtest <- xgb.DMatrix(data = new_ts,label=ts_label)
params <- list(booster = "gbtree", objective = "binary:logistic", eta=0.3, gamma=0, max_depth=6, min_child_weight=1, subsample=1, colsample_bytree=1)
xgb1 <- xgb.train(params = params, data = dtrain, nrounds = 291, watchlist = list(val=dtest,train=dtrain), print_every_n = 10,
early_stop_round = 10, maximize = F , eval_metric = "error")
xgbpred <- predict(xgb1,dtest)
xgbpred <- ifelse(xgbpred > 0.5,1,0)
confusionMatrix(xgbpred, ts_label)
Confusion Matrix and Statistics
Reference
Prediction 0 1
0 1904 70
1 191 2015
Accuracy : 0.9376
95% CI : (0.9298, 0.9447)
No Information Rate : 0.5012
P-Value [Acc > NIR] : < 0.00000000000000022
Kappa : 0.8751
Mcnemar's Test P-Value : 0.0000000000001104
Sensitivity : 0.9088
Specificity : 0.9664
Pos Pred Value : 0.9645
Neg Pred Value : 0.9134
Prevalence : 0.5012
Detection Rate : 0.4555
Detection Prevalence : 0.4722
Balanced Accuracy : 0.9376
'Positive' Class : 0
Run Code Online (Sandbox Code Playgroud)
这个精度适合我,但我想检查 auc 的指标。我写的:
xgb1 <- xgb.train(params = params, data = dtrain, nrounds = 291, watchlist = list(val=dtest,train=dtrain), print_every_n = 10,
early_stop_round = 10, maximize = F , eval_metric = "auc")
Run Code Online (Sandbox Code Playgroud)
但之后我不知道如何对 AUC 指标进行预测。我需要你的帮助,因为这是我第一次使用 XGBoost。谢谢。
UPD:据我了解,在 auc 指标之后,我需要一个我会逃课的系数。现在我在 0,5 处截断
您可以通过以下方式查看训练数据集的训练模型的 AUC 值
> max(xgb1$evaluation_log$train_auc)
Run Code Online (Sandbox Code Playgroud)
您还可以使用 pROC 包计算对测试集的预测,如下所示
> library(pROC)
> roc_test <- roc( test_label_vec, predictions_for_test, algorithm = 2)
Run Code Online (Sandbox Code Playgroud)
对于用你的参数编写的代码,它是
> roc_test <- roc(ts_label, xgbpred, algorithm = 2)
> plot(roc_test )
> auc(roc_test )
Run Code Online (Sandbox Code Playgroud)
如果你想计算AUC并为你的训练集绘制ROC曲线,你可以使用以下
> roc_training <- roc(train_output_vec, train_predictions, algorithm = 2)
> plot(roc_training )
> auc(roc_training)
Run Code Online (Sandbox Code Playgroud)
ROC曲线和AUC不需要考虑分界点。绘制 ROC 并计算 AUC,对预测分数进行排序并查看在预测集中找到的目标事件的百分比。因此,它正在检查如果移动截止点,您可以找到多少%的目标事件。截止点的决定与成本有关,或者与算法的应用有关。您可以搜索 cutoff 以获取更多信息。