Document Similarity
Last updated
Was this helpful?
Last updated
Was this helpful?
Let us vectorize the following three documents using the bag-of-words model with TF-IDF scores estimated from the corpus:
Once the documents are vectorized, they can be compared within the respective vector space. Two common metrics for comparing document vectors are the and .
Euclidean distance is a measure of the straight-line distance between two vectors in Euclidean space such that it represents the magnitude of the differences between the two vectors.
L6: ** k
represents the power of k
.
We then measure the Euclidean distance between the two vectors above:
Cosine similarity is a measure of similarity between two vectors in an inner product space such that it calculates the cosine of the angle between two vectors, where a value of 1 indicates that the vectors are identical (i.e., pointing in the same direction), a value of -1 indicates that they are exactly opposite, and a value of 0 indicates that the vectors are orthogonal (i.e., perpendicular to each other).
The cosine similarity between two vectors can be measured as follow:
Let us define a function that takes two sparse vectors and returns the cosine similarity between them:
We then measure the Euclidean distance between the two vectors above:
The following diagram illustrates the difference between the two metrics. The Euclidean distance measures the magnitude between two vectors, while the Cosine similarity measures their angle to the origin.
Q7: Why is Cosine Similarity generally preferred over Euclidean Distance in most NLP applications?
Let and be two vectors representing documents and . The Euclidean distance between the two vectors can be measured as follow:
Let us define a function that takes two vectors in our notation and returns the Euclidean distance between them:
The Euclidean distance between two identical vectors is 0 (L1). Interestingly, the distance between and is shorter than the distance between and , implying that is more similar to than , which contradicts our intuition.
The Cosine similarity between two identical vectors is 1, although it is calculated as 0.99 due to limitations in decimal points (L1). Similar to the Euclidean distance case, the similarity between and is greater than the similarity between and , which again contradicts our intuition.