繁体   English   中英

如何在 R 中制作 plot ROC 曲线进行多类分类?

[英]How do I make and plot ROC curves in R for multiclass classification?

我正在做我的学位工作,我正在研究我用来进行分类的高斯混合模型(GMM),因为我正在使用 GMM 进行分类,所以我很想知道 model 的准确度作为分类器。 我正在处理一个有 5 个标签的数据集,它指的是 5 类大米,我将数据集分成测试数据和训练数据,并使用Mclustmlcust库中的r function 运行 GMM,我想知道如何我可以为这种多类情况制作 ROC 曲线,并计算 AUC,我看到使用类型 1 与所有类型的图表是可能的,但他们没有详细说明如何在其他地方找到的示例运行不好,因此很容易看出它是如何工作的

我附上了使用 iris 的 GMM 示例代码,对于这种情况,我想看看 ROC 曲线是什么样的

library(mclust)

Datos<-iris

set.seed(2022)

indices<-sample(1:nrow(Datos),size = floor(0.40*nrow(Datos)),replace = FALSE)
entreno<-Datos[indices,]
prueba<-Datos[-indices,]
table(Datos[indices,]$Species)
X<-Datos[indices,-5]
y <-Datos[indices,5]



GMM_iris<-Mclust(X,G=3)
summary(GMM_iris,parameters = TRUE)
table(y,GMM_iris$classification)

prediccion<-predict(GMM_iris,newdata = prueba[,-5])
table(Datos[-indices,]$Species,prediccion$classification)```

您可以使用 pROC plot 多类分类pROC的 roc 曲线。

library(mclust)
#> Package 'mclust' version 5.4.9
#> Type 'citation("mclust")' for citing this R package in publications.

Datos<-iris

set.seed(2022)

indices<-sample(1:nrow(Datos),size = floor(0.40*nrow(Datos)),replace = FALSE)
entreno<-Datos[indices,]
prueba<-Datos[-indices,]

X<-Datos[indices,-5]
y <-Datos[indices,5]


GMM_iris<-Mclust(X,G=3)



table(y,GMM_iris$classification)
#>             
#> y             1  2  3
#>   setosa      0 21  0
#>   versicolor  0  0 22
#>   virginica  17  0  0

prediction<-predict(GMM_iris,newdata = prueba[,-5])


library(pROC)
result <- pROC::multiclass.roc(prediction$classification, 
                               as.numeric(prueba$Species))

multiclass.roc 的multiclass.roc prueba$Species已更改为numeric

plot.roc(result$rocs[[1]], 
         print.auc=T,
         legacy.axes = T)
plot.roc(result$rocs[[2]],
         add=T, col = 'red',
         print.auc = T,
         legacy.axes = T,
         print.auc.adj = c(0,3))
plot.roc(result$rocs[[3]],add=T, col = 'blue',
         print.auc=T,
         legacy.axes = T,
         print.auc.adj = c(0,5))

legend('bottomright',
       legend = c('setosa-versicolor',
                  'setosa-virginica',
                  'versicolor-virginica'),
       col=c('black','red','blue'),lwd=2)

result中有 3 个 rocs ,是鸢尾种组合的个数。
(setosa & versicolor、setosa & virginica、versicolor & virginica)。

reprex package (v2.0.1) 创建于 2022-05-10

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM