朴素贝叶斯分类器示例
rm(list=ls())
library(kernlab)
data(spam)
data <- spam
id <- sample(1:4601,4000)
train <- data[id,]
test <- data[-id,]
library(e1071)
nb=naiveBayes(type~., data=train)
pred2 <- predict(nb, test, type = "class")
table(pred2,test$type)
#pred2 nonspam spam
# nonspam 201 11
# spam 156 233
mean(pred2!=test$type)
# [1] 0.2778702
代入loss matrix中,需要在predict
中的type设为raw
predicted
L= observed nonspam 0 1
spam 10 0
p=predict(nb, newdata=test, type = "raw") # 直接返回近01两者的概率
pred2 <- ifelse(p[,1]*10>p[,2],"nonspam","spam")
table(pred2,test$type)
pred2 nonspam spam
nonspam 210 11
spam 147 233
mean(pred2!=test$type)
[1] 0.2628952