英文:
Calculating ROC and AUC in loop
问题
我为一个二分类问题创建了多个模型,我想要计算并绘制每个模型的AUC和ROC(最好在一个图中)。我应该如何做?
这是我目前使用的代码:
library(pROC)
model <- load_model_hdf5('M1.h5')
pred <- model %>% predict(test[,1:10])
roc_object <- roc(test[,11], pred) # 创建ROC曲线
auc(roc_object) # 计算曲线下面积
test[,1:10]
是测试数据,test[,11]
是真实标签。
英文:
I created several models for a binary classification problem and I'd like to calculate and plot the AUC and the ROC for each model (ideally in one plot). How could I do this?
This is the code I currently use:
library(pROC)
model <- load_model_hdf5('M1.h5')
pred <- model %>% predict(test[,1:10])
roc_object <- roc(test[,11], pred) # create ROC curve
auc(roc_object) # calculate area under curve
test[,1:10]
is the test data and test[,11]
is the ground truth.
答案1
得分: 1
使用mtcars
的示例数据,我创建了一个涉及多个ROC曲线的示例,并将它们绘制在一个单一的图中。
在第一次调用plot()
后,后续使用lines()
的调用将附加到现有的图形上,而不是创建一个新的图形。
data(mtcars)
roc1 <- roc(mtcars[, "am"], mtcars[, "mpg"])
roc2 <- roc(mtcars[, "am"], mtcars[, "disp"])
roc3 <- roc(mtcars[, "am"], mtcars[, "hp"])
plot(1 - roc1[["specificities"]], roc1[["sensitivities"]], type = "l", col = "green")
lines(1 - roc2[["specificities"]], roc2[["sensitivities"]], col = "blue")
lines(1 - roc3[["specificities"]], roc3[["sensitivities"]], col = "red")
英文:
Using sample data of mtcars
, I have created an example involving several ROC in one single plot.
After your first call to plot()
, the subsequent calls using lines()
will append to the existing plot instead of creating a new plot.
data(mtcars)
roc1 <- roc(mtcars[, "am"], mtcars[, "mpg"])
roc2 <- roc(mtcars[, "am"], mtcars[, "disp"])
roc3 <- roc(mtcars[, "am"], mtcars[, "hp"])
plot(1 - roc1[["specificities"]], roc1[["sensitivities"]], type = "l", col = "green")
lines(1 - roc2[["specificities"]], roc2[["sensitivities"]], col = "blue")
lines(1 - roc3[["specificities"]], roc3[["sensitivities"]], col = "red")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论