Vector Representation of Documents
A document that mentions the many times is not necessarily a document about the.
Why Raw Word Counts Fall Short
Suppose you have a collection of text documents and you want a computer to turn each document into a list of numbers. A simple approach is to count how often each word appears. That works, but it has a weakness. Common words such as the or and appear almost everywhere, so their counts tell you very little about what a particular document is about. A document that mentions the many times is not necessarily a document about the.
tf-idf is a method for scoring each word in a document so that words which are frequent in that document but rare across the collection receive higher scores. Before we look at how it works, try a quick prediction.
What do you think happens?
For the same number of repeats inside one document, which word should receive the larger tf-idf weight: a word that appears in only two training documents, or a word that appears in nearly every training document?
Reveal answer
The word that appears in only two training documents
For the same term frequency, a word that appears in fewer training documents receives a larger tf-idf score than a word that appears in many of them. The rare word is the more informative signal for distinguishing documents.
Positions in a Document Vector
A vectorized document is a list of numbers with a fixed length. Every word in the vocabulary, which is the set of words the system knows about, is assigned one fixed position in that list. The document does not choose its own positions. Each position always refers to the same word, so two documents can be compared position by position.
The numbers that fill those positions come from the document itself. For each vocabulary word, the document contributes a value for that word's position. Words the document never uses still have a position, and that position simply holds a value for zero occurrences in the raw count version.
In this generated example, the vocabulary has four words, so every document becomes a vector with four positions. The document shown contains cat twice, sat once, mat once, and never mentions dog. Its vector is therefore determined entirely by those four positions, in that fixed order. A tf-idf representation follows the same layout, but each position holds a weight instead of a plain count.
Three Quantities Behind a tf-idf Score
A tf-idf score for a word $w$ in a document $d$ is computed from three quantities. Each one answers a different question about the word and the collection it lives in. Keeping them separate in your head makes the rest of the calculation much easier to follow.
Why Common Words Get Smaller Weights
The central idea is the direction of the effect. Hold the term frequency fixed, and look at how the score changes as the number of training documents containing the word grows. The score for a word that appears in few documents is larger than the score for a word that appears in many of them. The exact formula combines N, $N_{w}$, and tf together, and the precise numbers depend on that formula. What you should carry forward is the direction: a larger $N_{w}$ relative to N pulls the score down, and a smaller $N_{w}$ relative to N keeps it higher.
Two words, the same term count
A training set has 10 documents. Two words, the and tokenizer, each appear exactly 3 times in a document d that you are encoding. The word the appears in all 10 training documents, while tokenizer appears in only 2 of them. Which word receives the larger tf-idf score?
- 1Identify the shared quantities
Both words have tf equal to 3 in document d, and both are measured against the same N equal to 10. Only $N_{w}$ differs between them.
- 2Compare the collection counts
the appears in 10 of 10 training documents, so its $N_{w}$ equals N. tokenizer appears in 2 of 10, so its $N_{w}$ is much smaller relative to N.
- 3Apply the direction of the effect
With term frequency held fixed, a larger $N_{w}$ relative to N pulls the score down. The word the is penalized for being spread across the whole collection, so tokenizer, which is concentrated in fewer documents, receives the larger score.
tokenizer receives the larger tf-idf score, even though both words occur exactly three times in the document. Frequency inside one document is not enough; a word also needs to be distinctive across the collection.
What do you think happens?
If a word appears in every training document, what should happen to its tf-idf weight compared with an otherwise identical word that appears in only one training document?
Reveal answer
The word in every training document receives the smaller weight.
For the same term frequency, the word that appears in many training documents receives a smaller tf-idf score than the word that appears in fewer documents.
Normalizing Each Document to Length One
After the tf-idf representation is computed, the classes in this source also apply L2 normalization. L2 normalization rescales the representation of each document so that it has Euclidean norm 1. In other words, every document's vector is scaled to the same overall length, regardless of how large its raw values were.
The Euclidean norm of a vector is the square root of the sum of its squared values, written $\lVert v \rVert_2$. Dividing every value in the vector by this norm produces a new vector whose norm is 1. Only the overall length changes; the proportions among the values stay the same.
Rescaling a short vector
Take a generated document vector with two positions holding the values 3 and 4. Apply L2 normalization so that its Euclidean norm becomes 1.
- 1Compute the norm
$\lVert v \rVert_2 = \sqrt{3^{2} + 4^{2}} = \sqrt{25} = 5$.
- 2Divide each value by the norm
3 divided by 5 is 0.6, and 4 divided by 5 is 0.8.
- 3Check the result
The new vector is 0.6 and 0.8, whose norm is $\sqrt{0.6^{2} + 0.8^{2}} = 1$. The proportion between the two values is still 3 to 4.
The vector (3, 4) becomes (0.6, 0.8). A document with twice as many words would have larger raw values, but after normalization it would land on the same vector if its word proportions matched.
This is why document length stops mattering. A long document naturally contains more words than a short one, which tends to inflate raw counts. The source states that rescaling in this way means the length of a document, measured as the number of words, does not change the vectorized representation. Two documents that use the same words in the same proportions end up with the same normalized vector, even if one of them is much longer.
Why a Pipeline Belongs Inside Grid Search
Grid search tries different combinations of settings and compares how well each one performs. When tf-idf is one of the steps being tuned, its calculation depends on statistics of the training data, such as N and $N_{w}$. Those statistics are only meaningful for the training data they were computed from.
A pipeline chains the preprocessing steps and the model into a single object, so that the grid search repeats the whole sequence in a consistent way. The source explains that because tf-idf makes use of these statistical properties of the training data, a pipeline is used to ensure that the results of the grid search are valid. The pipeline keeps the tf-idf step tied to the training data it was built from, rather than letting the search reuse statistics in a way that does not match the comparison it is making.
Mistakes Learners Make With tf-idf
Common mistake Thinking a high raw count always means a high tf-idf weight
Why it failsThe score also depends on $N_{w}$ and N. A word repeated often in a document can still receive a modest weight if it appears in most training documents.
Do this insteadRead tf as the document's view and $N_{w}$ relative to N as the collection's view, and combine both.
Common mistake Assuming L2 normalization removes meaning from the values
Why it failsNormalization rescales the whole vector to norm 1 but preserves the proportions among values.
Do this insteadCompare normalized vectors by their relative values, not by their absolute size.
Common mistake Fitting the vectorizer on all data before the grid search
Why it failstf-idf statistics come from the training data, so computing them outside the search can make the reported scores invalid.
Do this insteadPlace the vectorizer and the classifier in a pipeline so the search repeats the sequence consistently.
Common mistake Assuming an unused vocabulary word has no position in the vector
Trace the Representation for Yourself
A generated vocabulary has three words: alpha, beta, gamma, in that fixed order. Document A contains alpha twice and gamma once. Write its count vector, then explain what must be true of the other documents for their vectors to line up with it position by position.
Hints
- Each position always refers to the same vocabulary word, so the order of the words is fixed.
- Words that a document never uses still occupy a position, holding zero in the raw count version.
- Other documents must be encoded against the same vocabulary, in the same order.
Two documents, X and Y, contain the same words in the same proportions, but Y is three times longer. After L2 normalization, explain whether their vectorized representations will match, and why the length difference does not matter.
Hints
- Consider what the source says about document length and the vectorized representation.
- Normalization divides by the vector's norm, which scales the longer document's raw values down.
Key Takeaways
- A document becomes a fixed-length vector in which each vocabulary word owns one position, and the document's values fill those positions.
- A tf-idf score depends on tf, the count inside the document, and on N and $N_{w}$, which describe the training collection; for the same tf, a word in fewer training documents scores higher.
- L2 normalization rescales each document's vector to Euclidean norm 1, so document length no longer changes the vectorized representation.
- When tf-idf is tuned in a grid search, a pipeline keeps its training-data statistics tied to the correct data so the search results are valid.