我用R caret训练了一个树模型.我现在正在尝试生成混淆矩阵并继续收到以下错误:
confusionMatrix.default(predictionsTree,testdata $ catgeory)出错:数据和参考因子必须具有相同的级别数
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)
Run Code Online (Sandbox Code Playgroud)
生成混淆矩阵时会发生错误.两个对象的级别相同.我无法弄清问题是什么.它们的结构和水平如下.它们应该是一样的.任何帮助将非常感谢,因为它让我破解!
> 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 …Run Code Online (Sandbox Code Playgroud)