## Naive Bayes Classifier using R

### Introduction

• Naive Bayes is a simple technique for constructing classifier.
• Models that requires to label class to the feature vector in classification problem.
• How do you classify a fruit to apple/orange/Guava. There shapes are different, colors are different….
• In Naive classification we start with decision class. Decision class is our target variable. Here the decision class is Sold

### Calculating Probability…..

• It is an unleveled dataset.
• Let us calculate decision class for unleveled sample.
• Sample X = Red , Hybrid and Foreign
• P(X / Yes) = P(Red/Yes)*P(Hybrid/Yes)*P(Foreign/Yes)
= 3/5 * 1/5 * 2/5 = 0.024
• P(X / No) = P(Red/No)*P(Hybrid/No)*P(Foreign/No)
= 2/5 * 3/5 * 3/5 =0.072

### Naive Bayes Classifier using R :

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

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