Hello guys.
We are going to continue the discussion with respect to NLP.
In our previous video, we have already seen that we have discussed how one hot encoding works, right?
And if I probably talk with respect to different types of ways how we can convert a text into a vectors,
we had completed already one hot encoding.
Now we are into the second that is bag of words.
Now let's go ahead and understand how bag of words actually work.
And this is super important because this is the technique which you can do simple tasks like sentiment
classifications or whether, uh, basically, in short, a text classification kind of task easily you
will be able to solve, okay, uh, some of the applications famous application like whether a mail
is a spam or a ham, everything, you'll be able to solve it.
Now let's say that I have a data set.
Okay.
And in this particular data set, uh, I'm just saying that this is a positive or negative statements.
Now, in this particular data set, I have three sentences.
Let's say he is a good boy.
She is a good girl.
Boy and girl are good okay.
And all these are like positive statements.
So the output of all this are ones right?
In supervised machine learning we really need to know the output also.
Now let me go step by step and show you that how bag of words are implemented.
So this is basically the step one right?
I have the data set.
Now let's go to the step two.
Now what happens in the step two is that once I go over here right, there's multiple steps that uh,
actually occur.
And initially we should also go with respect to this.
The first thing is that if I probably consider two things you are going to basically happen.
First of all, we will lower all the words and usually all the steps We will even be doing for all the
other techniques also.
First we will lower all the words and then we will probably apply stop words, right?
And this also I have already shown you.
Now when I lower all the words, let's say my sentence one write my sentence one now becomes I'm going
to take only this text data.
I don't have to worry about the output because later on, once we convert this text into vectors, we
can apply it to the machine learning algorithm.
So in the sentence one here you can see as soon as I lower the words what will happen?
This all will become smaller.
Now why I'm doing this?
Because there may be some repeated words.
Now in this particular case, the capital with capital B you have boy here small B you have a boy.
So both these words are same, but since it is in uppercase this will be treated as a separate word.
So we really need to lower all lowercase all the words.
So I'm just going to write lowercase all the words now in sentence one after lowering casing.
So what will happen is that this all will become smaller letters.
Then once we apply stop words.
Now what happens in stop words?
This is all words like he she is a you know it will get deleted, right?
Because we don't require this particular word for any kind of task like sentiment analysis.
Important words like good boy, good girl is basically required.
So this all things will go like and R will also go.
Now what will happen with respect to this?
The sentence one words will become only like this.
Good boy.
Okay, I'm showing you step by step, uh, with the help of Python.
When we do with the help of libraries, when we do, it is very much simple.
We don't have to worry that much about it.
Okay.
Uh, we just have to use one library.
So sentence two.
What it will become once it is stop, words will get removed.
It will become good girl, right?
And similarly with sentence three, I will be having this specific words that is boy girl.
Good, right?
So all these things are there.
I have sentence one, sentence two, sentence three.
Perfect.
Uh, now what we do is that we go ahead and calculate the vocabulary.
Now, how many words are there in this vocabulary?
We have?
Good boy.
girl, boy and girl.
And again it is getting repeated.
So if I probably consider how many unique words are there in the vocabulary, so I will be able to write
it over.
The first word is nothing but good.
So I'll go ahead and write good.
Good is my first word.
And one more thing that I will probably write that what is the frequency of this specific word?
Like how many times this words are there in different different sentences.
And obviously you can see one, two, three, three are there.
So I'm going to keep the count as three.
Then we have something called as boy over here.
You will be able to see how many times boy are there.
So in sentence three also boy is there in sentence one also boy is there.
So I'm just going to make the count to two and coming to the next one which is called as girl.
So again girl word is also there in this vocabulary.
And again girl also you will be able to see two times it is there right now.
First of all, you need to see that in this frequency with respect to different different vocabulary
words.
Is this in ascending order?
And obviously, uh, sorry, is this in descending order.
So maximum number of frequency will be put up in the first word.
Right then boys present two times and girl is present two times.
So this can be moved up and down.
But just understand what I have done is that based on this descending order I have just ordered all
these words.
So from it is basically from maximum to minimum okay.
So this is that perfect.
Now before applying bag of words you already have seen that.
How many vocabulary size.
What is the vocabulary size.
The vocabulary size is three right.
And I can see in a bigger dataset I can have this kind of words a lot.
So I will be having all these words like this and frequency will also be there.
And one important step is that it is not necessary that you use all the words.
This is also there in this bag of words itself.
Let's say that over here in the vocabulary there are 100 100 unique words.
And let's say some of the words are just present once, right?
So if some of the words are just present once, we even do not take that.
So when we are, when we'll be doing the coding with respect to a bag of words, you know, we also
have an option that we just select the top ten features or top 20 features.
Which of the words are getting repeated more and more.
And that is the importance of this particular frequency.
Okay.
Now perfect.
Uh, we have we have we are till here.
Now the next step will be that it is very, very simple now based on the top most frequency, what I'm
actually going to do, I'm going to keep this words as my feature.
So good will come over here boy will come over here and girl will come over here.
Okay?
Now you already know.
What is the sentence one right?
So sentence one, what will happen is that this sentence one has.
Good boy.
Now see how this will get converted into a vectors.
Wherever good is present, that will become one.
Wherever boy is present that will become one and remaining will become zero.
So this entire sentence will be able to see, uh, that it is getting converted to 110 as a vector.
Okay.
So this was the text.
Now this is getting converted to 110 as a vector.
Now similarly if I go with respect to sentence two, wherever there is a word like good that will become
one, wherever there is a girl that will become one and remaining all will become zero.
I'll talk about the advantages and disadvantages why we are doing this.
Previously in one hot encoded.
You saw that for every word we are doing this for every word, we are creating a vector.
But here for the entire sentence, this vector is coming, okay.
And there are a lot of advantages if when I talk about this, which I'll discuss, uh, first of all,
let's understand what a more we'll be having in this.
So s three you'll be able to see the sentence three here I have boy, girl and good.
So wherever boy is there that will become one girl is there that will become one and good will be there.
They will become one.
Okay, now this is my entire vectors and obviously I will also have my output variable.
The output variable can be one zeros.
Anything as such right.
If you are probably solving the sentiment analysis or something like that okay now this are my entire
vectors okay.
And this is the vector for the entire sentence okay.
Entire syntax.
And this is how the entire bag of words converts a text into a vectors.
Okay, now what we can do is that we can take this particular vectors.
We can train with an in machine learning model and will be able to get the output.
Now one more important thing that I really want to put, let's say that I, I have a word over here.
Right.
Good girl.
Girl.
Right.
So let's say if I have one more word like good.
Now in this case, what will happen?
Okay.
In this case what will happen usually in bag of words since good is repeated two times.
So I will increase the count to two okay.
In this particular case.
So I what I will do I will increase the count to two.
Okay.
Now there are two things.
One is binary bag of words and one is normal bag of words.
Now in the case of binary bag of words, even though the count is two, what it is going to do, it
is going to force it to become one.
So most of the time, if the word is present, it may be present any number of time.
The value is either one or it is zero.
And in normal bag of words, I can increase the count to two, three, four based on the number of words
that is there.
So that is the basic difference between binary bag of words and bag of words.
That basically means here you will be having only ones and zeros.
And here based on the frequency count will get updated.
Count will get updated based on frequency.
So this is the basic difference with respect to this.
Okay.
So I hope you have understood like how with the help of Bag of words we are converting a text into a
vectors.
Now in the next video I'm going to discuss about what are the advantages and disadvantages with respect
to this, like how we have actually discussed in one hot encoding.
Okay.
And first of all, I'll talk about like what all problems are getting solved.
And all right.
So yes, I will see you all in the next video.
I hope you are able to understand it and just try with some other data set of your own.
Just try to create a text, try to perform all these steps and then try to convert that into a vectors.
So yes, I will see you all in the next video.
Thank you.