CSCE 471 Natural Language Processing · Spring 2027

Bag-of-Words Features

Kuan-Hao Huang · Course Website

Note: This page was created with the help of AI as a supplementary reading. It may contain errors or simplifications. The lecture slides are the primary reference for this course; please refer to them first.

A classifier is a mathematical function: it takes numbers in and gives a label out. Text, though, is a string of characters. Before any classifier can work on “This restaurant is the best one in College Station,” we must turn the sentence into a vector of numbers that keeps the information the classifier needs.

The simplest way to do this, and still a surprisingly strong one, is to ignore grammar and word order entirely and just record which words appear. This reading builds that representation, called bag-of-words, shows where it fails, extends it with n-grams, and then compares the different ways of filling in the vector, from simple 0/1 values to TF-IDF weights.

After reading this, you should be able to build a vocabulary and convert a text into a binary bag-of-words vector · explain the advantages and limitations of bag-of-words · list the n-grams of a sentence and build bag-of-n-grams features · compute word-count, word-frequency, and TF-IDF features · explain what IDF does and why · compare the feature variants and choose between them.

1Text needs a mathematical form

Most text classifiers have two parts: a feature (representation) step that turns text $x$ into a vector, and a classifier (model) that turns the vector into a label $y$:

$$\text{Text } x\;\longrightarrow\;\boxed{\text{Feature (Representation)}}\;\longrightarrow\;\boxed{\text{Classifier (Model)}}\;\longrightarrow\;\text{Label } y$$

This reading is only about the first box: how to turn text into a vector. The features here are fixed rules that need no training at all. You define them once, and every text gets a vector.

2The bag-of-words representation

Imagine cutting a sentence into words and dropping them into a bag. The bag remembers which words went in, but not their order. That's a bag-of-words (BoW).

To turn the bag into a vector:

  1. Build a vocabulary $V$: the list of all distinct words in the training data, usually after simple preprocessing such as lowercasing. Each word gets a fixed position (dimension).
  2. Represent a text by a vector $\mathbf x$ of length $|V|$, the vocabulary size. In the binary version, $x_j=1$ if word $j$ appears in the text and $0$ otherwise.
Vocabulary (built from the three training sentences on the buttons) and the binary BoW vector
Figure 1 (interactive). Binary bag-of-words. Each vocabulary word is one dimension; highlighted dimensions are 1. Type your own text: words not in the vocabulary, called out-of-vocabulary (OOV) words, have no dimension and are simply ignored.

Strictly speaking, a binary vector records a set of words. When we also keep counts (section 5), the representation is a true bag (a multiset), where repeated words count more than once.

Advantages

Disadvantages

3Capturing local order with n-grams

An n-gram is a sequence of $n$ consecutive words. A sentence with $L$ words has $L-n+1$ of them:

nn-grams of Bob likes Alice very much
1 (unigram){Bob, likes, Alice, very, much}
2 (bigram){Bob likes, likes Alice, Alice very, very much}
3 (trigram){Bob likes Alice, likes Alice very, Alice very much}
4-gram{Bob likes Alice very, likes Alice very much}
Table 1. Each n-gram fixes the order of a few neighboring words.

A bag-of-n-grams representation adds one dimension for each n-gram in the vocabulary, usually on top of the unigrams. The unigram part of Bob likes Alice and Alice likes Bob is still identical, but the bigram part differs (Bob likes vs. Alice likes), so the vectors are no longer the same. N-gram features capture more sentence structure, including short phrases such as not good and very much.

Features:
Features of sentence A
Features of sentence B
Figure 2 (interactive). Bag-of-n-grams for two sentences. Grey features are shared; colored features appear in only one sentence. With unigrams alone, the two default sentences are indistinguishable.

The price is size. The number of possible bigrams is $|V|^2$ and of trigrams $|V|^3$. Only the n-grams that actually occur in the training data get a dimension, and in practice we usually keep only those that occur at least a few times.

4Feature variants: what goes in each dimension?

The binary vector is only one option. The same dimensions can hold other numbers:

VariantValue of dimension $t$ for a document $d$
Binary BoW1 if $t$ appears in $d$, else 0
Word countthe number of times $t$ appears in $d$
Word frequency (TF)count of $t$ ÷ total number of words in $d$
TF-IDF$f_t\cdot\log\dfrac{N}{n_t}$
Table 2. Bag-of-words variants. For example, word counts [0, 2, 1, 0, 0, 4, …] in a 25-word document become frequencies [0, 0.08, 0.04, 0, 0, 0.16, …].

The four variants keep different amounts of information. Compare them on the same document below, and try repeating the document twice, which is what happens when a review says the same thing at twice the length.

Figure 3 (interactive). The four variants side by side for one document of the collection in Figure 4 ($N=4$, natural log). Binary BoW only records presence; counts record how often; word frequency divides by the length; TF-IDF also down-weights words that occur in many documents.

TF-IDF goes one step further. Frequent words aren't necessarily informative: the, is, and and appear in almost every document and say little about its content. TF-IDF multiplies two factors:

So TF-IDF is high for words that are frequent in this document but rare in the collection, which are exactly the words that characterize it.

Show features of:
Figure 4 (interactive). TF-IDF on a four-document collection ($N=4$, natural log). Pick a document to see each word's count, TF, document frequency $n_t$, IDF, and TF-IDF. Words like the and is that occur in every document get a TF-IDF of 0.

Comparing the variants

VariantKeeps how often a word occurs?Independent of document length?Down-weights common words?Needs the whole collection?
Binary BoWnomostlynono
Word countyesnonono
Word frequencyyes (relative)yesnono
TF-IDFyes (relative)yesyesyes, to compute $n_t$ and $N$
Table 3. What each variant captures. All of them still ignore word order; adding n-gram dimensions (section 3) works with any of the four.

No variant is best for every task. Binary features are often enough for short texts, where most words appear once anyway. Counts and frequencies matter more for longer documents. TF-IDF is the standard choice when common words would otherwise dominate, for example in document retrieval or topic classification of long articles.

Practical notes. Real systems usually lowercase text and may remove very common stop words (the, is, of) or very rare words to shrink the vocabulary. TF-IDF comes in several slightly different versions (for example, using raw counts instead of frequencies, or smoothing the IDF to avoid dividing by zero), but they share the same idea. The base of the logarithm only rescales the values.

5Check your understanding

Exercise 1: binary BoW

The vocabulary is [a, cat, dog, the, sat, on, mat]. Write the binary BoW vectors of the cat sat on the mat and a dog sat.

the cat sat on the mat: [0, 1, 0, 1, 1, 1, 1]. (the appears twice, but binary BoW records only presence.)
a dog sat: [1, 0, 1, 0, 1, 0, 0].

Exercise 2: n-grams

List the bigrams of I do not like it. Which bigram captures meaning that the unigrams miss?

{I do, do not, not like, like it}: $5-2+1=4$ bigrams. not like is the key one: it flips the meaning of like, which unigrams can't capture.

Exercise 3: TF-IDF by hand

A collection has $N=10$ documents. In a 20-word document, the word pasta appears 2 times and occurs in 2 of the 10 documents; the word the appears 4 times and occurs in all 10. Compute both TF-IDF values (natural log).

pasta: TF $=2/20=0.1$, IDF $=\ln(10/2)=\ln5\approx1.609$, TF-IDF $\approx0.161$.
the: TF $=4/20=0.2$, IDF $=\ln(10/10)=0$, TF-IDF $=0$. Even though the is twice as frequent, it carries no weight.

Quick quiz

6Further reading