liveBook · Manning

Bag-of-words model

As explained in Natural Language Processing in Action, Second Edition

Bag-of-words is a text representation that records word occurrences while disregarding the order and syntactic relationships of the words.

A bag-of-words (BOW) model turns a sentence or document into a record of which words occur, or how often they occur. Imagine sorting tokens into labeled bins and counting each stack: the counts survive, but the original sequence does not. This gives text a numerical representation that can support document retrieval and other NLP tasks. section 1.5.3 section 3.1

To make documents comparable:

  • Tokenize each document and establish a vocabulary. Keep the vocabulary’s order consistent across documents so each vector position refers to the same token. section 2.5.2 section 3.1
  • Choose binary values for presence or absence, or counts for occurrences. Dividing counts by the total number of tokens gives word frequencies. section 3.1
  • Inspect the tokens before interpreting their counts. Punctuation and common words can dominate an unfiltered bag. section 3.1

Count a sentence

Using spaCy’s `en_core_web_sm` model, tokenize “It has also arisen in criminal justice, healthcare, and hiring, compounding existing racial, economic, and gender biases”. Then `Counter(token.text for token in nlp(sentence))` gives five commas and two occurrences of `and`. The 23 tokens yield 18 distinct counted tokens; punctuation’s frequency is 5/23. These are properties of the tokens, not a judgment about which words matter. section 3.1

>>> len(counts)    #1
18
>>> counts.sum()
23
>>> len(tokens)         #2
23
>>> counts / counts.sum()    #3
,              0.217391
and            0.086957
It             0.043478
has            0.043478
also           0.043478
...

Inspect the count totals and the resulting frequencies, including punctuation, before using the vector downstream. section 3.1

Losing order is an acceptable approximation for some greetings and broad document topics, but it can erase the logic of a complex request. Rearrangements of “Good morning Rosa!” still suggest a greeting; rearranging a query that joins search conditions can change what it asks. For order-sensitive information, bags of n-grams retain short word sequences. TF–IDF takes a different next step: it weights word counts to better reflect their importance across documents. Neither turns a raw count into a reliable understanding of intent. section 1.7 section 3.0

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Works through tokenization, binary vectors, counts, and normalized frequencies, while deferring richer weighting and word relationships to other models.
Examples
Counts tokens in a sentence about algorithmic bias and contrasts greeting permutations with an order-sensitive search query.
What it assumes
Assumes basic Python familiarity while demonstrating tokenization and counting with spaCy and Counter.
What you can do after
Build it from scratch with token counts, inspect frequencies, and recognize when discarded word order makes the representation unsuitable.
How it connects
Connects tokenization and vocabulary to vector space models, bags of n-grams, and TF–IDF.

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 Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.