Okay, my friends, are you ready to finish this?
It's actually very simple now, and I'm sure that you're confident
that the solution I'm about to give you will be the same as your solution.
Because indeed.
Now, the only thing that we have to do is juggle with our different
machine learning toolkits, including the Data preprocessing toolkit
and the classic toolkit to complete this implementation.
So let's do this.
Starting with splitting the data set into the training set in a test set.
Well, that's super easy.
We're ready to do this in only one copy paste because we have indeed
the matrix of features x and the dependent variable vector y.
Therefore, the only thing that we have to do here
is just to go to our data preprocessing template
and then take exactly these two lines of code
to indeed split our data set composed of the matrix of features X
and the dependent variable vector y into, well,
a new training set and test it.
And that's our first copy paste.
And of course here we have nothing to change.
Now next step training the naive base model on the training set.
So here we'll have to juggle with another of our machine
learning toolkit which is of course the classification toolkit.
So we're going to go back into our whole machine learning.
Is that folder.
Then we're going to,
you know, use this little shortcut here to go back to the base of the folder,
which is this one, machine learning A to Z codes and data sets.
Then we're going to go into part three classification.
And then we'll see all our different models including the Naive Bayes.
But I would just like to remind that, you know, the choice of Naive
Bayes was just based on my experience.
I observed that the Naive Bayes does very well with natural language
processing problems.
But I'll give you another exercise at the end of this tutorial,
which will be to beat me or, you know, to beat the score
that we're going to get at the end of this implementation.
And so your goal will be to get an even better accuracy.
And if you get it, you'll post it in the comments.
Or you can send me a private message to say that indeed, you managed
to beat the accuracy score that we're about to get together
and that you probably got by yourself when doing this exercise.
All right, so there we go.
Let's just choose for now the Naive Bayes model.
So we're going to go into the section section 18 Naive Bayes.
Then we're going to go into Python.
And then we're going to open this Naive Bayes implementation
open with Google Collaboratory.
And you know I can just put it here.
It is now opening loading it laying out the notebook.
And there we go.
And now you have everything.
You can just find the cell that's, you know, trained Naive Bayes
model on the training set, which is right here.
By the way, you could also take, you know, your model selection folder
containing all the classification models as you want.
But there we go.
What we need right now is this cell and nothing else.
And we actually don't have anything to change inside.
So that's all good.
Now let's go back to our copy of our natural
language Processing implementation.
Let's create a new code cell here.
And let's just paste that cell to train.
Well the Gaussian naive base model on the training set
composed of X train and Y train that was just created just before.
All right.
Good.
Now next step predicting the test results.
Well, once again here we won't have anything to do
except a simple copy paste still from our Naive Bayes implementation.
Because you know what?
We just want to do here is display next to each other the vector of predictions
and the vector of real results containing the real reviews.
You know, the real outcomes of the reviews, whether they're positive,
which gives a one or negative which gives a zero.
So here I just copied and I'm about to paste that here in a new code cell.
All right.
And this will indeed print next to each other.
First the vector of predictions which we got here in this first line of code,
and the vector of real results containing the real outcomes of the reviews.
Perfect.
And finally making the confusion matrix.
That's our last step here.
And once again we're going to go back to our two kids.
You know the classification toolkit for Naive Bayes.
We're going to scroll down a bit more.
And we're going to find indeed the confusion matrix
computing as well, the accuracy score.
You know, the accuracy being simply the number of correct predictions
divided by the total number of observations in the test set.
Of course.
All right.
So let's copy and paste that in this new code cell.
And there you go my friends.
Now this implementation is over.
We finished it in just a few seconds
or you know in a few minutes with the explanation.
But there we go.
That's what I mean by juggling with your different toolkits.
You can be super efficient at implementing a classification
or regression model by using your code templates.
All right.
Now it's showtime.
We will execute the cells that we haven't executed so far.
So the last one we executed with this one, basically everything that is related
to the bag of words model.
So now let's play the rest of the cell starting this one
which will split the data set into the training set and test set.
All good.
Now that we have the training set,
we're going to train the Naive Bayes model on the training set.
And all good again.
And now we're
going to predict the test set results by displaying next to each other
the vector of predictions and the vector of real outcomes of the reviews.
And well, well look at this.
We don't start well because we start with three incorrect predictions.
Right?
For the first review, which, be careful, is not well, love this place
because these are the reviews of the test set, not the whole data set.
So this is not the first review.
I love this place.
This is just a random
review taken from the original data set and put into the test set.
But anyway, for this first review of the test set, our model predicted
this review to be positive, whereas in reality it is negative.
Same for the second review predicted positive, but in reality negative.
The same for the third review predicted positive, but in reality negative.
And then this is correct.
That's negative review which was indeed predicted as negative.
Same for this one.
Negative review predicted as negative here.
Another mistake.
Negative review predicted as positive here.
Correct prediction.
Positive review predicted as positive then incorrect prediction.
Negative review predicted as positive, then an incorrect prediction again.
Negative review predicted as positive and correct.
Correct. Correct. Incorrect.
Anyway. So yeah.
But by scrolling down we can see you know that
we actually have many correct predictions.
And anyway, we're going to check that right away with our confusion
matrix below.
So I'm actually going to scroll down from here.
There we go. And perfect.
So this is our last cell which will display the confusion matrix
and compute the accuracy score which I will want you to beat
right after this tutorial.
As a final exercise of NLP.
And so let's play the cell to see what the confusion matrix looks like,
and mostly to see the final accuracy, which is 73% all right.
So that's pretty good.
But I'm sure we can do better.
You know there are many ways to better.
And so I really look forward to seeing your results.
You know after you experiment with more classification models or even
by doing a better cleaning of the text, you know, the reviews,
maybe you can add some more exclusions in the list of stopwords.
You know, we exclude it
not from the list of stopwords, but maybe you can exclude as well.
Isn't you know, I know that isn't is actually part of the Stopwords list.
So you know you can do some extra work in order to improve this,
because I'm sure that we can get a better accuracy than 73%.
But still, this is pretty good.
You know, remember that we actually trained a machine to understand English.
And, you know, a couple of years ago, maybe decades ago,
this would definitely seem very challenging.
But here we did it in just a few minutes.
And so that's absolutely incredible.
And the model predicts whether these English written reviews
are positive or negative correctly 73% of the time.
So that's really, really good.
And that's the confusion matrix with 55 correct predictions of negative reviews,
91 correct predictions of positive reviews, 42 incorrect predictions
of positive reviews, and 12 incorrect predictions of negative reviews.
All right, so try to reduce these two numbers here
and let me know or let everybody know in the comments
what you managed to get, which solution you managed to improve.
And I look forward to seeing if you managed to, you know,
for example, go over 80%.
That would be fantastic.
You know, you would definitely beat me by pretty far.
All right.
So now we're done with natural language processing.
I hope you liked this introduction to sentiment analysis.
If you liked NLP and if you want to,
you know, study more in-depth this branch of machine learning,
well know that we have other courses about chatbots, about the Bert model.
So I really recommend to check it out.
But first I recommend that, you know you complete this journey of machine learning.
And speaking of this, well, the next step of our journey here
is to enter the fascinating world of deep learning, where we will, you know,
mimic the process of the human brain to actually give for the first time
to our AI, an artificial brain which will itself perform some predictions.
It's super fascinating.
It's actually, you know, the most fascinating branch of machine learning,
because this is the closest one to human intelligence.
So now I just can't wait to see you in this next
part to enter the world of deep learning.
And until then, enjoy machine learning.