# TF-IDF — Term Frequency-Inverse Document Frequency

## What is TF-IDF?

Term Frequency - Inverse Document Frequency (TF-IDF) is a widely used statistical method in natural language processing and information retrieval. It measures how important a term is within a document relative to a collection of documents (i.e., relative to a corpus). Words within a text document are transformed into importance numbers by a text vectorization process. There are many different text vectorization scoring schemes, with TF-IDF being one of the most common.

As its name implies, TF-IDF vectorizes/scores a word by multiplying the word’s Term Frequency (TF) with the Inverse Document Frequency (IDF).

**Term Frequency:** TF of a term or word is the number of times the term appears in a document compared to the total number of words in the document.

$$ TF = \frac {\textrm{number of times the term appears in the document} }{ \textrm{total number of terms in the document}} $$

**Inverse Document Frequency**: IDF of a term reflects the proportion of documents in the corpus that contain the term. Words unique to a small percentage of documents (e.g., technical jargon terms) receive higher importance values than words common across all documents (e.g., a, the, and).

$$ IDF = log (\frac {\textrm{number of the documents in the corpus}} {\textrm{number of documents in the corpus contain the term}}) $$

The TF-IDF of a term is calculated by multiplying TF and IDF scores.

$$ \textit{TF-IDF} = TF * IDF $$

Translated into plain English, importance of a term is high when it occurs a lot in a given document and rarely in others. In short, commonality within a document measured by TF is balanced by rarity between documents measured by IDF. The resulting TF-IDF score reflects the importance of a term for a document in the corpus.

TF-IDF is useful in many natural language processing applications. For example, Search Engines use TF-IDF to rank the relevance of a document for a query. TF-IDF is also employed in text classification, text summarization, and topic modeling.

Note that there are some different approaches to calculating the IDF score. The base 10 logarithm is often used in the calculation. However, some libraries use a natural logarithm. In addition, one can be added to the denominator as follows in order to avoid division by zero.

$$ IDF = log (\frac {\textrm{number of the documents in the corpus}} {\textrm{number of documents in the corpus contain the term} +1}) $$

## Numerical Example

Imagine the term $t$ appears 20 times in a document that contains a total of 100 words. Term Frequency (TF) of $t$ can be calculated as follow:

$$ TF= \frac{20}{100} = 0.2 $$

Assume a collection of related documents contains 10,000 documents. If 100 documents out of 10,000 documents contain the term $t$, Inverse Document Frequency (IDF) of $t$ can be calculated as follows

$$ IDF = log \frac{10000}{100} = 2 $$

Using these two quantities, we can calculate TF-IDF score of the term $t$ for the document.

$$ \textit{TF-IDF} = 0.2 * 2 = 0.4 $$

## Python Implementation

Some popular python libraries have a function to calculate TF-IDF. The popular machine learning library `Sklearn`

has `TfidfVectorizer()`

function (docs).

We will write a TF-IDF function from scratch using the standard formula given above, but we will not apply any preprocessing operations such as stop words removal, stemming, punctuation removal, or lowercasing. It should be noted that the result may be different when using a native function built into a library.

First, let's construct a small corpus.

Next, we'll create a word set for the corpus:

### Computing Term Frequency

Now we can create a dataframe by the number of documents in the corpus and the word set, and use that information to compute the **term frequency (TF)**:

important | scientists | best | courses | this | analyze | of | most | the | is | science | fields | one | data | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|

0 | 0.090909 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.00 | 0.181818 | 0.090909 | 0.090909 | 0.090909 | 0.181818 | 0.090909 | 0.090909 | 0.090909 |

1 | 0.000000 | 0.00 | 0.111111 | 0.111111 | 0.111111 | 0.00 | 0.111111 | 0.000000 | 0.111111 | 0.111111 | 0.111111 | 0.000000 | 0.111111 | 0.111111 |

2 | 0.000000 | 0.25 | 0.000000 | 0.000000 | 0.000000 | 0.25 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.500000 |

The dataframe above shows we have a column for each word and a row for each document. This shows the frequency of each word in each document.

### Computing Inverse Document Frequency

Now, we'll compute the **inverse document frequency (IDF)**:

### Putting it Together: Computing TF-IDF

Since we have TF and IDF now, we can compute **TF-IDF**:

important | scientists | best | courses | this | analyze | of | most | the | is | science | fields | one | data | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|

0 | 0.043375 | 0.00000 | 0.000000 | 0.000000 | 0.000000 | 0.00000 | 0.032017 | 0.043375 | 0.016008 | 0.016008 | 0.032017 | 0.043375 | 0.016008 | 0.0 |

1 | 0.000000 | 0.00000 | 0.053013 | 0.053013 | 0.053013 | 0.00000 | 0.019566 | 0.000000 | 0.019566 | 0.019566 | 0.019566 | 0.000000 | 0.019566 | 0.0 |

2 | 0.000000 | 0.11928 | 0.000000 | 0.000000 | 0.000000 | 0.11928 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.0 |

Notice that "data" has an IDF of 0 because it appears in every document. As a result, is not considered to be an important term in this corpus. This will change slightly in the following sklearn implementation, where "data" will be non-zero.

## TF-IDF Using scikit-learn

First, we need to import sklearn's TfidfVectorizer:

We need to instantiate the class first, then we can call the `fit_transform`

method on our test corpus. This will perform all of the calculations we performed above.

After vectorizing the corpus by the function, a sparse matrix is obtained.

Here's the current shape of the matrix:

And we can convert to an regular array to get a better idea of the values:

It's now very straightforward to obtain the original terms in the corpus by using `get_feature_names`

:

Finally, we'll create a dataframe to better show the TF-IDF scores of each document:

analyze | best | courses | data | fields | important | is | most | of | one | science | scientists | the | this | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|

0 | 0.000000 | 0.000000 | 0.000000 | 0.189526 | 0.320895 | 0.320895 | 0.244049 | 0.320895 | 0.488098 | 0.244049 | 0.488098 | 0.000000 | 0.244049 | 0.000000 |

1 | 0.000000 | 0.400294 | 0.400294 | 0.236420 | 0.000000 | 0.000000 | 0.304434 | 0.000000 | 0.304434 | 0.304434 | 0.304434 | 0.000000 | 0.304434 | 0.400294 |

2 | 0.542701 | 0.000000 | 0.000000 | 0.641055 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.542701 | 0.000000 | 0.000000 |

As you can see from the output above, the TF-IDF scores are different than the scores obtained by the manual process we used earlier. This difference is due to sklearn's implementation of TF-IDF, which uses a slightly different formula. For more details, you can learn more about how sklearn calculates TF-IDF term weighting here.