R:多类矩阵 [英] R: Multiclass Matrices

查看:54
本文介绍了R:多类矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R编程语言.我正在尝试学习如何制作一个混淆矩阵".用于多类变量(例如如何构造混淆多类变量的矩阵).

I am working with the R programming language. I am trying to learn how to make a "confusion matrix" for multiclass variables (e.g. How to construct the confusion matrix for a multi class variable).

假设我生成了一些数据并拟合了决策树模型:

Suppose I generate some data and fit a decision tree model :

#load libraries

library(rpart)
library(caret)
    
#generate data

a <- rnorm(1000, 10, 10)

b <- rnorm(1000, 10, 5)

d <- rnorm(1000, 5, 10)
 


group_1 <- sample( LETTERS[1:3], 1000, replace=TRUE, prob=c(0.33,0.33,0.34) )


e = data.frame(a,b,d, group_1)

e$group_1 = as.factor(d$group_1)

#split data into train and test set
trainIndex <- createDataPartition(e$group_1, p = .8, 
                                  list = FALSE, 
                                  times = 1)
training <- e[trainIndex,]
test  <- e[-trainIndex,]


fitControl <- trainControl(## 10-fold CV
    method = "repeatedcv",
    number = 5,
    ## repeated ten times
    repeats = 1)
    
#fit decision tree model
    TreeFit <- train(group_1 ~ ., data = training, 
                     method = "rpart2", 
                     trControl = fitControl)

从这里,我可以将结果存储到混淆矩阵"中:

From here, I am able to store the results into a "confusion matrix":

pred <- predict(TreeFit,test)
table_example <- table(pred,test$group_1)

这满足了我的要求-但此表"要求我手动计算"A","B"的不同精度度量.和"C"表示(以及总准确度).

This satisfies my requirements - but this "table" requires me to manually calculate the different accuracy metrics of "A", "B" and "C" (as well as the total accuracy).

我的问题:是否可以使用 caret :: confusionMatrix()命令解决此问题?

My question: Is it possible to use the caret::confusionMatrix() command for this problem?

例如

  pred <- predict(TreeFit, test, type = "prob")
  labels_example <- as.factor(ifelse(pred[,2]>0.5, "1", "0"))
  con <- confusionMatrix(labels_example, test$group_1)

这样,我将能够直接从混淆矩阵中访问准确性测量值.例如. metric = con $ overall [1]

This way, I would be able to directly access the accuracy measurements from the confusion matrix. E.g. metric = con$overall[1]

谢谢

推荐答案

这是您要寻找的吗?

pred <- predict(
  TreeFit,
  test)
con <- confusionMatrix(
  test$group_1,
  pred)
con
con$overall[1]

与以下输出相同:

table(test$group_1, pred)

加上准确性指标.

这篇关于R:多类矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆