案例三比较简单,不需要自己写公式算法,使用了R自带的naiveBayes函数。
代码如下:
> library(e1071)> classifier<-naiveBayes(iris[,1:4], iris[,5]) #或写成下面形式,都可以。 > classifier<- naiveBayes(Species ~ ., data = iris) #其中Species是类别变量 #预测 > predict(classifier, iris[1, -5])
预测结果为:
[1] setosa Levels: setosa versicolor virginica
和原数据一样!
*********************************这里是分割线**************************************
我们再拿这个方法来预测一下案例一中的样本。
#样本数据集: mydata <- matrix(c("sunny","hot","high","weak","no", "sunny","hot","high","strong","no", "overcast","hot","high","weak","yes", "rain","mild","high","weak","yes", "rain","cool","normal","weak","yes", "rain","cool","normal","strong","no", "overcast","cool","normal","strong","yes", "sunny","mild","high","weak","no", "sunny","cool","normal","weak","yes", "rain","mild","normal","weak","yes", "sunny","mild","normal","strong","yes", "overcast","mild","high","strong","yes", "overcast","hot","normal","weak","yes", "rain","mild","high","strong","no"), byrow = TRUE, nrow=14, ncol=5) #添加列名: colnames(mydata) <- c("outlook","temperature","humidity","wind","playtennis") #贝叶斯算法: m<-naiveBayes(mydata[,1:4], mydata[,5]) #或使用下面的方法 m<- naiveBayes(playtennis ~ ., data = mydata) #报错:Error in sum(x) : invalid ‘type‘ (character) of argument 无效的类型,只能是数字? #创建预测数据集: new_data = data.frame(outlook="rain", temperature="cool", humidity="normal", wind="strong", playtennis="so") #预测: predict(m, new_data)
在使用naiveBayes函数时报错:Error in sum(x) : invalid ‘type‘ (character) of argument
我们看一下官方文档,对data有这样一句描述:
data Either a data frame of predictors (categorical and/or numeric) or a contingency table.
data是一个数字类型的数据框。
时间: 2024-11-08 20:28:18