如何解决“数据不能具有比参考更多的级别”?使用confusioMatrix时出错? [英] How to solve "The data cannot have more levels than the reference" error when using confusioMatrix?

查看:603
本文介绍了如何解决“数据不能具有比参考更多的级别”?使用confusioMatrix时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R编程。
我将数据除以train&

I'm using R programming. I divided the data as train & test for predicting accuracy.

这是我的代码:

library("tree")
credit<-read.csv("C:/Users/Administrator/Desktop/german_credit (2).csv")

library("caret")
set.seed(1000)

intrain<-createDataPartition(y=credit$Creditability,p=0.7,list=FALSE)
train<-credit[intrain, ]
test<-credit[-intrain, ]

treemod<-tree(Creditability~. , data=train)
plot(treemod)
text(treemod)

cv.trees<-cv.tree(treemod,FUN=prune.tree)
plot(cv.trees)

prune.trees<-prune.tree(treemod,best=3)
plot(prune.trees)
text(prune.trees,pretty=0)

install.packages("e1071")
library("e1071")
treepred<-predict(prune.trees, newdata=test)

confusionMatrix(treepred, test$Creditability)

confusionMatrix 中发生以下错误消息:

The following error message happens in confusionMatrix:


confusionMatr错误ix.default(rpartpred,test $ Creditability):数据的级别不能超过参考

Error in confusionMatrix.default(rpartpred, test$Creditability) : the data cannot have more levels than the reference

信用数据可以在此处下载网站。

http://freakonometrics.free.fr/german_credit.csv

The credit data can download at this site.
http://freakonometrics.free.fr/german_credit.csv

推荐答案

如果仔细查看情节,您会看到您正在训练的是回归树,而不是经典树。

If you look carefully at your plots, you will see that you are training a regression tree and not a classication tree.

如果在读取数据后运行 credit $ Creditability<-as.factor(credit $ Creditability)并在预测函数中使用 type = class ,您的代码应该可以使用。

If you run credit$Creditability <- as.factor(credit$Creditability) after reading in the data and use type = "class" in the predict function, your code should work.

代码:

credit <- read.csv("http://freakonometrics.free.fr/german_credit.csv" )

credit$Creditability <- as.factor(credit$Creditability)

library(caret)
library(tree)
library(e1071)

set.seed(1000)
intrain <- createDataPartition(y = credit$Creditability, p = 0.7, list = FALSE)
train <- credit[intrain, ]
test <- credit[-intrain, ]

treemod <- tree(Creditability ~ ., data = train, )

cv.trees <- cv.tree(treemod, FUN = prune.tree)
plot(cv.trees)

prune.trees <- prune.tree(treemod, best = 3)
plot(prune.trees)
text(prune.trees, pretty = 0)

treepred <- predict(prune.trees, newdata = test, type = "class")
confusionMatrix(treepred, test$Creditability)

这篇关于如何解决“数据不能具有比参考更多的级别”?使用confusioMatrix时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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