TF-IDF Weight
Term importance: frequency in a document scaled by rarity across documents.
Formula
Variables
Example
Rare but frequent-in-doc terms score highest
How it works
TF-IDF gives a term a high score when it appears often in one document but not in many others. The first part, term frequency, rewards words that seem important inside the document; the second part, inverse document frequency, down-weights words that are common everywhere, because those words usually carry less distinguishing information. It is widely used because it balances local importance against global commonness, making the score useful for search, text classification, and keyword extraction.
When to use it
Use TF-IDF when you need to rank which words matter most in a document compared with a larger collection of documents, such as finding keywords, building search indexes, or creating simple text features for machine learning. It is especially useful for bag-of-words style text problems where word order is less important than word presence and relative importance.
Worked example, step by step
Suppose a collection has N = 500 documents. We want the TF-IDF weight for the term "neural" in one document. Step 1: term frequency in the document tf_{t,d} = 4 Step 2: number of documents containing the term df_t = 10 Step 3: compute inverse document frequency log(N / df_t) = log(500 / 10) = log(50) Using natural log, log(50) ≈ 3.912 Step 4: multiply tf by idf w_{t,d} = tf_{t,d} × log(N / df_t) w_{t,d} = 4 × 3.912 = 15.648 Final answer: w_{t,d} ≈ 15.65
Common mistakes to avoid
- Using the total number of times a word appears in the whole corpus instead of the number of documents containing it
- Forgetting that common words like "the" or "and" usually get very small weights even if they appear many times in one document
- Mixing up the logarithm base or the exact TF-IDF variant being used in a library
Did You Know?
TF-IDF was the backbone of search engines before neural embeddings.
Share this formula
More in Data Science & ML
View allMean Squared Error (MSE)
BasicAverage of squared differences between predicted and actual values.
Root Mean Squared Error (RMSE)
BasicSquare root of MSE, in the same units as the target variable.
Mean Absolute Error (MAE)
BasicAverage of absolute differences between predictions and actual values.
Coefficient of Determination (R²)
IntermediateProportion of variance in the target explained by the model.
Classification Accuracy
BasicFraction of predictions that are correct.
Precision
IntermediateOf all positive predictions, the fraction that were correct.