All problems
Cosine Similarity of Vectors
Problem
Compute the cosine similarity between vectors a and b:
cos(a, b) = (a . b) / (||a||_2 * ||b||_2)
If either vector is the zero vector, return 0.0.
Hint
The result lies in [-1, 1] for nonzero vectors.
Test cases
case 1
in: [[1,0],[1,0]]
expected: 1
case 2
in: [[1,0],[0,1]]
expected: 0
case 3
in: [[1,2,3],[4,5,6]]
expected: 0.9746318461970762
case 4
in: [[0,0],[1,1]]
expected: 0
Starter codePython
def cosine_similarity(a, b):
# Your code here
pass4 test cases · run your code against them in the editor