What Is TF-IDF? Weighting Words by Document Importance
How term and document frequency combine, with a worked example, scikit-learn usage, and practical limits.
TF-IDF (term frequency–inverse document frequency) gives greater relative weight to terms that occur in a document but appear in fewer documents overall. TF counts occurrences within a document; DF counts documents containing a term, and IDF downweights terms with high DF. scikit-learn defaults to idf(t)=log((1+N)/(1+df(t)))+1 and L2-normalizes each document vector. In four documents, a term present in all four has IDF 1, while one present in only one has a larger value; if each occurs once, the rarer term receives more weight. TfidfVectorizer learns vocabulary and IDF: fit only on training text, then use transform on validation, test, and new text. TF-IDF is a baseline for search, similarity, classification, or clustering, but it does not encode word order or context. Compare it with counts on the same held-out data and metric. The code example is illustrative and was not run.