Re: [問題] 快速產生混淆矩陣

看板R_Language作者 (綠豆冰)時間3年前 (2020/10/02 22:24), 3年前編輯推噓1(101)
留言2則, 1人參與, 3年前最新討論串2/2 (看更多)
※ 引述《totolink (吐吐林克)》之銘言: : 大家好, : 今天已經有整理好的資料表 : 一欄是真實類別,一欄是預測類別 : 是否有辦法直接透過整理好的資料表快速建立混淆矩陣呢? 因為沒有給例子,我在網路上找到下面這個資料參考 install.packages("caret", dependencies = c("Depends", "Suggests")) library(caret) Conf_mat<-structure(list(Predicted = c(100, 200, 200, 100, 100, 200, 200, 200, 100, 200, 500, 100, 100, 100, 100, 100, 100, 100, 500, 200 ), Reference = c(600, 200, 200, 200, 200, 200, 200, 200, 500, 500, 500, 200, 200, 200, 200, 200, 200, 200, 200, 200)), .Names = c("Predicted", "Reference"), row.names = c(NA, 20L), class = "data.frame") 第一個解法是沒有用迴圈 conf_mat_tab<-as.vector(mode="list") conf_mat_tab[[1]] <- caret::confusionMatrix(table(lapply(Conf_mat, factor, levels = seq(100, 200, 100)))) conf_mat_tab[[2]] <- caret::confusionMatrix(table(lapply(Conf_mat, factor, levels = seq(200, 300, 100)))) conf_mat_tab[[3]] <- caret::confusionMatrix(table(lapply(Conf_mat, factor, levels = seq(300, 400, 100)))) conf_mat_tab[[4]] <- caret::confusionMatrix(table(lapply(Conf_mat, factor, levels = seq(400, 500, 100)))) conf_mat_tab[[5]] <- caret::confusionMatrix(table(lapply(Conf_mat, factor, levels = seq(500, 600, 100)))) 下面是迴圈的做法 conf_mat_tab<-as.vector(mode="list") L<-seq(100,600,100) for (i in 1:c(length(L)-1)) { conf_mat_tab[[i]] <- caret::confusionMatrix(table(lapply(Conf_mat, factor, levels = seq(L[i],L[i+1],100)))) } 下面是印conf_mat_tab[[1]]的結果 > conf_mat_tab[[1]] Confusion Matrix and Statistics Reference Predicted 100 200 100 0 9 200 0 6 Accuracy : 0.4 95% CI : (0.1634, 0.6771) No Information Rate : 1 P-Value [Acc > NIR] : 1.000000 Kappa : 0 Mcnemar's Test P-Value : 0.007661 Sensitivity : NA Specificity : 0.4 Pos Pred Value : NA Neg Pred Value : NA Prevalence : 0.0 Detection Rate : 0.0 Detection Prevalence : 0.6 Balanced Accuracy : NA 'Positive' Class : 100 不知道是不是原PO要的結果 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.136.143.26 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1601648658.A.7D8.html ※ 編輯: rebe212296 (223.136.143.26 臺灣), 10/02/2020 22:31:29

10/02 22:46, 3年前 , 1F
感謝,後來發現把data frame的每欄都進行factor處理再ta
10/02 22:46, 1F

10/02 22:46, 3年前 , 2F
ble就可以了 XD
10/02 22:46, 2F
※ 編輯: rebe212296 (223.136.143.26 臺灣), 10/03/2020 00:40:29
文章代碼(AID): #1VTpWIVO (R_Language)
文章代碼(AID): #1VTpWIVO (R_Language)