liveBook · Manning

Bag-of-words model

As explained in Data Without Labels

Bag-of-words is a text representation that uses word occurrences or frequencies as features while disregarding word order and context.

The bag-of-words model represents each sentence or document as a fixed-length vector of word frequencies. It tokenizes the corpus, builds a vocabulary of unique words, and ignores grammar, word order, and context. Optional frequency thresholds can restrict the vocabulary. section 7.9

A practical construction workflow is:

  • Tokenize every document into individual words. section 7.9
  • Collect the unique tokens into a corpus vocabulary, optionally applying upper and lower frequency limits. section 7.9
  • Encode each document with one vector position per vocabulary word, using zero when a word is absent. section 7.9
  • Use the resulting vectors for fast, computationally inexpensive document classification, while recognizing that frequency alone can reduce accuracy. section 7.9 section 7.20

Two-sentence vocabulary

For “It is raining heavily” and “We should eat fruits,” treat the words in both sentences as the vocabulary. The first sentence receives its word counts and zeros for words appearing only in the second sentence. section 7.9

Figure 7.5 The frequency of each word has been calculated. In this example, we have two sentences.
figure

Inspect the token frequencies that precede vector construction. section 7.9

Figure 7.6 We are assuming that in the vocabulary only two sentences are present and the first sentence will be represented as shown.
figure

Inspect how the first sentence becomes a vocabulary-sized vector with zero values for absent words. section 7.9

How Data Without Labels covers it

How deep it goes
Explains tokenization, vocabulary creation, frequency vectors, and thresholds, but stops short of implementation code and contextual modeling.
Examples
Uses two short sentences to demonstrate token frequencies, vocabulary-sized vectors, and zero values for absent words.
What it assumes
Assumes a text corpus and basic tokenization, while defining vocabulary construction and frequency-based vectorization.
What you can do after
Apply the model to convert sentences or documents into frequency vectors for fast document classification.
How it connects
Contrasts frequency-only representation with TF-IDF, n-grams, language models, and contextual word representations.

Also explained in

Compare how 5 books explain bag-of-words model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Without Labels. The section links point to the parts of the book it draws on.