0
0
NLPml~20 mins

Cosine similarity in NLP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cosine Similarity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:00remaining
Understanding the range of cosine similarity values
What is the range of values that cosine similarity between two vectors can take?
AFrom -1 to 0, where -1 means identical vectors and 0 means orthogonal vectors
BFrom -1 to 1, where 1 means identical direction and -1 means opposite direction
CFrom 0 to 1, where 0 means orthogonal vectors and 1 means identical vectors
DFrom 0 to infinity, where higher values mean more similarity
Attempts:
2 left
💡 Hint
Think about the angle between two vectors and how cosine behaves.
Predict Output
intermediate
1: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))
A0.71
B1.00
C-1.00
D0.00
Attempts:
2 left
💡 Hint
Consider the angle between the two vectors.
Model Choice
advanced
1: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?
ACosine similarity
BEuclidean distance
CManhattan distance
DJaccard index
Attempts:
2 left
💡 Hint
Think about how vector length affects similarity in text data.
Metrics
advanced
1: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?
AThey have no preferences in common
BThey have very different preferences
CThey have very similar preferences
DThey have opposite preferences
Attempts:
2 left
💡 Hint
Recall what a high cosine similarity value means.
🔧 Debug
expert
2: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)
AZeroDivisionError
BValueError
CTypeError
DNo error, outputs 0
Attempts:
2 left
💡 Hint
Think about what happens when dividing by zero norm.