Loading content...
In vector mathematics and machine learning, measuring the directional similarity between two vectors is a fundamental operation that captures how similarly oriented they are in space, independent of their magnitudes. This concept, known as angular similarity, quantifies the relationship between vectors based purely on the angle between them.
The angular similarity between two vectors v₁ and v₂ is computed by taking the normalized dot product of the vectors—dividing their dot product by the product of their magnitudes (Euclidean norms):
$$\text{angular_similarity}(v_1, v_2) = \frac{v_1 \cdot v_2}{|v_1| \times |v_2|} = \frac{\sum_{i=1}^{n} v_{1i} \cdot v_{2i}}{\sqrt{\sum_{i=1}^{n} v_{1i}^2} \times \sqrt{\sum_{i=1}^{n} v_{2i}^2}}$$
Interpretation of Values:
Key Properties:
Your Task: Write a Python function that computes the angular similarity between two vectors. The function should:
v1 = [1.0, 2.0, 3.0]
v2 = [2.0, 4.0, 6.0]1.0The vectors v1 and v2 are scalar multiples of each other (v2 = 2 × v1). Since they point in exactly the same direction, their angular similarity is 1.0.
• Dot product: (1×2) + (2×4) + (3×6) = 2 + 8 + 18 = 28 • ||v1|| = √(1² + 2² + 3²) = √14 ≈ 3.742 • ||v2|| = √(2² + 4² + 6²) = √56 ≈ 7.483 • Angular similarity = 28 / (3.742 × 7.483) = 28 / 28 = 1.0
v1 = [1.0, 0.0]
v2 = [0.0, 1.0]0.0These vectors are orthogonal (perpendicular) to each other in 2D space—one points along the x-axis and the other along the y-axis.
• Dot product: (1×0) + (0×1) = 0 • ||v1|| = √(1² + 0²) = 1 • ||v2|| = √(0² + 1²) = 1 • Angular similarity = 0 / (1 × 1) = 0.0
Orthogonal vectors always have an angular similarity of 0, indicating no directional correlation.
v1 = [1.0, 1.0, 1.0]
v2 = [-1.0, -1.0, -1.0]-1.0These vectors point in exactly opposite directions (v2 = -1 × v1), representing perfect anti-correlation.
• Dot product: (1×-1) + (1×-1) + (1×-1) = -3 • ||v1|| = √(1² + 1² + 1²) = √3 ≈ 1.732 • ||v2|| = √((-1)² + (-1)² + (-1)²) = √3 ≈ 1.732 • Angular similarity = -3 / (1.732 × 1.732) = -3 / 3 = -1.0
Constraints