计算 ROC 和 AUC 在循环中。

huangapple go评论52阅读模式
英文:

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 &lt;- load_model_hdf5(&#39;M1.h5&#39;)
pred &lt;- model %&gt;% predict(test[,1:10]) 
roc_object &lt;- 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 &lt;- roc(mtcars[, &quot;am&quot;], mtcars[, &quot;mpg&quot;])
roc2 &lt;- roc(mtcars[, &quot;am&quot;], mtcars[, &quot;disp&quot;])
roc3 &lt;- roc(mtcars[, &quot;am&quot;], mtcars[, &quot;hp&quot;])

plot(1 - roc1[[&quot;specificities&quot;]], roc1[[&quot;sensitivities&quot;]], type = &quot;l&quot;, col = &quot;green&quot;)
lines(1 - roc2[[&quot;specificities&quot;]], roc2[[&quot;sensitivities&quot;]], col = &quot;blue&quot;)
lines(1 - roc3[[&quot;specificities&quot;]], roc3[[&quot;sensitivities&quot;]], col = &quot;red&quot;)

huangapple
  • 本文由 发表于 2023年6月15日 01:15:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476055.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定