Natural Language Processing · TEE · formulas
TF–IDF from the exact slide convention
Representation & similarity
Formulas explained through full worked examples
TF–IDF weights a term highly when it is frequent in a particular document but uncommon across the corpus. The supplied Unit 2 slides use normalized term frequency and base-10 inverse document frequency:
\operatorname{TF}(t,d)=\frac{C(t,d)}{N_d}\operatorname{IDF}(t)=\log_{10}\frac{D}{\operatorname{df}(t)}\operatorname{TFIDF}(t,d)=\operatorname{TF}(t,d)\operatorname{IDF}(t).LaTeX:
N_d is the document's token count after the specified preprocessing; LaTeX:
D is number of documents; LaTeX:
\operatorname{df}(t) is the number of documents containing the term at least once. A word repeated ten times in one document contributes one to document frequency.
Why the log? It compresses the ratio's scale. If a term occurs in all documents, LaTeX:
D/df=1, so IDF=0 under this unsmoothed convention. Other libraries use smoothed IDF, different TF or normalization. Use the question's formula; do not silently substitute a library default.
Question. A corpus contains 10 documents. The word model appears in 2 documents and occurs 3 times in a 12-token document. Compute TF, IDF and TF–IDF using the slide convention.
Given
D=10, df(model)=2, C(model,d)=3, Nd=12; log base 10.
To find
The TF–IDF weight.
Formula and why it applies
LaTeX:
TF=C/N_d, LaTeX:
IDF=\log_{10}(D/df), then multiply.
Step 1. TF
LaTeX:
3/12=0.25.
Step 2. Document ratio
LaTeX:
10/2=5.
Step 3. IDF
LaTeX:
\log_{10}5\approx0.698970.
Step 4. Weight
LaTeX:
0.25\times0.698970\approx0.174743.
Answer. TF=0.25, IDF≈0.698970, TF–IDF≈0.174743.
Question. A term occurs 4 times in a 20-token document and is present in all 8 corpus documents. Calculate its TF–IDF.
Given
C=4, Nd=20, D=8, df=8.
To find
Its weight under unsmoothed base-10 IDF.
Formula and why it applies
LaTeX:
TF=C/N_d, LaTeX:
IDF=\log_{10}(D/df).
Step 1. TF
LaTeX:
4/20=0.2.
Step 2. IDF
LaTeX:
\log_{10}(8/8)=\log_{10}1=0.
Step 3. Multiply
LaTeX:
0.2\times0=0.
Answer. Weight 0: this term does not distinguish documents under this convention.
Question. Find TF–IDF vectors for D1=“cat cat dog”, D2=“dog fish”, D3=“cat fish fish”, using [cat,dog,fish], normalized TF and log10 IDF.
Given
No further preprocessing. Each term occurs in exactly two of the three documents.
To find
All three vectors.
Formula and why it applies
First count df by documents, then compute each document's TF, then multiply by its term's IDF.
Step 1. Common IDF
LaTeX:
D=3, LaTeX:
df(cat)=df(dog)=df(fish)=2. Each IDF LaTeX:
=\log_{10}(3/2)\approx0.176091.
Step 2. D1 term frequencies
Length 3 → LaTeX:
[2/3,1/3,0].
Step 3. D1 weights
LaTeX:
[(2/3)0.176091,(1/3)0.176091,0]\approx[0.117394,0.058697,0].
Step 4. D2 weights
Length 2; TF LaTeX:
[0,1/2,1/2]. Vector LaTeX:
\approx[0,0.088046,0.088046].
Step 5. D3 weights
Length 3; TF LaTeX:
[1/3,0,2/3]. Vector LaTeX:
\approx[0.058697,0,0.117394].
Answer. D1≈[0.117394,0.058697,0]; D2≈[0,0.088046,0.088046]; D3≈[0.058697,0,0.117394].
TF: iss document mein kitna. IDF: poore corpus mein kitna rare. Dono multiply.