Implementing Bag-of-Words Representation
Introducing Bag-of-Words Representation
In the world of text analysis, transforming raw data into a format that is both computer-friendly and preserves the essential information for further processing is crucial. One of the simplest yet versatile methods to do this is the Bag-of-Words Representation, or BoW for short.
BoW is essentially a method to extract features from text. Imagine you have a big bag filled with words. These words can come from anywhere: a book, a website, or, in our case, movie reviews from the IMDB dataset. For each document or sentence, the BoW representation will contain the count of how many times each word appears. Most importantly, in this "bag," we don't care about the order of words, only their occurrence.
Consider this simple example with three sentences:
The cat sat on the mat.The cat sat near the mat.The cat played with a ball.
Using a BoW representation, our table would look like this:
| the | cat | sat | on | mat | near | played | with | a | ball | |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 |
| 2 | 2 | 1 | 1 | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
| 3 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
Each sentence (document) corresponds to a row, and each unique word is a column. The values in the cells represent the word count in the given sentence.
Illustrating Bag-of-Words with a Simple Example
We can start practising the Bag-of-Words model by using Scikit-learn CountVectorizer on the exact same three sentences:
The output of the above code will be:
From the output, you'll notice that Scikit-learn CountVectorizer has done the exact thing as our previous manual process. It's created a Bag-of-Words representation for our sentences where each row corresponds to a sentence and each column to a unique word.
