All right.
And that's all you will see that it will work perfectly well.
So I'll show you right now
let's select what's above, which is all the pre-processing steps.
So command and control plus end to execute.
All right. Pre-processing. Well done.
We are good to go.
And now we can create our classifier.
So yes let's do this.
If your 1071 package is not selected you need to select that as well.
So command and control plus enter to execute.
And here is our classifier.
Awesome.
So as you can see it works perfectly well.
The classifier is well created using only those two informations.
And when you think about it
well this is all we need to train a classifier
because we need the information of the independent variables
and the information
of the dependent variable, so that it can learn the correlations
between the independent variables and the dependent variable.
All right. So that makes sense.
And now we can create our vector of prediction.
Why pred using the predict function on our classifier and on our new data
which is the test set.
So here we go.
White bread is created.
Let's have a quick look. Why pred.
So for the first time we are obtaining something here.
Remember before when we entered white bread in the console,
we had all the predictions listed in the console.
But here we have this factor zero.
Here.
So that means that white bread is a vector of vectors but with no vector.
So it's basically an empty vector.
And that is because the naive Bayes function of the 1871 library
didn't recognize our dependent variable vector purchased
as a categorical variable with zero and one factors.
So far, the libraries and functions we've been using
recognized the dependent variable as factors.
So we didn't have any issues with our predictions,
and we didn't have to encode the dependent variable purchased as factors.
But here with Naive Bayes it's not the case.
It doesn't recognize the dependent variable purchased as vectors.
So we need to encode it as factors.
Because as you can see, if we try to compute the confusion
matrix below, we will get this following error message.
All arguments must have the same length.
Well yes indeed we have a problem because the two arguments here are to set three.
That is the third column purchased of the test set, and then white bread.
So since the set three has length 100 and white bread has length
zero, because it's an empty vector with no vectors,
then obviously we can not compute any confusion matrix.
So what we need to do is to jump back up to the pre-processing.
First step to encode are dependent variable purchased as vectors.
Then the naive base function will recognize the dependent variable
as vectors, and will perfectly be able to create a classifier
that will allow the predict function to return the expected vector
of predictions of y print.
So it's great that you see this error, because this is a classic error in machine
learning in R, and that way from now on, you will make this error in the future.
Or if you make it, you'll know how to fix it.
So let's do it right now.
Let's add a new code section here.
And let's call it and coding
the target feature
as vector.
All right.
And now let's factorize R column purchased okay.
Let's do this.
So we will take it from start.
That means that we will take the purchased
dependent variable column of the data set.
And then we will recompute all of these to set
this purchase column everywhere for all of set.
That is a training set. And the test set okay.
So we're going to take our last column from our data set.
So we're going to type data set dollar purchased
write equals
factor and in parenthesis data set.
And then again we're taking the purchased column
that is the dependent variable column of our data set.
So again we are taking the dollar sign here and purchased to take this column.
And then here
we will just specify the levels or levels are
C zero and one.
All right.
And that's it.
That's how you encode your dependent variable column purchased into factors.
All right. So we're going to start from scratch now.
So to do this we can clear everything.
So that's what we're going to do to everything here.
So we're cleaning the data clearing the script here.
So I'm pressing control L to clear.
And that's.