A bag-of-words model discards word order, represents a review as an unordered set of tokens, and learns a weight for each token. Token presence can then serve as input for text classification. section 14.3 section 14.4
From a review to a feature vector
For “this movie made me cry,” tokenization produces token IDs such as [0, 1, 3, 0, 5]. Removing order gives {0, 1, 3, 5}; multi-hot encoding turns that set into [1, 1, 0, 1, 0, 1]. The resulting fixed-size vector records which vocabulary items are present, such as “cry,” but not their positions. section 14.4
Use the representation as a simple baseline for text classification:
- Assign learned weights to words whose presence suggests a class, such as “terrible” for negative sentiment or “riveting” for positive sentiment. section 14.4
- Prefer sequence models when meaning depends on word order; recurrent models and Transformers preserve or inject positional information. section 14.3
"this movie made me cry"
c("[UNK]" = 0, "movie" = 1, "film" = 2, "made" = 3, "laugh" = 4, "cry" = 5)
Inspect the toy sentence and vocabulary used to build the bag-of-words representation. section 14.4
c(1, 1, 0, 1, 0, 1)
Inspect the final multi-hot vector that records token presence and absence. section 14.4




