Text Analysis
Cosine Similarity
TD-IDF
Document Similarity
Data Science

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.

Practice ML system design

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:

  1. Term Frequency (TF): This measures how frequently a term appears in a document. Given a term tt, a document dd, and the term frequency tf(t,d)tf(t, d) is calculated as:
    tf(t,d)=Number of times term t appears in document dTotal number of terms in document dtf(t, d) = \frac{\text{Number of times term } t \text{ appears in document } d}{\text{Total number of terms in document } d}
  2. Inverse Document Frequency (IDF): This quantifies the importance of the term across the entire corpus:
    idf(t,D)=log(Ndf(t,D))idf(t, D) = \log \left(\frac{N}{df(t, D)}\right)
    where: • NN is the total number of documents in the corpus. • df(t,D)df(t, D) is the number of documents containing the term tt.

The `TF-IDF` score is thus calculated by multiplying these two metrics:

tf-idf(t,d,D)=tf(t,d)×idf(t,D)tf\text{-}idf(t, d, D) = tf(t, d) \times idf(t, D)

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:

cosine_similarity(A,B)=ABAB\text{cosine\_similarity}(\vec{A}, \vec{B}) = \frac{\vec{A} \cdot \vec{B}}{|\vec{A}| |\vec{B}|}

where: • $\vec\{A\}$ and $\vec\{B\}$ are the `TF-IDF` vectors for two documents. • \cdot denotes the dot product of the two vectors. • A\|\vec{A}\| and B\|\vec{B}\| 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 IDContent
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

  1. Preprocessing: Tokenize, lower-case, and perform stop-word removal on all documents.
  2. TF-IDF Vectorization: • Calculate the `TF-IDF` vectors for each document and the new document.
  3. 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 IDCosine Similarity
10.23
20.19
30.38
40.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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.