簡體   English   中英

如何創建 R 輸出喜歡混淆矩陣表

[英]How to create R output likes confusion matrix table

我有兩個目錄。 第一個目錄的名稱是“model”,第二個目錄的名稱是“test”,兩個目錄中的文件列表相同但內容不同。 兩個目錄中的文件總數也相同,即 37 個文件。

我展示了其中一個文件的內容示例。

模型目錄中的第一個文件

名稱文件:Model_A5B45

                               data
1  papaya | durian | orange | grapes
2                             orange
3                             grapes
4                    banana | durian
5                             tomato
6                     apple | tomato
7                              apple
8                        mangostine 
9                         strawberry
10                strawberry | mango

輸出輸出:

structure(list(data = structure(c(7L, 6L, 4L, 3L, 10L, 2L, 1L, 
5L, 8L, 9L), .Label = c("apple", "apple | tomato", "banana | durian", 
"grapes", "mangostine ", "orange", "papaya | durian | orange | grapes", 
"strawberry", "strawberry | mango", "tomato"), class = "factor")), .Names = "data", class = "data.frame", row.names = c(NA, 
-10L))

測試目錄中的第二個文件

名稱文件:Test_A5B45

                               data
1                             apple
2            orange | apple | mango
3                             apple
4                            banana
5                            grapes
6                            papaya
7                            durian
8 tomato | orange | papaya | durian

輸出輸出:

structure(list(data = structure(c(1L, 5L, 1L, 2L, 4L, 6L, 3L, 
7L), .Label = c("apple", "banana", "durian", "grapes", "orange | apple | mango", 
"papaya", "tomato | orange | papaya | durian"), class = "factor")), .Names = "data", class = "data.frame", row.names = c(NA, 
-8L))

我想計算從目錄 test 中的文件到目錄模型中的文件的相交和除外數據的百分比。

這是我僅用於兩個文件(Model_A5B45 和 Test_A5B45)的代碼示例。

library(dplyr)

data_test <- read.csv("Test_A5B45")
data_model <- read.csv("Model_A5B45")
intersect <- semi_join(data_test,data_model)
except <- anti_join(data_test,data_model)
except_percentage <- (nrow(except)/nrow(data_test))*100
intersect_percentage <- (nrow(intersect)/nrow(data_test))*100
sprintf("%s/%s",intersect_percentage,except_percentage) 

輸出: "37.5/62.5"

我的問題是,我想將我的代碼實現到所有文件(在兩個目錄中循環),因此輸出看起來像混淆矩陣。

我的預期輸出示例:

##             y
##              Model_A5B45       Model_A6B46    Model_A7B47
##   Test_A5B45     37.5/62.5          value         value
##   Test_A6B46      value             value         value
##   Test_A7B47      value             value         value

我的答案:

我已經創建了可以處理這些東西的代碼,但我仍然不知道如何使輸出看起來像混淆矩陣。

這是我的代碼:(我不知道這是否有效,我使用for循環)

f_performance_testing <- function(data_model_path, data_test_path){
  library(dplyr)
  data_model <- read.csv(data_model_path, header=TRUE)
  data_test <- read.csv(data_test_path, header=TRUE)
  intersect <- semi_join(data_test,data_model)
  except <- anti_join(data_test,data_model)
  except_percentage <- (nrow(except)/nrow(data_test))*100
  intersect_percentage <- (nrow(intersect)/nrow(data_test))*100

  return(list("intersect"=intersect_percentage,"except"=except_percentage))
}


for (model in model_list){
  for (test in test_list){
    result <- f_performance_testing(model,test)
    intersect_percentage <- round(result$intersect,3)
    except_percentage <- round(result$except,3)
    final_output <- sprintf("intersect : %s | except : %s",intersect_percentage,except_percentage) 
    cat(print(paste(substring(model,57),substring(test,56), final_output,sep=",")),file="outfile.txt",append=TRUE,"\n")
    print("Writing to file.......")
  }
}

輸出是:

Model_A5B45,Test_A5B45, 37.5/62.5 
Model_A5B45,Test_A6B46, value
Model_A5B45,Test_A7B47, value
Model_A6B46,...... 
Model_A7B47,.....
...............
......
....

如何將此輸出轉換為看起來像混淆矩陣表?

這不會直接回答您的問題,但希望為您提供足夠的信息來得出您自己的解決方案。

我建議創建一個如下所示的函數:

myFun <- function(model, test, datasource) {
  model <- datasource[[model]]
  test <- datasource[[test]]
  paste(rev(mapply(function(x, y) (x/y)*100, 
                   lapply(split(test, test %in% model), length), 
                   length(test))), 
        collapse = "/")
}

此函數將與兩列data.frame ,其中列表示“測試”和“模型”值的所有組合(當字符vector就足夠時,為什么要使用data.frame結構?)

這是此類data.frame示例(其他示例數據位於答案末尾)。

models <- c("model_1", "model_2", "model_3")
tests <- c("test_1", "test_2", "test_3")
A <- expand.grid(models, tests, stringsAsFactors = FALSE)

接下來,創建模型和測試的命名list 如果您已經使用lapply閱讀過數據,那么無論如何您可能都有可以使用的名稱。

dataList <- mget(c(models, tests))

現在,計算相關值。 在這里,我們可以使用apply循環遍歷每一行並執行相關計算。

A$value <- apply(A, 1, function(x) myFun(x[1], x[2], dataList))

最后,您將數據從“長”形式reshape為“寬”形式。

reshape(A, direction = "wide", idvar = "Var1", timevar = "Var2")
#      Var1 value.test_1 value.test_2 value.test_3
# 1 model_1        75/25          100        75/25
# 2 model_2        50/50        50/50    62.5/37.5
# 3 model_3    62.5/37.5        50/50    87.5/12.5

這是一些示例數據。 請注意,它們是基本字符向量,而不是data.frame

set.seed(1)
sets <- c("A", "A|B", "B", "C", "A|B|C", "A|C", "D", "A|D", "B|C", "B|D")

test_1 <- sample(sets, 8, TRUE)
model_1 <- sample(sets, 10, TRUE)
test_2 <- sample(sets, 8, TRUE)
model_2 <- sample(sets, 10, TRUE)
test_3 <- sample(sets, 8, TRUE)
model_3 <- sample(sets, 10, TRUE)

在現實世界的應用程序中,您可能會執行以下操作:

testList <- lapply(list.files(path = "path/to/test/files"),
                   function(x) read.csv(x, stringsAsFactors = FALSE)$data)
modelList <- lapply(list.files(path = "path/to/model/files"),
                   function(x) read.csv(x, stringsAsFactors = FALSE)$data)
dataList <- c(testList, modelList)

但是,這純粹是我的推測,基於您在問題中作為工作代碼共享的內容(例如,沒有文件擴展名的 csv 文件)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM