一、数据:
[[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
```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解答一下为什么,这个该怎么改最后两行呀?
查看一下R及两个包安装的版本。如下版本测试代码运行和结果正常,无报错。:
R1.4.0
rpart.plot_3.1.0
rpart_4.1.16
:
同学是zju的么呜呜我也是写到最后两步那里怎么都不会改了,你找到方法了吗
好像是因为数据集里的Wind数据后面有个空格,不能直接当做逻辑变量
请问uu最后改出来了吗 我也是zju的 也遇到了同样的问题 悲