Cosine Similarity
Implement cosine_similarity(a, b) without external libraries. Return 0.0 when either vector has zero magnitude and round the result to six decimals.
INTERACTIVE CONCEPT
0.71similarityExplore vector similarity
Rotate the second unit vector to see how alignment changes the cosine score.
For these unit vectors, cosine similarity equals cos(angle). A zero vector needs separate handling in your solution.
0° aligned90° perpendicular180° opposite
Examples
EXAMPLE WALKTHROUGHidentical
A guided view of the published example, not a trace of your code.
INPUT
[[1,0],[1,0]]TASKcosine_similarity()
EXPECTED OUTPUT
1Start with the input values shown in this example.
Example 1 of 2
Input: a = [1,0], b = [1,0]
Output: 1
Explanation: identical
Input: a = [1,0], b = [0,1]
Output: 0
Explanation: orthogonal
Hint 1
Start with the dot product.
Hint 2
Compute each L2 norm from the sum of squared values.