Term frequency × inverse document frequency: the knobs of the vectorizer, and where every sparse vector breaks
\(n\) documents, \(\text{df}(t)\) of them contain the word \(t\), \(\text{tf}(t, d)\) is its count in document \(d\); then each row is divided by its length.
| knob | default | the other choice |
|---|---|---|
smooth_idf | True: the "+1" inside the fraction | False: \(\ln(n/\text{df}) + 1\) |
sublinear_tf | False: tf is the raw count | True: \(1 + \ln(\text{tf})\) |
norm | 'l2': each row of length 1 | None: the raw products |
| option | dorme | gato | sonha |
|---|---|---|---|
| default | 0.5427 | 0.6411 | 0.5427 |
smooth_idf=False | 0.5864 | 0.5588 | 0.5864 |
sublinear_tf=True | 0.5774 | 0.5774 | 0.5774 |
norm=None | 1.6931 | 2 | 1.6931 |
smooth_idf=False: \(\ln(n/\text{df}) + 1\); the rare words get \(\ln 3 + 1 = 2.0986\) and overtake "gato".sublinear_tf=True: count → \(1 + \ln(\text{tf})\); the two "gato" count \(1.6931\), equal to the rare words' idf by coincidence.norm=None: raw products; longer documents get larger vectors (matters for Euclidean distance, not for the cosine).Each word is its own axis, orthogonal to every other word.
| pair | cosine | shared words |
|---|---|---|
| synonyms: o carro é rápido / o automóvel é veloz | 0.00 | none |
| word order: o cachorro mordeu o homem / o homem mordeu o cachorro | 1.00 | cachorro, homem, mordeu |
| two meanings: a manga da camisa rasgou / comi uma manga madura | 0.14 | manga |
| same meaning: a manga da camisa rasgou / o punho da blusa descosturou | 0.14 | da |
'o cachorro latiu' non-zeros: 1 {'cachorro': 1.0}
'um felino adormeceu' non-zeros: 0 {}
"a feline fell asleep": a sentence about a cat sleeping, next to documents about cats sleeping, with similarity 0 to all of them.
smooth_idf, sublinear_tf and norm change the weights; sublinear_tf stops a repeated word from taking over its row.fit has no column; a text made only of new words is the zero vector.Pace: step 6 and the simulation in 10 min; step 7 in 5 min.