R语言 错误: `data` and `reference` should be factors with the same levels.

谢谢点击进来,如果您觉得有用,麻烦点赞鼓励一下!
  • 报错如下图:
    0

这个问题困扰了好久,看这个信息也没看到是哪里出错,然后一行一行执行。最终找到了

rf.confusionMatrix <- confusionMatrix(rf.class , data.test$Class, positive = "T")

这行代码是通过预测出来的结果和测试集比较,生成混淆矩阵。那么这个levels指的就是T或者F。也就是说可能有数据没匹配上或者变量类型不是factor。我们一步一步验证。

  • 首先用table函数查看数据条目,可以看到,数据条目是一样的,那么就不是这个问题。
    1
  • 接下来查看变量类型,如下图所示,变量类型不一样。
    2
  • 看到这里,我们就知道解决办法了,我们需要把预测结果转化为factor类型。
rf.confusionMatrix <- confusionMatrix(factor(rf.class) , data.test$Class, positive = "T")
参考文章

StackOverflow-1
StackOverflow-2

  • 20
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Beyond Spreadsheets with R shows you how to take raw data and transform it for use in computations, tables, graphs, and more. You’ll build on simple programming techniques like loops and conditionals to create your own custom functions. You’ll come away with a toolkit of strategies for analyzing and visualizing data of all sorts using R and RStudio. Spreadsheets are powerful tools for many tasks, but if you need to interpret, interrogate, and present data, they can feel like the wrong tools for the task. That’s when R programming is the way to go. The R programming language provides a comfortable environment to properly handle all types of data. And within the open source RStudio development suite, you have at your fingertips easy-to-use ways to simplify complex manipulations and create reproducible processes for analysis and reporting. With Beyond Spreadsheets with R you’ll learn how to go from raw data to meaningful insights using R and RStudio. Each carefully crafted chapter covers a unique way to wrangle data, from understanding individual values to interacting with complex collections of data, including data you scrape from the web. You’ll build on simple programming techniques like loops and conditionals to create your own custom functions. You’ll come away with a toolkit of strategies for analyzing and visualizing data of all sorts. What’s inside How to start programming with R and RStudio Understanding and implementing important R structures and operators Installing and working with R packages Tidying, refining, and plotting your data

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值