All labs

TF-IDF Geometry: Angle, Length, and What Similar Means

Six short documents, one weighting switch: watch cosine similarity separate topic from document length.

Ch 7 7.2
Explorer
Unsupervised Learning & Text
16 mindifficulty 2/5

Controls

Term weighting
Sublinear tf

1 + log(count) instead of the count.

L2 normalize rows

Project every document onto the unit sphere.

Drop stop words

Remove a 20-word English stop list.

Inspect document
The corpus
  • D1 · clinical · 22 tokens
  • D2 · clinical · 20 tokens
  • D3 · sports · 19 tokens
  • D4 · sports · 18 tokens
  • D5 · sports injury · 19 tokens
  • D6 · long clinical · 45 tokens

Cosine similarity

Angle between documents. Darker means more similar.

docD1D2D3D4D5D6
D1 clinical10.870.800.780.870.93
D2 clinical0.8710.770.810.900.86
D3 sports0.800.7710.930.900.78
D4 sports0.780.810.9310.870.80
D5 sports injury0.870.900.900.8710.84
D6 long clinical0.930.860.780.800.841

Euclidean distance

Length-sensitive: the same topic at twice the length looks far away.

D104.25.25.54.47.9
D24.2054.53.39.7
D35.2502.63.210.8
D45.54.52.603.610.7
D54.43.33.23.6010.2
D67.99.710.810.710.20

Top terms in D1

Weight under the current scheme, relative to the largest.

00.200.400.600.8010510[0.10, 0.90) n=6[1.10, 1.90) n=3[2.10, 2.90) n=6[3.10, 3.90) n=5[4.10, 4.90) n=6[5.10, 5.90) n=4[6.10, 6.90) n=4[7.10, 7.90) n=4[8.10, 8.90) n=2[9.10, 9.90) n=2relative weight
  1. 1. the df=6
  2. 2. patient df=3
  3. 3. and df=6
  4. 4. in df=5
  5. 5. is df=6
  6. 6. clinic df=4
  7. 7. doctor df=4
  8. 8. knee df=4
  9. 9. pain df=2
  10. 10. reports df=2
cos(D1, D6)
0.935
same topic, D6 nearly three times longer
‖D1 − D6‖
7.94
Euclidean, same pair
cos(D1, D3)
0.804
clinical versus sports
Signal margin
0.131
same-topic minus cross-topic cosine
Vocabulary size
33
idf of “the”
1
1.0 when a term is in every document
Why the margin is the number to watch

A retrieval system is only useful if same-topic pairs score higher than cross-topic pairs. Turn on TF-IDF and read the signal margin: down-weighting terms that appear everywhere widens the gap without touching the documents.