Cosine Similarity
Number Lists
Vector Analysis
Mathematical Computing
Data Science

Cosine Similarity between 2 Number Lists

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

Cosine similarity is a fundamental concept used in various fields, such as information retrieval, text analysis, and data mining, to measure the similarity between two non-zero vectors. When considering number lists, which can be perceived as vectors in multi-dimensional space, cosine similarity provides insights into the orientation of the lists irrespective of their magnitude. Below, we discuss the intricacies of cosine similarity for two number lists, detailing its computation, properties, and applications.

Understanding Cosine Similarity

Technical Definition

Cosine similarity measures the cosine of the angle between two vectors projected in multi-dimensional space. Given two vectors, A=(a1,a2,,an)A = (a_1, a_2, \ldots, a_n) and B=(b1,b2,,bn)B = (b_1, b_2, \ldots, b_n), the cosine similarity, denoted as simcos(A,B)sim_{cos}(A, B), is defined as:

sim_cos(A,B)=ABA,Bsim\_{cos}(A, B) = \frac{A \cdot B}{||A|| , ||B||}

Where: • ABA \cdot B is the dot product of vectors AA and BB. • A||A|| and B||B|| are the Euclidean norms (magnitudes) of vectors AA and BB, respectively.

Calculation Steps

  1. Calculate Dot Product: AB=i=1naibiA \cdot B = \sum_{i=1}^{n} a_i b_i
  2. Calculate Magnitude of Each Vector: • A=i=1nai2||A|| = \sqrt{\sum_{i=1}^{n} a_i^2}B=i=1nbi2||B|| = \sqrt{\sum_{i=1}^{n} b_i^2}
  3. Compute Cosine Similarity: • simcos(A,B)=i=1naibii=1nai2×i=1nbi2sim_{cos}(A, B) = \frac{\sum_{i=1}^{n} a_i b_i}{\sqrt{\sum_{i=1}^{n} a_i^2} \times \sqrt{\sum_{i=1}^{n} b_i^2}}

Properties

Range: The cosine similarity value ranges from -1 to 1. • 1 indicates complete similarity, meaning the vectors point in the same direction. • 0 indicates orthogonality, meaning there is no similarity. • -1 indicates complete dissimilarity, meaning the vectors point in opposite directions.

Invariant to Magnitude: As cosine similarity measures the angle, it is unaffected by the magnitude of the vectors, making it a useful metric for assessing direction-based similarity.

Example

Consider two number lists:

A=[2,3,4]A = [2, 3, 4]B=[1,0,5]B = [1, 0, 5]

Step 1: Compute Dot Product AB=(2×1)+(3×0)+(4×5)=2+0+20=22A \cdot B = (2 \times 1) + (3 \times 0) + (4 \times 5) = 2 + 0 + 20 = 22

Step 2: Compute MagnitudesA=22+32+42=4+9+16=29||A|| = \sqrt{2^2 + 3^2 + 4^2} = \sqrt{4 + 9 + 16} = \sqrt{29}B=12+02+52=1+0+25=26||B|| = \sqrt{1^2 + 0^2 + 5^2} = \sqrt{1 + 0 + 25} = \sqrt{26}

Step 3: Compute Cosine Similarity simcos(A,B)=2229×262227.030.814sim_{cos}(A, B) = \frac{22}{\sqrt{29} \times \sqrt{26}} \approx \frac{22}{27.03} \approx 0.814

Thus, the cosine similarity of AA and BB is approximately 0.814, indicating a strong directional similarity.

Applications

Cosine similarity is extensively used across multiple domains. Some examples include:

Text Mining and NLP: In Natural Language Processing, cosine similarity is used to compare documents by converting them into TF-IDF vectors, allowing the comparison of textual similarity between documents.

Recommendation Systems: It helps in building recommendation engines by comparing the similarity between user profiles or item vectors, which can suggest items or services with similar profiles.

Clustering and Classification: In unsupervised learning, cosine similarity aids in clustering similar data points and is also used in various classification tasks.

Key Points Summary

AspectDescription
DefinitionMeasures the cosine of the angle between two vectors in multi-dimensional space.
Calculationsimcos(A,B)=ABlVertArVertlVertBrVertsim_{cos}(A, B) = \frac{A \cdot B}{\\lVert A \\rVert \, \\lVert B \\rVert}
Range-1 (opposite), 0 (orthogonal), 1 (similar)
PropertiesMagnitude-invariant, measures directional similarity
Common ApplicationsNLP, Recommendation Systems, Clustering

In conclusion, cosine similarity offers a robust method for evaluating the similarity between number lists. By focusing on the direction rather than the magnitude, it provides valuable insights in various data-driven applications where similarity assessment is crucial.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the 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.