TD-IDF Find Cosine Similarity Between New Document and Dataset
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Understanding `TF-IDF` and Cosine Similarity
In the context of information retrieval and machine learning, evaluating the similarity between textual documents is crucial. Two pivotal techniques in this domain are Term Frequency-Inverse Document Frequency (TF-IDF) and Cosine Similarity. These statistical measures convert text data into numerical representations to analyze and compute how similar documents are to one another. Here's a detailed exploration of these methods.
Term Frequency-Inverse Document Frequency (TF-IDF)
`TF-IDF` is a numerical statistic that intends to reflect how important a word is to a document in a collection or corpus. It is often used as a weighting factor in searches of information retrieval, text mining, and user modeling. The concept is broken down into two parts:
- Term Frequency (TF): This measures how frequently a term appears in a document. Given a term , a document , and the term frequency is calculated as:
- Inverse Document Frequency (IDF): This quantifies the importance of the term across the entire corpus:where: • is the total number of documents in the corpus. • is the number of documents containing the term .
The `TF-IDF` score is thus calculated by multiplying these two metrics:
Cosine Similarity
Once we have a vector representation of the documents using TF-IDF, we move to calculate the similarity between these vectors. Cosine Similarity is a measure that calculates the cosine of the angle between two non-zero vectors in an inner product space, which essentially determines how similar the two documents are in terms of their content. It is defined as:
where:
• $\vec\{A\}$ and $\vec\{B\}$ are the `TF-IDF` vectors for two documents.
• denotes the dot product of the two vectors.
• and are the magnitudes (or euclidean norms) of vectors $\vec\{A\}$ and $\vec\{B\}$, respectively.
Implementing `TF-IDF` and Calculating Cosine Similarity
To illustrate, consider implementing `TF-IDF` and cosine similarity on a dataset. Suppose we have a set of documents and a new incoming document. Our goal is to find the closest document in the dataset to the new document using these measures.
Sample Dataset
| Document ID | Content |
| 1 | "The sky is blue and beautiful" |
| 2 | "Love this blue and bright sky" |
| 3 | "The quick brown fox jumps over the lazy dog" |
| 4 | "A fast brown fox leaps over a sleeping dog" |
New Document
• Content: "A brown fox quickly jumps over the lazy sleeping dog"
Steps to Compute Similarity
- Preprocessing: Tokenize, lower-case, and perform stop-word removal on all documents.
- TF-IDF Vectorization: • Calculate the `TF-IDF` vectors for each document and the new document.
- Cosine Similarity Calculation: • Compute the cosine similarity between the new document vector and each document vector in the dataset.
Example Result
If we assume the `TF-IDF` vectors are generated, the resulting table for cosine similarity might look like this:
| Document ID | Cosine Similarity |
| 1 | 0.23 |
| 2 | 0.19 |
| 3 | 0.38 |
| 4 | 0.45 |
Conclusion
The document with the highest cosine similarity to the new document will be the most contextually similar one. In this example, Document 4 ("A fast brown fox leaps over a sleeping dog") has the highest similarity, thus being the closest to the new document.
`TF-IDF` and Cosine Similarity are powerful methods that transform raw text data into quantitative analyses. These metrics are extensively used in applications such as document retrieval, clustering, and classification tasks. Understanding and efficiently implementing these techniques can enhance text analysis in data-heavy environments.
Related reading
- Tensorflow can not restore vocabulary in evaluation process
- TensorFlow Embedding Lookup
- Tensorflow Enlarge images on Tensorboard embedding?
- Tensorflow implementation of word2vec
- TensorBoard doesn't show all data points
- TensorBoard Embedding Example?
- Tensorflow vocabularyprocessor
- TensorFlow with a NER-Tagger
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.