Challenge - 5 Problems
Cosine Similarity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:00remaining
Understanding the range of cosine similarity values
What is the range of values that cosine similarity between two vectors can take?
Attempts:
2 left
💡 Hint
Think about the angle between two vectors and how cosine behaves.
✗ Incorrect
Cosine similarity measures the cosine of the angle between two vectors. It ranges from -1 (vectors point in opposite directions) to 1 (vectors point in the same direction). Zero means vectors are orthogonal (at 90 degrees).
❓ Predict Output
intermediate1:30remaining
Output of cosine similarity calculation
What is the output of this Python code calculating cosine similarity between two vectors?
NLP
import numpy as np def cosine_similarity(a, b): return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)) vec1 = np.array([1, 0, 0]) vec2 = np.array([0, 1, 0]) result = cosine_similarity(vec1, vec2) print(round(result, 2))
Attempts:
2 left
💡 Hint
Consider the angle between the two vectors.
✗ Incorrect
The vectors are orthogonal (at 90 degrees), so their cosine similarity is 0. The dot product is zero, and norms are 1, so the result is 0.
❓ Model Choice
advanced1:30remaining
Choosing cosine similarity for text similarity
You want to measure similarity between two text documents represented as TF-IDF vectors. Which similarity measure is most appropriate?
Attempts:
2 left
💡 Hint
Think about how vector length affects similarity in text data.
✗ Incorrect
Cosine similarity is preferred for text vectors because it measures the angle between vectors, ignoring their length, which is useful when document lengths vary.
❓ Metrics
advanced1:00remaining
Interpreting cosine similarity value in recommendation
A recommendation system uses cosine similarity between user preference vectors. If two users have a cosine similarity of 0.95, what does this imply?
Attempts:
2 left
💡 Hint
Recall what a high cosine similarity value means.
✗ Incorrect
A cosine similarity close to 1 means the vectors point in nearly the same direction, indicating very similar preferences.
🔧 Debug
expert2:00remaining
Debugging cosine similarity code with zero vector
What error will this code raise when computing cosine similarity if one input vector is all zeros?
NLP
import numpy as np def cosine_similarity(a, b): return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)) vec1 = np.array([0, 0, 0]) vec2 = np.array([1, 2, 3]) result = cosine_similarity(vec1, vec2) print(result)
Attempts:
2 left
💡 Hint
Think about what happens when dividing by zero norm.
✗ Incorrect
The norm of vec1 is zero, so the denominator is zero, causing a ZeroDivisionError.