星期一, 十月 13, 2008

A quick tour to naiveBayes algorithm in R

Dataset used below can be downloaded from
http://archive.ics.uci.edu/ml/datasets/Mushroom

> install.packages("e1071")

> library(e1071)
> help(naiveBayes)
>
> mushroom = read.table("agaricus-lepiota.data", sep=",")
> index<-1:nrow(mushroom)
> testindex<-sample(index, trunc(length(index)/10))
> trainset<-mushroom[-testindex,]
> testset <-mushroom[testindex,]
> model<-naiveBayes(V1~., data=trainset)
> pred<-predict(model,testset[,-1],type="class")
> table(pred=pred,true=testset[,1])
true
pred e p
e 407 51
p 5 349

没有评论: