NLP Data Science Intermediate

TF-IDF

تكرار المصطلح × معكوس تكرار الوثيقة

Abbreviation: TF-IDF

Definition

A weight that measures how important a word is to a document relative to a collection of documents: frequent in the document and rare in the others.

Definition

TF-IDF weights a term by its frequency in a document, discounted by how common it is across a corpus, highlighting distinctive words.

Practical examples

Code example Python
from sklearn.feature_extraction.text import TfidfVectorizer

docs = ['the cat sat', 'the dog sat', 'the cat ran']
v = TfidfVectorizer()
v.fit_transform(docs)
print(sorted(v.vocabulary_))

Expected output

['cat', 'dog', 'ran', 'sat', 'the']

Mathematical equations

TF-IDF

\mathrm{tfidf}(t,d)=\mathrm{tf}(t,d)\cdot\log\frac{N}{\mathrm{df}(t)}

Learn and review

Suggested terms to learn next