简体   繁体   English

当未使用R中的插入符号包预测所有类别时的混淆矩阵

[英]Confusion matrix when all classes are not predicted with caret package in R

I have a classification model predict(model, test.x) to evaluate a model on a data with 11 classes, the result of prediction: 我有一个分类模型predict(model, test.x)来评估具有11个类的数据的模型,预测的结果是:

table(predicted_class)
0   1   2   3   5   6   8  10 
7   6  232  11  74  58   1   1 

My test lables (truth) are: 我的测试标签(真相)是:

table(test.y)
  0   1   2   3   4   5   6   7   8   9  10 
105  16  78  25  14  74  12   9  23  15  19 

When I want to obtain the confusion matrix with caret package, I have this error message because classes 7 and 9 are not predicted by my model: 当我想使用插入符号包获取混淆矩阵时,出现此错误消息,因为模型未预测类7和9:

caret::confusionMatrix(test.y, predicted_class, mode = "everything")
Error in confusionMatrix.default(test.y, predicted_class,  : 
  the data cannot have more levels than the reference

How can I obtain a confusion matrix when some factor level are missing in prediction: How can I add 0 automatically for predicted_class for missing classes (in this case 4, 7 and 9) 当预测中缺少某些因子水平时,如何获得混淆矩阵:如何为缺少的类(在本例中为4、7和9)的预测类自动添加0

Make the levels same by joining the factors with union 通过结合union因素使水平相同

all_class <- union(predicted_class, test.y)
newtable <- table(factor(predicted_class, all_class), factor(test.y, all_class))
caret::confusionMatrix(newtable)

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

相关问题 如何使用插入符号 package 在 R 中的 plot 混淆矩阵 - How to plot confusion matrix in R with caret package R 如何使用 caret 包可视化混淆矩阵 - R how to visualize confusion matrix using the caret package 如何加载已计算的混淆矩阵以与R中的插入符号包一起使用 - How to load already computed confusion matrix to be used with the caret package in R 混淆矩阵和插入符号包-Rpart算法 - Confusion Matrix and caret package - rpart algorithm 获取混淆矩阵,预测概率图,使用 R 中的 glmnet 包设置截止值 - Get a confusion matrix , predicted probability plot, set a cutoff value using glmnet package in R 如何使用插入符号 package 获得混淆矩阵? - How to obtain confusion matrix using caret package? R Caret中随机森林的混淆矩阵 - Confusion matrix for random forest in R Caret 使用R的Caret包计算预测值的预测间隔 - Calculate Prediction Intervals of a predicted value using Caret package of R 在查看分类后的混淆矩阵时,R 包 bartMachine 中是否存在错误? - Is there an error in the R package, bartMachine, when looking at the confusion matrix following classification? 如何使用 Caret 包中的“gbm”方法生成混淆矩阵 - How to Produce a Confusion Matrix using the 'gbm' Method in the Caret Package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM