Term Frequency–Inverse Document Frequency (TF-IDF)

From PRIMO.ai
Jump to navigation Jump to search

Youtube search... | ...Google search


Term Frequency–Inverse Document Frequency (TF-IDF) is a statistical measure that evaluates how relevant a word is to a document in a collection of documents. This statistic represents words’ importance in each document. It is calculated by multiplying two different metrics:

  • Term frequency (TF): The number of times a word appears in a document. We use a word's frequency as a proxy for its importance: if "football" is mentioned 25 times in a document, it might be more important than if it was only mentioned once.
  • Inverse document frequency (IDF): A measure of how common or rare a word is across the entire collection of documents (the number of documents containing a given word).

The IDF score is calculated by dividing the total number of documents in the collection by the number of documents that contain the word. This means that words that are more common in general will have a lower IDF score, and words that are more rare will have a higher IDF score. This minimizes the effect of stop-words such as pronouns, or domain-specific language that does not add much information (for example, a word such as "news" that might be present in most documents).

The TF-IDF score is then calculated by multiplying the TF and IDF scores. This means that words that appear frequently in a document and are rare in the collection as a whole will have a higher TF-IDF score.

TF-IDF is a widely used metric in information retrieval and Natural Language Processing (NLP). It is used to rank documents in search results, to identify important keywords in documents, and to recommend documents to users. TF-IDF is a powerful tool that can be used to extract important information from documents. It is used in a wide variety of applications, including search engines, document classification, and text summarization.

Example Use Case

Suppose we have a collection of documents about different types of animals. We want to identify the most important keywords in a document about dogs. We can use TF-IDF to do this by calculating the TF-IDF score for each word in the document. The words with the highest TF-IDF scores will be the most important keywords in the document.

For example, the word "dog" will likely have a high TF-IDF score in a document about dogs, because it will appear frequently in the document and will be rare in the collection as a whole. Other important keywords in the document might include "breed," "training," and "exercise."

Step-by-step Overview of the TF-IDF Vectorization Process

  1. Calculate the TF for each word in a document by dividing the number of times the word appears in the document by the total number of words in the document.
  2. Calculate the IDF for each word by dividing the total number of documents in the corpus by the number of documents that contain the word, and taking the logarithm of the result.
  3. Multiply the TF and IDF values for each word to get the TF-IDF score.
  4. Repeat steps 1-3 for all the documents in the corpus to create a TF-IDF vector for each document, where the vector's dimension is equal to the vocabulary words.
  5. Use the TF-IDF vectors for various tasks, such as similarity calculations using cosine similarity or information retrieval.

Similarity

TF-IDF is a transformation applied to texts to get two real-valued vectors in vector space. We can then obtain the Cosine similarity of any pair of vectors by taking their dot product and dividing that by the product of their norms. That yields the cosine of the angle between the vectors. Cosine similarity is a measure of similarity between two non-zero vectors. Using this formula we can find out the similarity between any two documents d1 and d2. Building a Simple Chatbot from Scratch in Python (using NLTK) | Paul Pandey - Analytics Vidhya

Featured Videos