Last Updated on August 7, 2019
The bag-of-words model is a way of representing text data when modeling text with machine learning algorithms.
The bag-of-words model is simple to understand and implement and has seen great success in problems such as language modeling and document classification.
In this tutorial, you will discover the bag-of-words model for feature extraction in natural language processing.
After completing this tutorial, you will know:
- What the bag-of-words model is and why it is needed to represent text.
- How to develop a bag-of-words model for a collection of documents.
- How to use different techniques to prepare a vocabulary and score words.
Kick-start your project with my new book Deep Learning for Natural Language Processing, including step-by-step tutorials and the Python source code files for all examples.
Let’s get started.
This tutorial is divided into 6 parts; they are:
- The Problem with Text
- What is a Bag-of-Words?
- Example of the Bag-of-Words Model
- Managing Vocabulary
- Scoring Words
- Limitations of Bag-of-Words
Need help with Deep Learning for Text Data?
Take my free 7-day email crash course now (with code).
Click to sign-up and also get a free PDF Ebook version of the course.
The Problem with Text
A problem with modeling text is that it is messy, and techniques like machine learning algorithms prefer well defined fixed-length inputs and outputs.
Machine learning algorithms cannot work with raw text directly; the text must be converted into numbers. Specifically, vectors of numbers.
In language processing, the vectors x are derived from textual data, in order to reflect various linguistic properties of the text.
— Page 65, Neural Network Methods in Natural Language Processing, 2017.
This is called feature extraction or feature encoding.
A popular and simple method of feature extraction with text data is called the bag-of-words model of text.
What is a Bag-of-Words?
A bag-of-words model, or BoW for short, is a way of extracting features from text for use in modeling, such as with machine learning algorithms.
The approach is very simple and flexible, and can be used in a myriad of ways for extracting features from documents.
A bag-of-words is a representation of text that describes the occurrence of words within a document. It involves two things:
- A vocabulary of known words.
- A measure of the presence of known words.
It is called a “bag” of words, because any information about the order or structure of words in the document is discarded. The model is only concerned with whether known words occur in the document, not where in the document.
A very common feature extraction procedures for sentences and documents is the bag-of-words approach (BOW). In this approach, we look at the histogram of the words within the text, i.e. considering each word count as a feature.
— Page 69, Neural Network Methods in Natural Language Processing, 2017.
The intuition is that documents are similar if they have similar content. Further, that from the content alone we can learn something about the meaning of the document.
The bag-of-words can be as simple or complex as you like. The complexity comes both in deciding how to design the vocabulary of known words (or tokens) and how to score the presence of known words.
We will take a closer look at both of these concerns.
Example of the Bag-of-Words Model
Let’s make the bag-of-words model concrete with a worked example.
Step 1: Collect Data
Below is a snippet of the first few lines of text from the book “A Tale of Two Cities” by Charles Dickens, taken from Project Gutenberg.
It was the best of times,
it was the worst of times,
it was the age of wisdom,
it was the age of foolishness,
For this small example, let’s treat each line as a separate “document” and the 4 lines as our entire corpus of documents.
Step 2: Design the Vocabulary
Now we can make a list of all of the words in our model vocabulary.
The unique words here (ignoring case and punctuation) are:
That is a vocabulary of 10 words from a corpus containing 24 words.
Step 3: Create Document Vectors
The next step is to score the words in each document.
The objective is to turn each document of free text into a vector that we can use as input or output for a machine learning model.
Because we know the vocabulary has 10 words, we can use a fixed-length document representation of 10, with one position in the vector to score each word.
The simplest scoring method is to mark the presence of words as a boolean value, 0 for absent, 1 for present.
Using the arbitrary ordering of words listed above in our vocabulary, we can step through the first document (“It was the best of times“) and convert it into a binary vector.
The scoring of the document would look as follows:
- “it” = 1
- “was” = 1
- “the” = 1
- “best” = 1
- “of” = 1
- “times” = 1
- “worst” = 0
- “age” = 0
- “wisdom” = 0
- “foolishness” = 0
As a binary vector, this would look as follows:
[1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
The other three documents would look as follows:
"it was the worst of times" = [1, 1, 1, 0, 1, 1, 1, 0, 0, 0]
"it was the age of wisdom" = [1, 1, 1, 0, 1, 0, 0, 1, 1, 0]
"it was the age of foolishness" = [1, 1, 1, 0, 1, 0, 0, 1, 0, 1]
All ordering of the words is nominally discarded and we have a consistent way of extracting features from any document in our corpus, ready for use in modeling.
New documents that overlap with the vocabulary of known words, but may contain words outside of the vocabulary, can still be encoded, where only the occurrence of known words are scored and unknown words are ignored.
You can see how this might naturally scale to large vocabularies and larger documents.
As the vocabulary size increases, so does the vector representation of documents.
In the previous example, the length of the document vector is equal to the number of known words.
You can imagine that for a very large corpus, such as thousands of books, that the length of the vector might be thousands or millions of positions. Further, each document may contain very few of the known words in the vocabulary.
This results in a vector with lots of zero scores, called a sparse vector or sparse representation.
Sparse vectors require more memory and computational resources when modeling and the vast number of positions or dimensions can make the modeling process very challenging for traditional algorithms.
As such, there is pressure to decrease the size of the vocabulary when using a bag-of-words model.
There are simple text cleaning techniques that can be used as a first step, such as:
- Ignoring case
- Ignoring punctuation
- Ignoring frequent words that don’t contain much information, called stop words, like “a,” “of,” etc.
- Fixing misspelled words.
- Reducing words to their stem (e.g. “play” from “playing”) using stemming algorithms.
A more sophisticated approach is to create a vocabulary of grouped words. This both changes the scope of the vocabulary and allows the bag-of-words to capture a little bit more meaning from the document.
In this approach, each word or token is called a “gram”. Creating a vocabulary of two-word pairs is, in turn, called a bigram model. Again, only the bigrams that appear in the corpus are modeled, not all possible bigrams.
An N-gram is an N-token sequence of words: a 2-gram (more commonly called a bigram) is a two-word sequence of words like “please turn”, “turn your”, or “your homework”, and a 3-gram (more commonly called a trigram) is a three-word sequence of words like “please turn your”, or “turn your homework”.
— Page 85, Speech and Language Processing, 2009.
For example, the bigrams in the first line of text in the previous section: “It was the best of times” are as follows:
- “it was”
- “was the”
- “the best”
- “best of”
- “of times”
A vocabulary then tracks triplets of words is called a trigram model and the general approach is called the n-gram model, where n refers to the number of grouped words.
Often a simple bigram approach is better than a 1-gram bag-of-words model for tasks like documentation classification.
a bag-of-bigrams representation is much more powerful than bag-of-words, and in many cases proves very hard to beat.
— Page 75, Neural Network Methods in Natural Language Processing, 2017.
Once a vocabulary has been chosen, the occurrence of words in example documents needs to be scored.
In the worked example, we have already seen one very simple approach to scoring: a binary scoring of the presence or absence of words.
Some additional simple scoring methods include:
- Counts. Count the number of times each word appears in a document.
- Frequencies. Calculate the frequency that each word appears in a document out of all the words in the document.
You may remember from computer science that a hash function is a bit of math that maps data to a fixed size set of numbers.
For example, we use them in hash tables when programming where perhaps names are converted to numbers for fast lookup.
We can use a hash representation of known words in our vocabulary. This addresses the problem of having a very large vocabulary for a large text corpus because we can choose the size of the hash space, which is in turn the size of the vector representation of the document.
Words are hashed deterministically to the same integer index in the target hash space. A binary score or count can then be used to score the word.
This is called the “hash trick” or “feature hashing“.
The challenge is to choose a hash space to accommodate the chosen vocabulary size to minimize the probability of collisions and trade-off sparsity.
A problem with scoring word frequency is that highly frequent words start to dominate in the document (e.g. larger score), but may not contain as much “informational content” to the model as rarer but perhaps domain specific words.
One approach is to rescale the frequency of words by how often they appear in all documents, so that the scores for frequent words like “the” that are also frequent across all documents are penalized.
This approach to scoring is called Term Frequency – Inverse Document Frequency, or TF-IDF for short, where:
- Term Frequency: is a scoring of the frequency of the word in the current document.
- Inverse Document Frequency: is a scoring of how rare the word is across documents.
The scores are a weighting where not all words are equally as important or interesting.
The scores have the effect of highlighting words that are distinct (contain useful information) in a given document.
Thus the idf of a rare term is high, whereas the idf of a frequent term is likely to be low.
— Page 118, An Introduction to Information Retrieval, 2008.
Limitations of Bag-of-Words
The bag-of-words model is very simple to understand and implement and offers a lot of flexibility for customization on your specific text data.
It has been used with great success on prediction problems like language modeling and documentation classification.
Nevertheless, it suffers from some shortcomings, such as:
- Vocabulary: The vocabulary requires careful design, most specifically in order to manage the size, which impacts the sparsity of the document representations.
- Sparsity: Sparse representations are harder to model both for computational reasons (space and time complexity) and also for information reasons, where the challenge is for the models to harness so little information in such a large representational space.
- Meaning: Discarding word order ignores the context, and in turn meaning of words in the document (semantics). Context and meaning can offer a lot to the model, that if modeled could tell the difference between the same words differently arranged (“this is interesting” vs “is this interesting”), synonyms (“old bike” vs “used bike”), and much more.
This section provides more resources on the topic if you are looking go deeper.
- Bag-of-words model on Wikipedia
- N-gram on Wikipedia
- Feature hashing on Wikipedia
- tf–idf on Wikipedia
- Chapter 6, Neural Network Methods in Natural Language Processing, 2017.
- Chapter 4, Speech and Language Processing, 2009.
- Chapter 6, An Introduction to Information Retrieval, 2008.
- Chapter 6, Foundations of Statistical Natural Language Processing, 1999.
In this tutorial, you discovered the bag-of-words model for feature extraction with text data.
Specifically, you learned:
- What the bag-of-words model is and why we need it.
- How to work through the application of a bag-of-words model to a collection of documents.
- What techniques can be used for preparing a vocabulary and scoring words.
Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.
Great article, thanks for keeping it concise and still easy to understand and read.
great read and good references.
Thank you for your clear explanation.
It is really a gentle intro.
I hope it helped.
Very helpful and clear step by step explanation.
Great article! So, since using Bag-of-Words does not take into account the relation between words or word order. Does transferring the Bag-of-Words model into CNN could tackle the problem and increase the prediction accuracy? I’ve been searching for the article of implementing BOW + CNN for text classification but no luck so far.
No. But you could use a word embedding and an LSTM that would learn the relationship between words.
if you have written any articles or any others explaining word embedding or LSTM which understands relationships between words..
please share the links it would be helpful for me.
Yes, I have many, you can search on the blog or start here:
If I understood it correctly, the purpose of word hashing is to easily map the value to the word and get to easily update the count. My question is, would it be easier if I just use a dictionary instead of implementing word hashing?
A dictionary of what?
Note that in Python, a dictionary IS an implementation of a hash table. You’ll still need to decide what the words map to, though, and I think the idea with word hashing is that each words maps to its own hashed value. It might be helpful to have a dictionary mapping each word to its own hashed value, if lookups are quicker than your hash function and memory is not a limitation, but you can’t really *replace* a hash function with a dictionary.
This would be a set.
Thank you for article
i dont actually understand what bag of words is after reading
1) The binary vector is the ready BOW model output?
[1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
2) How would it look like if we have more than one occurrence of a word?
[1, 1, 2, 1, 1, 1, 0, 0, 0, 0] (not a strict example but suppose “the” was twice in the doc)
3) I dont understand why we cant put bag of words into rnn models? Into lstm for example
The representation of a document as a vector of word frequencies is the BoW model.
You can choose how to count, either exists/not-exists, or a count, or something else.
We can plug words into RNNs, often we use a word embedding on the front end to get a more distributed representation of the words:
Does that help Georgy?
Hello! Thank you for your illustration. We are doing a project of music genre classification based on the song lyrics. However, due to the license issue we only obtained the lyrics in a bag-of-words format and couldn’t access the full lyrics. We are trying to use TFIDF, in combination of bag-of-words model. However, in our case we couldn’t get document vectors since we don’t have information of complete sentences. Do we need to get the full lyric texts to do the training? Or is it sufficient to implement the model with the data we have right now? Thank you very much!
See how far you can get with BoW. To use the embedding/LSTM you will need the original docs.
Say I have 10 documents. After removing stop words and stemming etc. I have 50 word vocabulary. My each document would be a vector of 50 tf-idf values which I will model using the dependent variable. That means my modeling data has 10rows*50 features + 1 dependent column..And each cell holds the tf-idf of that vocabulary word. Is this right approach?
Also, tf-if is a value is a function the term and document and all the documents., Since tf comes from what is the term and what is it’s frequency in a given document…And idf comes from what is that term’s frequency in the overall set of all documents.
Is this understanding right?
Or is tf-idf …After being computed….is summarized at a term level or a document level??
Yes, it is terms described statistically within and across documents in the corpus.
Dear Jason I like your Article too much, Say for example i have many classes and each class may contain 2 or 3 comments. Each class comments is different say class 1 has 5 comments and class 2 has 3 comments etc . how i will considered the class as documents and how to convert to CLASS-CLASS metrix
Sounds like a straight multi-class classification problem. You can create a confusion matrix from predictions directly.
Perhaps this will help:
Hi, DR. Jason,
I have two questions, I am seeking for help:
1. I saw something called Term Document Matrix (TDM) in R is it the same thing as Bag-of-Words in Python?
2. I read from one of your posts about Bag-of-Words result in a sparse vector I would like to know if after having the sparse vector is necessary to convert them in a dense vector before using whit machine learning algorithms.
I don’t know about TDM sorry.
No need to convert to dense.
Hi. I just want to ask if I can use the Bag Of Words Model in filtering word. For example, I got the tweets from Twitter, then I need to filter those tweets which I will consider as relevant data. And those filtered data will be used for classification.
I need your help about this. Thank you in advance.
Sorry, I don’t have an example of text filtering, I cannot give you good advice.
Hey Dr. Jason,
thank you so much.
It is really a gentle and great introduction.
You have mentioned this:
This results in a vector with lots of zero scores, called a sparse vector or sparse representation.
But in Google’s ML Crash Course they have mentioned this:
* A dense representation of this sentence must set an integer for all one million cells, placing a 0 in most of them, and a low integer into a few of them.
* A sparse representation of this sentence stores only those cells symbolizing a word actually in the sentence. So, if the sentence contained only 20 unique words, then the sparse representation for the sentence would store an integer in only 20 cells.
Sure. It is saying we don’t save the zeros when using a sparse coding method.
You can learn more here:
Hi Jason, excellent article. I’m trying to categorize Tweets based on topics. Ex. tweets with amazon get placed into one cluster, and tweets with netflix get put into another cluster. Say there are 3 topics, A, B, C. My incoming stream of tweets is ABABAACCABA etc. I just need to cluster these into their respective groups. I’m using Spark Streaming, and the StreamingKMeans model to do this.
How can I vectorize tweets such that those vectors when predicted on by the K-Means model, get placed in the same cluster
Sorry, I don’t have examples of working with streaming models.
How can you model a system where you have a collection of documents mapped to some labels, and some unlabelled examples.
D1 —- c1
D2 —- c2
D3 —- c3
Dk —– c1
Two questions here:-
Q1. The lables I might see in the future might be different from what I have at trainig time
and even the corpus might change to some extent.So I have to apply semi-supervised
or unsupervised learning to learn online(on the fly) and then do better in later predictions
for the seen label, classifying into appropriate class(label).
Q2. If i see a label which I have already seen lets say(c1) and I come across similiar feature
vector, I just classify it as 1 and if I see label lets say ck, I predict it as 0 if ck was never
seen before but should have the ability to learn this and later on predict 1 for ck as well.
Basically classifying into bi-class classification as some ticket having a parent ticket
(predicting 1) and not having any parent ticket(0).Text documents here are ticket
I am struggling to devise an architecture for the problem itself, it would be really helpful if you could guide me regarding this.
Sorry, I don’t have examples of semi-supervised learning.
Hi, I followed the tutorial and Now I have a model which I trained using Bag of Word,
What I did was converted my text into Sparse Matrix and trained the model. It is giving 95 percent accuracy but now I am unable to predict a simple statement using the model.
This is my code –
I have a data frame with 2 classes labels and body.
# using bag of word model for the same
cout_vect = CountVectorizer()
# Convert from object to unicode
final_count = cout_vect.fit_transform(df[‘body’].values.astype(‘U’))
# Using a classifier for the bag of word representation
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.utils import np_utils
X_train, X_test, y_train, y_test = train_test_split(final_count, df[‘label’], test_size = .3, random_state=25)
model = Sequential()
model.add(Dense(264, input_dim=X_train.shape, activation=’relu’))
# Compile model
model.compile(loss=’categorical_crossentropy’, optimizer=’adam’, metrics=[‘accuracy’])
y_train = np_utils.to_categorical(y_train, num_classes=3)
y_test = np_utils.to_categorical(y_test, num_classes=3)
model.fit(X_train, y_train, epochs=50, batch_size=32)
model.evaluate(x=X_test, y=y_test, batch_size=None, verbose=1, sample_weight=None)
Now I want to predict this statement using my model. How to do this
x = “Your account balance has been deducted for 4300”
model.predict(x, batch_size=None, verbose=0, steps=None)
To make a prediction you must prepare the input in the same way as you did the training data.
I followed this article.I want to ask how can we extract some difficult words(terminologies) from l
different document and store it in vector to make it as the vocabulary for the machine.Will BoW be better solution or should i look for something else.
Not sure I follow.
Bag of words and word2vec are two popular representations for text data in machine learning.
Really fantastic article. Excellent clarity. Thanks Jason!
Thanks Mike, glad it helped.
Thanks for this informative article. I wonder
What is the difference between BOW and TF?
Are these same things?
BOW and TF?
Bag of words and term frequency?
Same generally, although the vector can be filled with counts, binary, proportions, etc.
Hey, thanks for the article, Jason. Very informative and concise.
Thanks, I’m glad it helped.
thanks for the article, Jason.
i have a question.
if I want to do a classification task with TFIDF vector representation, should that technique representation be carried out on all datasets (training data + test data) first, or done separately, on the training data first then then do the test data?
Prepare the vocab and encoding on the training dataset, then apply to train and test.
I’m thinking of writing a thesis about using text from a social media platform(twitter or facebook) to measure the social influence of people(maybe just influencers) on purchase behavior of software licences on mobile apps. Do you think the the Bag-of-Words Model is a good fit, or would you suggest other text analysis models?
If you have any recommendations please!
Thanks in advance,
I recommend testing a suite of representations and models in order to see what works best for your specific prediction problem.
I am a reader from China, and you are a minor celebrity due to your concise and helpful explanation on those machine learning topics. Thanks for your works.
One of my personal question is how long did it take for you to compose of this piece of article?
I try to write one tutorial per day. Usually, I can write a tutorial in a few hours.
Really great article! Thanks for sharing!
Thanks, I’m glad it helped!
First of all, thank you for the material.
Under the session Books, I think both “Chapter 6, An Introduction to Information Retrieval, 2008.”
and “Chapter 6, Foundations of Statistical Natural Language Processing, 1999.” are pointing to the latter book, so no reference for the first one.
Best regards, Elisio
Nice article about BOW explained well
NIce article. FYI, the hyperlink on Bag-of-words model on Wikipedia leads to
Thanks Alex, fixed!
Thanks for this article!!
If i have to predict the ‘impact areas’ of a issue/story, with the below features(textual data):
1. ‘Files modified’ as part of the issue.
2. ‘Component’ that the issue belongs to.
3. Related ‘Test cases’.
Which method can be used here to process this data to feed into a Machine learning model?
[1. Data Pre-processing is done.
2. Supervised test data available]
Can you please suggest something here? It will be a great help!!
I don’t know, sounds like an interest project.
Perhaps try a few techniques and also see what techniques are being used in the literature for similar problems?
great article. In this web (https://unipython.com/el-modelo-de-la-bolsa-de-palabras-o-bag-of-words/) they have plagiarized it, translating it into Spanish word by word.
Thanks for letting me know.
Hi Jason, thanks for your clear explanation. Would like to know how do I cite your article?
Would you mind to share any reference to your publication/article so that I can cite your research on this topic.
Sure, this shows you how to cite a post:
Thanks for great explanation, Jason.
You’re welcome, I’m glad it helped.
What about cosine similarity?
Sorry, I don’t have a post on that topic. Perhaps in the future.
can you explain Fuzzy bag-of-word cluster (BoWC) with algorithms?
Thanks for the suggestion.
Excellent article, introduces fundamental concepts in a direct and straight forward approach. Thanks Jason, looking forward for more related articles.
In practice, a document isn’t encoded as the ‘raw’ BoW, like ‘[1, 1, 1, 1, 1, 1, 0, 0, 0, 0]’ in the example, but as a one-hot encoding scheme. So a sentence will be encoded as a matrix (number of words * size of vocabulary), not a vector. Is that right?
Not in this case.
Think of a bag of words as a one hot encoding for a document (or paragraph).
I had an experience like reading the article in my mother tongue, though I am an Indian.
Superb & Nice explanation.
Hi Jason, thanks for the clear and coincide tutorial!
Would you consider BoW a “word embedding” method? If not, what exactly is the difference?
It is my understanding that the BoW method is part of the so-called “vector semantic” and as such it is a form of embedding (i.e. representing) the meaning of a word in a vector. However, I frequently hear people contrasting “BoW” with “word embedding” approaches (and they refer to CBOW or skip-gram for example). This makes me wonder if it is correct defining BoW as a word embedding method.
Thank you in advance for your answer!
Maybe. Not really, there is no relationship between words reflected in the representation. It is distributed though.
The main disadvantage is that it does not take account of word order, so it looses important aspects of meaning ,It can’t take account of similarity between different words (word embeddings is a solution to this), It is a large representation that includes a lot of features (one for every word), most of which will be zero for a given text..
great explanation and article
i just have one question
if i am using bag of words in sentiment analysis to predict the polarity of any tweet positive or negative and any machine learning classifier used with bag of words for training i want to know how the classifier know the total tweet positive or negative
according to what the classifier know this tweet positive or negative
every word in the tweet have one vector from the bag of word model so how the classifier link between all the vectors of all words in the same tweet to specify the polarity of that tweet
How does the model know it is positive or negative – because you trained it using historical data.
Perhaps I don’t understand your question, if so, perhaps you can restate it.
thanks for reply
i mean how the machine learning classifier identify the polarity of tweet with only bag of word model
we dont use any rules or lexicon to extract sentiment words from the tweet then apply any rule on this sentiment (aggregation or any other rule) to say that all this tweet is positive or negative
we only have all the words and its count how this work
It learns the relationship between words and the target class label. It solves the problem because we cannot code the solution explicitly.
thanks for reply all the time
any resources to understand the details deep
Yes see the resources listed in the “Further Reading” section.
thank you or help
Awesome article Jason ! This explained what I needed to know. Thanks !
Thanks for your simplicity to deliver the information.
This is one of the best articles I’ve ever read in this field. Great Work Jason! will follow this website frequently to clear my doubts.
Thank you, I’m happy to hear that.
Best explanation ever seen.
Surfed through many sites, but not satisfied.
Thanks to Mr.Jason for crisp,clear and precise explanation.
Could you please mention steps for identify negative or positive movie reviews without any python libry, I followed your tutorial on this but it only have data preparation. Im expecting next steps what i do next. I can search the internet if i know each steps.
Thank you for your great tutorials.
Sorry, I don’t have a tutorial on coding BoW models from scratch.
If you decide to use a library see this:
Thanks a lot. Very good article, Clear BOW very well.
thank for your sharing this iedea i went to know how to convert categorical data to numerical data using one hot encoding i went to know this please help me
Here is an example:
can we consider the bag of words 3 types 1-a binary scoring exist/not exist 2- count 3- frequencies and if this is true can we consider tfidf also part of bag of words or we cannot say that tfidf is bag of words
Yes, TF/IDF is like an advanced bag of words. The numbers have more meaning.
thank you for reply and help all the time my question is if i want to say
To convert our cleaned review to numerical feature vectors we can do use the following methods :
1- bag of words
3-word2vec or glove
this is correct or i must say the methods is
1- bag of words
2-word2vec or glove
and in this case i consider tfidf inside the bag of words
thanks in advance
Yes, that is a good start.
you mean this is correct if i say it
1- bag of words
3-word2vec or glove
Yes, you can use those methods to encode your words ready for modeling.
thank you for help all the time and your nice and clear way in explanation all the time
Hi Jason, thanks for this great article!
I would like to do classification on tweeter messages, such as what they are talking about, e.g., pets, family, etc. What machine learning model would you recommend to use on a TF-IDF matrix considering its sparsity? I am considering logistic regression, but due to the high-dimension problem, penalisation is a must for it. RF might also be an option?
I recommend testing a suite of data preparations and models in order to discover what works best for your specific dataset.
Thank you for the article, it is really helpful.
You are very welcome SanjanaJain! Please let us know if you have any questions regarding our material.
Thanks for your article!
When using hashing trick for Bag of Words, what is the input to the model? As per my understanding, it should be the hash of the tokens present in the document. How can I encode the count or frequency information in the hash input of the token?
For example, if the input sentence was “quick brown fox was quick”. If we are using hash based Bag of Words, the input to the model will be the hash of the four words – ‘quick’, ‘brown’, ‘fox’, ‘was’. However, we are not able to input the detail that the count of word quick is 2 for this text input.
This is great but suppose I want to convey something meaningful directly, like a plot of some kind, from the score. Say, I want to compare the similarity of two texts. Is there a simple way to do that?
Hi Tony…You could plot MSE accuracy.
Really good article, I found this very intuitive to understand and well structured. Thank you!
Could you also cover Tf-Idf.
Thank you for the feedback and support Bisrat! We appreciate the recommendations!
Is BOW, TFIDF belong to Word2vec? Both ideas (TFIDF,Word2vec) are separated thing.