ConfusionMatrix中的错误数据和参考因子必须具有相同数量的水平

编程入门 行业动态 更新时间:2024-10-07 04:31:22
本文介绍了ConfusionMatrix中的错误数据和参考因子必须具有相同数量的水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经用R插入符号训练了树模型.我现在正在尝试生成一个混淆矩阵,并不断出现以下错误:

I've trained a tree model with R caret. I'm now trying to generate a confusion matrix and keep getting the following error:

confusionMatrix.default(predictionsTree,testdata $ catgeory)中的错误 :数据和参考因子必须具有相同数量的水平

Error in confusionMatrix.default(predictionsTree, testdata$catgeory) : the data and reference factors must have the same number of levels

prob <- 0.5 #Specify class split singleSplit <- createDataPartition(modellingData2$category, p=prob, times=1, list=FALSE) cvControl <- trainControl(method="repeatedcv", number=10, repeats=5) traindata <- modellingData2[singleSplit,] testdata <- modellingData2[-singleSplit,] treeFit <- train(traindata$category~., data=traindata, trControl=cvControl, method="rpart", tuneLength=10) predictionsTree <- predict(treeFit, testdata) confusionMatrix(predictionsTree, testdata$catgeory)

生成混淆矩阵时发生错误.两个对象的级别相同.我不知道是什么问题.它们的结构和水平在下面给出. 它们应该是相同的.任何帮助将不胜感激,因为它使我无法自拔!

The error occurs when generating the confusion matrix. The levels are the same on both objects. I cant figure out what the problem is. Their structure and levels are given below. They should be the same. Any help would be greatly appreciated as its making me cracked!!

> str(predictionsTree) Factor w/ 30 levels "16-Merchant Service Charge",..: 28 22 22 22 22 6 6 6 6 6 ... > str(testdata$category) Factor w/ 30 levels "16-Merchant Service Charge",..: 30 30 7 7 7 7 7 30 7 7 ... > levels(predictionsTree) [1] "16-Merchant Service Charge" "17-Unpaid Cheque Fee" "18-Gov. Stamp Duty" "Misc" "26-Standard Transfer Charge" [6] "29-Bank Giro Credit" "3-Cheques Debit" "32-Standing Order - Debit" "33-Inter Branch Payment" "34-International" [11] "35-Point of Sale" "39-Direct Debits Received" "4-Notified Bank Fees" "40-Cash Lodged" "42-International Receipts" [16] "46-Direct Debits Paid" "56-Credit Card Receipts" "57-Inter Branch" "58-Unpaid Items" "59-Inter Company Transfers" [21] "6-Notified Interest Credited" "61-Domestic" "64-Charge Refund" "66-Inter Company Transfers" "67-Suppliers" [26] "68-Payroll" "69-Domestic" "73-Credit Card Payments" "82-CHAPS Fee" "Uncategorised" > levels(testdata$category) [1] "16-Merchant Service Charge" "17-Unpaid Cheque Fee" "18-Gov. Stamp Duty" "Misc" "26-Standard Transfer Charge" [6] "29-Bank Giro Credit" "3-Cheques Debit" "32-Standing Order - Debit" "33-Inter Branch Payment" "34-International" [11] "35-Point of Sale" "39-Direct Debits Received" "4-Notified Bank Fees" "40-Cash Lodged" "42-International Receipts" [16] "46-Direct Debits Paid" "56-Credit Card Receipts" "57-Inter Branch" "58-Unpaid Items" "59-Inter Company Transfers" [21] "6-Notified Interest Credited" "61-Domestic" "64-Charge Refund" "66-Inter Company Transfers" "67-Suppliers" [26] "68-Payroll" "69-Domestic" "73-Credit Card Payments" "82-CHAPS Fee" "Uncategorised"

推荐答案

尝试使用:

confusionMatrix(table(Argument 1, Argument 2))

那对我有用.

更多推荐

ConfusionMatrix中的错误数据和参考因子必须具有相同数量的水平

本文发布于:2023-11-29 09:13:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:因子   数量   错误   水平   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!