Hello and welcome to this art tutorial.
Today we are going to implement the Naive Bayes algorithm on R.
So let's start right now.
Let's first set the folder as working directory.
So I'm going to part three classification.
And that's the folder.
So make sure you have the social network add CSV file.
And if that's the case
click on this more button here to set this folder as working directory.
And now let's start implementing Naive Bayes on R.
So we're going to take our classification template.
We're going to take everything from here to here.
And then
paste right here.
And now we just need to change a few things.
So first let's change the titles r usual so that we don't forget.
So here we are with the Naive Bayes classifier.
Same for the training set.
So that is plotting the title in our graph.
And now what we only need to do
is to create our Naive Bayes classifier.
So let's do this.
Going to remove this.
And now what are we going to do.
Well it's actually funny
because we are going to use a library that we already used before.
But for a different classifier
it's the E 1071 library.
So there is no surprise why this library is very popular.
One of the most popular ones, because it contains a lot of tools
tools for SVM and tools for Naive Bayes.
So here again we'll use this library.
So for those of you who didn't follow the SVM tutorials on R,
and for those of you who are starting R for the first time, maybe you don't have
the E 1071 packages in your packages list.
So if that's the case, you just need to type the command here.
Install dot packages and then in quotes here in the parenthesis
you just add the name of the library which is E 1071.
And then you just need to select this and execute to install the package.
I won't do it here because I already have mine installed.
But I promise you it will work properly.
So I'm just going to put that in comment.
Here we go.
And now let's just also import the library that is to select it
in the packages list.
Because here it's selected.
But it might not always be the case.
And if you want to automate your machine learning scripts, it's better
to always have this script line here that selects automatically your packages
through the library. E10 71.
And now we are ready to start creating our Naive Bayes classifier.
Okay, so let's do this.
As usual, we will create a variable classifier.
And then we will use the
Naive Bayes function of the E 1071 library.
The E 1071 library contains a lot of functions,
and one of them is to create a Naive Bayes classifier.
So okay, so careful with the, capitals.
So it's, not capital N, the capital B, all right.
But then, you know, R is helping you with the term.
So you would have this, Naive Bayes popping up here.
So you just have to press enter.
And now let's input the parameters.
So the parameters what are they.
Let's look at them.
We press F1 here to have a look.
And here there are.
So we will actually need to input only the two first arguments.
We will need to import the formula like for the other one.
It will work perfectly well with only the first two ones.
So you'll see.
Let's input x here, which is by the way the training set.
So the training set.
But we need to remove
the last column of our training set because X is actually the matrix features.
You see it's written here in numeric matrix or data frame.
And by that they mean the matrix of features that is the matrix
of the independent variable.
So here, since the training set contains both the independent variables
and the dependent variable, we need to exclude the dependent variable.
So to do this we're going to add brackets here
and remove the last column by inputting here a minus.
And then the index of the last column which is well we can see here
it's minus three. So it's minus three.
All right. So perfect.
Then comma and then next argument.
So do you guess what the next argument is.
Well of course to train a classifier we need the independent variables
and the response.
And the response is the dependent variable.
So of course y is going to be the dependent variable.
So y equals and to take it we are going to take it this way.
We're going to input training set
dollar purchased.
So I'm choosing to write it this way because we can clearly see that
we are taking the dependent variable purchased.