Compute the cosine similarity between two vectors:
cosine(a,b)=∥a∥2∥b∥2a⋅bHere, a⋅b is the dot product and ∥a∥2 and ∥b∥2 are Euclidean norms. For this problem, return 0.0 when either vector has zero norm. Otherwise, return the similarity as a Python float.
Input: a = [1, 2, 3], b = [2, 4, 6]
Output: 1.0
Explanation: One vector is a positive multiple of the other, so they point in the same direction.
Input: a = [1, 0], b = [0, 1]
Output: 0.0
Use np.dot(a, b) for the numerator.
Use np.linalg.norm() on both vectors before dividing.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Compute the cosine similarity between two vectors:
cosine(a,b)=∥a∥2∥b∥2a⋅bHere, a⋅b is the dot product and ∥a∥2 and ∥b∥2 are Euclidean norms. For this problem, return 0.0 when either vector has zero norm. Otherwise, return the similarity as a Python float.
Input: a = [1, 2, 3], b = [2, 4, 6]
Output: 1.0
Explanation: One vector is a positive multiple of the other, so they point in the same direction.
Input: a = [1, 0], b = [0, 1]
Output: 0.0
Use np.dot(a, b) for the numerator.
Use np.linalg.norm() on both vectors before dividing.
Sign in to take notes on this problem
Accepts: array
Accepts: array