利用简单数据生成决策树,结尾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解答一下为什么,这个该怎么改最后两行呀?