如何计算ranger模型的AUC值?Ranger是randomForest算法在R中的快速实现。我使用以下代码来构建用于分类目的的ranger模型,并从模型中获得预测:
#Build the model using ranger() function
ranger.model <- ranger(formula, data = data_train, importance = 'impurity',
write.forest = TRUE, num.trees = 3000, mtry = sqrt(length(currentComb)),
classification = TRUE)
#get the prediction for the ranger model
pred.data <- predict(ranger.model, dat = data_test,)
table(pred.data$predictions)但是我不知道如何计算AUC值
有什么想法吗?
发布于 2017-08-14 22:45:27
计算AUC的关键是有一种方法将测试样本从“最有可能为正”到“最不可能为正”排序。修改您的培训电话,使其包含probability = TRUE。pred.data$predictions现在应该是一个类概率矩阵。记下与你的“积极”类相对应的列。此列提供了计算AUC所需的排名。
为了实际计算AUC,我们将使用Hand and Till, 2001中的公式(3)。我们可以按如下方式实现这个方程:
## An AUC estimate that doesn't require explicit construction of an ROC curve
auc <- function( scores, lbls )
{
stopifnot( length(scores) == length(lbls) )
jp <- which( lbls > 0 ); np <- length( jp )
jn <- which( lbls <= 0); nn <- length( jn )
s0 <- sum( rank(scores)[jp] )
(s0 - np*(np+1) / 2) / (np*nn)
} 其中scores是对应于正类的pred.data$predictions列,lbls是编码为二进制向量的相应测试标签(1表示正,0或-1表示负)。
https://stackoverflow.com/questions/45676745
复制相似问题