我无论如何也想不出如何在rpart上计算混淆矩阵。
以下是我所做的工作:
set.seed(12345)
UBANK_rand <- UBank[order(runif(1000)), ]
UBank_train <- UBank_rand[1:900, ]
UBank_test <- UBank_rand[901:1000, ]
dim(UBank_train)
dim(UBank_test)
#Build the formula for the Decision Tree
UB_tree <- Personal.Loan ~ Experience + Age+ Income +ZIP.Code + Family + CCAvg + Education
#Building the Decision Tree from Test Data
UB_rpart <- rpart(UB_tree, data=UBank_train)现在,我想我会做这样的事情
table(predict(UB_rpart, UBank_test, UBank_Test$Default))但这并没有给我一个混乱的矩阵。
发布于 2014-01-22 12:28:24
您没有提供可重现的示例,因此我将创建一个合成数据集:
set.seed(144)
df = data.frame(outcome = as.factor(sample(c(0, 1), 100, replace=T)),
x = rnorm(100))带有type="class"的rpart模型的predict函数将返回每个观察值的预测类。
library(rpart)
mod = rpart(outcome ~ x, data=df)
pred = predict(mod, type="class")
table(pred)
# pred
# 0 1
# 51 49 最后,您可以通过在预测结果和真实结果之间运行table来构建混淆矩阵:
table(pred, df$outcome)
# pred 0 1
# 0 36 15
# 1 14 35发布于 2016-09-21 17:33:01
你可以试试
pred <- predict(UB_rpart, UB_test) confusionMatrix(pred, UB_test$Personal.Loan)
https://stackoverflow.com/questions/21273934
复制相似问题