r - 为什么在尝试在决策树中创建混淆矩阵时出现错误?
问题描述
我正在学习如何在 r 中使用决策树。
我做了一个模型,做了一个预测。我想检查我的模型的准确性。但是,当我尝试使用表函数制作混淆矩阵时,我得到了错误:
表中的错误(test_data$Outcome,predictn):所有参数必须具有相同的长度
我使用的代码是:
data = read.csv("C:/Users/VIJAY/Desktop/ML/logistic regression/diabetes.csv")
head(data)
dim(data)
library(rpart)
library(rpart.plot)
library(caret)
s = sample(768,600)
train_data = data[s,]
test_data = data[-s,]
model = rpart(Outcome ~.,data = train_data, method = "class")
rpart.plot(model,cex = .9)
predictn = predict(model,data= test_data,type = "class")
tab = table(test_data$Outcome,predictn)
解决方案
Your response from the test set and predictions have different lengths. I would say that predictions weren't made for all observations (maybe because of missing values of some predictors - for this consider using surrogate variables or deleting the rows which have missing values in these predictors in the test set).
btw, when you are using caret, there is a nice function caret::confusionMatrix()
推荐阅读
- android - 无法将子视图传播到其父线性布局的两侧
- javascript - 单击事件仅适用于容器 + 使 querySelectorAll 在 IE11 中工作
- swift - 采用泛型类型的闭包?
- powershell - 如何从 Windows 10 获得干净的排序和过滤路由表?
- python - 如何创建一个空的python虚拟环境
- javascript - 在 javascript 中学习 OO:错误 - 无法读取未定义的属性
- python - 如何使用 python 有条件地对一个值进行 bin 处理?
- firebase - firebase.auth().signInWithCredential 错误没有电子邮件/凭据属性
- angular - Angular - 包含类别和类别中的项目的菜单(更整洁的解决方案)
- javascript - 将 JSON 数据数组分组到另一个数组