Naive Bayes Classifier using R

Introduction


           

Calculating Probability…..


Naive Bayes Classifier using R :


install.packages("e1071")  # Package for naive Algorithm
install.packages("caTools")  # Package for spliting the dataset
library(e1071)
library(caTools)

# Read the dataset
data_naive<-iris
> summary(data_naive)

   

# Partitioning the dataset into train and test
> split=sample.split(data_naive$Species,SplitRatio = 0.7)
> train_data=subset(data_naive,split==T)
> test_data=subset(data_naive,split==F)

# Fitting naïve_bayes model
> model<-naiveBayes(Species~.,data=iris)
> print(model)

Naive Bayes Classifier for Discrete Predictors Call:
naiveBayes.default(x = X, y = Y, laplace = laplace)

A-priori probabilities:
Y
 setosa    versicolor   virginica
  0.3333333   0.3333333   0.3333333

Conditional probabilities:
     Sepal.Length
Y       [,1]    [,2]
setosa   5.006  0.3524897
versicolor   5.936  0.5161711
virginica   6.588  0.6358796

     Sepal.Length
Y       [,1]    [,2]
setosa   3.428  0.3790644
versicolor   2.770  0.3137983
virginica   2.974  0.3224966

     Petal.Length
Y       [,1]    [,2]
setosa   1.462  0.1736640
versicolor    4.260    0.4699110
virginica   5.552  0.5518947

     Petal.Length
Y       [,1]    [,2]
setosa   0.246 0.1053856
versicolor   1.326  0.1977527
virginica   2.026    0.2746501

> pred<-predict(model,test_data)
> table(pred,test_data$Species)

pred    setosa  versicolor  virginica
setosa    15     0         0
versicolor   0     15     2
versicolor   0     15     2