How is cosine similarity computed?
- User
How is cosine similarity computed?
Assistant
Cosine similarity measures the similarity between two non-zero vectors by calculating the cosine of the angle between them. It's computed using the dot product of the vectors divided by the product of their Euclidean norms (magnitudes). Here's the step-by-step process:
Formula:
[ \text{cosine_similarity} = \frac{\mathbf{A} \cdot \mathbf{B}}{|\mathbf{A}| |\mathbf{B}|} ] where:
- (\mathbf{A} \cdot \mathbf{B}) is the dot product of vectors (\mathbf{A}) and (\mathbf{B}),
- (|\mathbf{A}|) and (|\mathbf{B}|) are the magnitudes (Euclidean norms) of the vectors.
Step-by-Step Calculation:
-
Compute the dot product: [ \mathbf{A} \cdot \mathbf{B} = \sum_{i=1}^{n} A_i \times B_i ]
- Multiply corresponding elements of both vectors and sum the results.
Example: (\mathbf{A} = [1, 2, 3], \mathbf{B} = [4, 5, 6])
(\mathbf{A} \cdot \mathbf{B} = (1 \times 4) + (2 \times 5) + (3 \times 6) = 4 + 10 + 18 = 32)
- Multiply corresponding elements of both vectors and sum the results.
-
Compute the magnitude of each vector: [ |\mathbf{A}| = \sqrt{\sum_{i=1}^{n} A_i^2}, \quad |\mathbf{B}| = \sqrt{\sum_{i=1}^{n} B_i^2} ]
- Square each element, sum the squares, and take the square root.
Example:
(|\mathbf{A}| = \sqrt{1^2 + 2^2 + 3^2} = \sqrt{14} \approx 3.742)
(|\mathbf{B}| = \sqrt{4^2 + 5^2 + 6^2} = \sqrt{77} \approx 8.775)
- Square each element, sum the squares, and take the square root.
-
Calculate cosine similarity: [ \text{cosine_similarity} = \frac{\mathbf{A} \cdot \mathbf{B}}{|\mathbf{A}| \times |\mathbf{B}|} ] Example:
(\frac{32}{3.742 \times 8.775} \approx \frac{32}{32.84} \approx 0.974)
Key Properties:
- Range: Values are between -1 (perfectly opposite) and 1 (identical direction).
- 1: Vectors point the same way.
- 0: Vectors are orthogonal (no correlation).
- -1: Vectors point in opposite directions.
- Ignores magnitude: Measures orientation, not length. Useful for text similarity (e.g., comparing TF-IDF vectors).
- Efficiency: Computationally efficient for sparse vectors (e.g., text data).
Example Use Case:
Comparing document similarity using TF-IDF vectors:
- Doc1: "I love cats" → ([0.8, 0.5, 0.3])
- Doc2: "I adore cats" → ([0.7, 0.6, 0.2])
- Cosine similarity ≈ 0.99 (highly similar).
Python Implementation:
import numpy as np def cosine_similarity(a, b): dot_product = np.dot(a, b) norm_a = np.linalg.norm(a) norm_b = np.linalg.norm(b) return dot_product / (norm_a * norm_b) # Example usage: A = np.array([1, 2, 3]) B = np.array([4, 5, 6]) print(cosine_similarity(A, B)) # Output: 0.974631846Note: Returns
NaNif either vector is zero (division by zero). Handle zero vectors by checking magnitudes first.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.