Classifying using the Naïve Bayes approach

The e1071 package contains the naiveBayes function for the Naïve Bayes classification.

Getting ready

If you do not already have the e1071 and caret packages, install them now. Download the data files for this chapter from the book's website and place the electronics-purchase.csv file in your R working directory. Naïve Bayes requires all the variables to be categorical. So, if needed, you should first convert all variables accordingly—refer to the recipe Binning numerical data in Chapter, Acquire and Prepare the Ingredients – Your Data.

How to do it...

To classify using the Naïve Bayes method, follow these steps:

  1. Load the e1071 and caret packages:
    > library(e1071)
    > library(caret)
  2. Read the data:
    > ep <- read.csv("electronics-purchase.csv") ...

Get R: Recipes for Analysis, Visualization and Machine Learning now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.