The tf-idf value increases in proportion to the number of times a word appears in the document, but is often offset by the frequency of the word in the corpus, which helps to correct for the fact that some words appear the most often.
TF-IDF uses two statistical methods , the first of them — Term Frequency and the other — Inverse Document Frequency. Term frequency refers to the total number of times a given term t appears in a doc document, in relation to (per) the total number of all words in the document and the Inverse Document Frequency, which determines how much information a word provides. It measures the weight of a given word throughout the entire document. IDF shows how often or rarely a given word appears in all documents.
TF-IDF can be calculated as tf * idf
Tf * Idf does not directly convert raw data into useful functions. First, it converts raw strings or dataset to vectors, and each word has its own vector. Then we will use a specific method to extract a function like Cosine Similarity that works with vectors, etc. As we know, we cannot directly pass a string to our model. So tf * idf gives us the numeric values of the entire document.
To extract elements from the word document, we import —
from sklearn.feature_extraction.text import TfidfVectorizer
1st Sentence - "hello i am pulkit" 2nd Sentence - "your name is akshit"
Code: Python code to find similarity measures
Output: p >
manhatten cos_sim euclidean 0 2.955813 0.0 1.414214
Dataset: Google Drive link
Note: The dataset is large, so it will take 30-40 seconds to display, and if you are going to work as it is, then it won`t work. This only works when you copy this code into your IDE and provide your dataset in the tfidf function.