Miracleeeees 2022-03-28 19:13 采纳率: 0%
浏览 157

【R语言】决策树及可视化,遇到报错,请教友友!

利用简单数据生成决策树,结尾predict有个报错查了很久也没看懂为什么,求个uu解释一下。

一、数据:

[[1]]
   Play  Outlook Temperature Humidity   Wind
1   yes    rainy        cool   normal FALSE 
2    no    rainy        cool   normal  TRUE 
3   yes overcast         hot     high FALSE 
4    no    sunny        mild     high FALSE 
5   yes    rainy        cool   normal FALSE 
6   yes    sunny        cool   normal FALSE 
7   yes    rainy        cool   normal FALSE 
8   yes    sunny         hot   normal FALSE 
9   yes overcast        mild     high  TRUE 
10   no    sunny        mild     high   TRUE

二、个人写的代码,前面都没问题,最后两行predict报错

```r
#导入数据和包
library(rpart)
library(rpart.plot)
d1<-read.csv(file.choose())
summary(d1)
#构建决策树
tree1<-rpart(Play~Outlook+Temperature+Humidity+Wind,
             method="class",
             data=d1,
             control=rpart.control(minsplit=1),
             parms=list(split='information')
)
summary(tree1)
#决策树可视化
rpart.plot(tree1,type=4,extra=2,clip.right.labs=FALSE,varlen=0,faclen=0)

#预测
newdata1<-data.frame(Outlook="sunny",Temperature="mild",
                     Humidity="high",Wind=FALSE)
newdata1
predict(tree1,newdata = newdata1,type="prob")
predict(tree1,newdata=newdata1,type="class")



###### 三、报错如下:

```r
> newdata1
  Outlook Temperature Humidity  Wind
1   sunny        mild     high FALSE
> predict(tree1,newdata = newdata1,type="prob")
Error: variable 'Wind' was fitted with type "character" but type "logical" was supplied
In addition: Warning message:
In model.frame.default(Terms, newdata, na.action = na.action, xlev = attr(object,  :
  variable 'Wind' is not a factor
> predict(tree1,newdata=newdata1,type="class")
Error: variable 'Wind' was fitted with type "character" but type "logical" was supplied
In addition: Warning message:
In model.frame.default(Terms, newdata, na.action = na.action, xlev = attr(object,  :
  variable 'Wind' is not a factor

希望哪个uu解答一下为什么,这个该怎么改最后两行呀?

  • 写回答

4条回答 默认 最新

  • CSDN专家-HGJ 2022-03-28 23:06
    关注

    查看一下R及两个包安装的版本。如下版本测试代码运行和结果正常,无报错。:
    R1.4.0
    rpart.plot_3.1.0
    rpart_4.1.16
    :

    评论

报告相同问题?

问题事件

  • 创建了问题 3月28日

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog