简体   繁体   English

混淆矩阵和插入符号包-Rpart算法

[英]Confusion Matrix and caret package - rpart algorithm

I'm developing a simple rpart model using caret package. 我正在使用插入符号包开发一个简单的rpart模型。

library(caret)
data <- fread(sprintf("%s/%s", dataDir, dataFile), header=T, stringsAsFactors = TRUE)
split <- createDataPartition(y = data$Audit, p = 0.7, list = FALSE)
trainSet <- data[split,]
test <- data[-split,]

fitControl <- trainControl( method = "repeatedcv",  number = 10,repeats = 10)


fitControl <- trainControl( method = "repeatedcv",  number = 10,repeats = 10)

fit <- train(Audit ~ Activity1 + Activity2 + Activity3 + Activity4 + Activity5 + Activity6 + Activity7 + Activity8, data = trainSet, method="rpart", trControl = fitControl)

Now I'm trying to create a confusion matrix: 现在,我正在尝试创建一个混淆矩阵:

preds<-predict(fit2, newdata = test)
cm<-confusionMatrix(preds,test$Audit)
print(cm)

When I see the output I see this: 当我看到输出时,会看到以下内容:

     [,1] [,2]
[1,]    0    0
[2,]    4    0

This is really confusing to me and it doesn't make sense. 这真的让我感到困惑,而且没有任何意义。 I was expecting something like this to be my Confusion Matrix 我期待像这样的东西成为我的困惑矩阵

preds A B U
    A 0 0 0
    B 0 4 0
    U 2 0 9

here is the snapshot of my data: 这是我的数据快照:

snapshot of the data 数据快照

here is dput of my data: 这是我的数据的输出:

dput(as.data.frame(data))
structure(list(Activity1 = structure(c(2L, 1L, 2L, 1L, 1L, 1L, 
1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 
2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 
2L, 2L), .Label = c("a", "b"), class = "factor"), Activity2 = 
structure(c(2L, 
1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 
1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 1L, 2L, 2L, 1L, 2L, 2L), .Label = c("a", "b"), class = "factor"), 
Activity3 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), .Label = c("a", "b"), class = "factor"), Activity4 = structure(c(2L, 
1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("b", 
"e"), class = "factor"), Activity5 = structure(c(2L, 1L, 
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 
2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("b", "d"), class = "factor"), 
Activity6 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), .Label = c("d", "e"), class = "factor"), Activity7 = structure(c(1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 
2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L), .Label = c("a", 
"c"), class = "factor"), Activity8 = structure(c(2L, 2L, 
4L, 2L, 2L, 2L, 2L, 4L, 2L, 1L, 3L, 1L, 2L, 4L, 1L, 2L, 2L, 
2L, 2L, 2L, 4L, 2L, 3L, 2L, 4L, 2L, 1L, 2L, 3L, 4L, 2L, 2L, 
1L, 2L, 2L, 2L, 3L, 4L, 1L, 2L, 2L, 2L, 3L, 1L, 1L, 2L, 2L, 
3L, 2L, 2L, 2L, 3L, 2L, 2L, 4L, 3L), .Label = c("a", "c", 
"d", "e"), class = "factor"), Audit = structure(c(3L, 1L, 
3L, 2L, 1L, 1L, 1L, 3L, 3L, 2L, 3L, 2L, 2L, 3L, 2L, 1L, 3L, 
3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 3L, 1L, 2L, 
2L, 1L, 3L, 2L, 3L, 3L, 2L, 1L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 3L), .Label = c("A", "B", 
"U"), class = "factor")), .Names = c("Activity1", "Activity2", 
"Activity3", "Activity4", "Activity5", "Activity6", "Activity7", 
"Activity8", "Audit"), row.names = c(NA, -56L), class = "data.frame")

Here is the dput of my test data: 这是我的测试数据的输出:

> dput(as.data.frame(test))
structure(list(Activity1 = structure(c(1L, 1L, 1L, 2L, 2L, 1L, 
1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L), .Label = c("a", "b"), class = 
"factor"), 
Activity2 = structure(c(1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 
1L, 2L, 2L, 1L, 2L, 2L), .Label = c("a", "b"), class = "factor"), 
Activity3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("a", "b"), class = "factor"), 
Activity4 = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("b", "e"), class = "factor"), 
Activity5 = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("b", "d"), class = "factor"), 
Activity6 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("d", "e"), class = "factor"), 
Activity7 = structure(c(2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 1L), .Label = c("a", "c"), class = "factor"), 
Activity8 = structure(c(2L, 2L, 2L, 4L, 2L, 2L, 2L, 3L, 2L, 
2L, 3L, 2L, 2L, 4L, 3L), .Label = c("a", "c", "d", "e"), class = "factor"), 
Audit = structure(c(2L, 3L, 2L, 3L, 3L, 1L, 2L, 3L, 1L, 3L, 
3L, 3L, 2L, 3L, 3L), .Label = c("A", "B", "U"), class = "factor")), .Names = c("Activity1", 
"Activity2", "Activity3", "Activity4", "Activity5", "Activity6", 
"Activity7", "Activity8", "Audit"), class = "data.frame", row.names = c(NA, 
-15L))

Can anyone help me with this please? 有人可以帮我吗? Thanks 谢谢

I have just found out the reason why my confusionMatrix was nbot working properly. 我刚刚发现了我的confusionMatrix无法正常工作的原因。 This was because I was loading library(caret) as well as library(ModelMetrics) at the start of the code. 这是因为我在代码开始时正在加载库(插入符)和库(ModelMetrics)。 as soon as I commented out library(ModelMetrics) and retarted my R, I was able to get the Confusion Matrix working perfectly fine. 我注释掉library(ModelMetrics)并拖慢了我的R语言后,我就能使Confusion Matrix正常工作。

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

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