Explain with a case study how to perform a Naive Bayes analysis using R.

471    Asked by Nabhashahin in Data Science , Asked on Nov 21, 2019
Answered by Nabha shahin

To perform a Naive Bayes analysis let us read the following data

First we will import the data both for training and testing

training_set = read.csv(file.choose())#salary_train

test_set = read.csv(file.choose())# salary_test

Now we will encode the target feature as factor

# Encoding the target feature as factor

#dataset$Purchased = factor(dataset$Purchased, levels = c(0, 1))

Now we will implement Naive Bayes and fit the training data

install.packages('e1071')

library(e1071)

classifier = naiveBayes(x = training_set[-14],

                        y = training_set$Salary)

After fitting the model, we will predict the test data

# Predicting the Test set results

y_pred = predict(classifier, newdata = test_set[-14])

Y_pred

Now we will evaluate with confusion matrix

# Making the Confusion Matrix

cm = table(test_set[,14], y_pred)

cm



Your Answer

Interviews

Parent Categories