gpt4 book ai didi

r - XGBoost 上的 AUC 指标

转载 作者:行者123 更新时间:2023-12-02 00:42:27 27 4
gpt4 key购买 nike

我使用 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

这个精度适合我,但我想检查一下 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")

但在那之后我不知道如何对 AUC 指标进行预测。我需要你的帮助,因为这是我第一次使用 XGBoost。谢谢。

UPD:据我所知,在 auc 指标之后我需要一个系数来削减类别。现在我在 0,5 处切断

最佳答案

您可以通过以下方式查看训练数据集的训练模型的 AUC 值

> max(xgb1$evaluation_log$train_auc)

您还可以使用 pROC 包计算您对测试集的预测,如下所示

> library(pROC) 
> roc_test <- roc( test_label_vec, predictions_for_test, algorithm = 2)

用你的参数编写的代码是

> roc_test <- roc(ts_label, xgbpred, algorithm = 2)
> plot(roc_test )
> auc(roc_test )

如果你想为你的训练集计算 AUC 并绘制 ROC 曲线,你可以使用以下方法

> roc_training <- roc(train_output_vec, train_predictions, algorithm = 2)
> plot(roc_training )
> auc(roc_training)

ROC曲线和AUC不需要考虑截止点。正在绘制 ROC 并计算 AUC,对预测分数进行排序并查看在预测集中找到的目标事件的百分比。因此,它正在检查如果您移动截止点可以找到多少百分比的目标事件。截止点的决定与成本或算法的应用有关。您可以搜索 cutoff 以获得更多相关信息。

关于r - XGBoost 上的 AUC 指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45834111/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com