liveBook · Manning

Bag-of-words model

As explained in Machine Learning System Design

A bag-of-words representation describes text using counts or indicators of its words while disregarding word order.

A bag-of-words model turns text into machine-learning features by recording which words appear or how often they appear, while ignoring their order. It offers a straightforward starting point for sequential data, but its simplicity removes information that may matter to the prediction task. section 11.1.2

Use the representation with these tradeoffs in mind:

  • Choose it when a simple text representation is sufficient for the downstream machine-learning algorithm. section 11.1.2
  • Expect word-order information to disappear; longer N-grams can partially recover local sequence information compared with single-word unigrams. section 11.1.2
  • Compare it with TF-IDF, BM25, or pretrained language-model representations such as BERT when the task needs a different text encoding. section 11.1.2

How Machine Learning System Design covers it

How deep it goes
Defines the representation and its word-order limitation, then points to longer N-grams and alternatives rather than implementation details.
Examples
Uses text as the input sequence and contrasts single-word unigrams with longer N-grams.
What you can do after
Apply it to transform text into machine-learning features while recognizing the loss of word-order information.
How it connects
Connects text features with TF-IDF, BM25, N-grams, BERT, and feature engineering.

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 Machine Learning System Design. The section links point to the parts of the book it draws on.