In Machine learning, Tf-idf stands for term frequency-inverse document frequency, and the tf-idf weight is a weight often used in information retrieval and text mining. This weight is a statistical measure used to evaluate how important a word is to a document in a collection or corpus. The importance increases proportionally to the number of times a word appears in the document but is offset by the frequency of the word in the corpus. Variations of the tf-IDF weighting scheme are often used by search engines as a central tool in scoring and ranking a document's relevance given a user query.
How to calculate tf-idf:
There is a simple way to calculate tf=idf in machine learning-
First, calculate tf (term frequency)
TF: Term Frequency, which measures how frequently a term occurs in a document. Since every document is different in length, it is possible that a term would appear much more time in long documents than shorter ones. Thus, the term frequency is often divided by the document length (aka. the total number of terms in the document) as a way of normalization:
tf(t) = (occurrence of the word in a particular document/total number of word)
After this, we will calculate IDF(Inverse term frequency)
idf = log(Total number of documents/Number of documents with term t in it )
Now finally we will calculate the tf-idf
tf-idf = tf * idf
Example:
Consider a document containing 100 words wherein the word cat appears 3 times. The term frequency (i.e., tf) for cat is then (3 / 100) = 0.03. Now, assume we have 10 million documents and the word cat appears in one thousand of these. Then, the inverse document frequency (i.e., idf) is calculated as log(10,000,000 / 1,000) = 4. Thus, the Tf-idf weight is the product of these quantities: 0.03 * 4 = 0.12.