Bird
0
0

What is the output of this Python code snippet?

medium📝 Predict Output Q13 of 15
NLP - Text Similarity and Search
What is the output of this Python code snippet?
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np

emb1 = np.array([[1, 0, 0]])
emb2 = np.array([[0, 1, 0]])
sim = cosine_similarity(emb1, emb2)
print(sim[0][0])
AError
B1.0
C-1.0
D0.0
Step-by-Step Solution
Solution:
  1. Step 1: Understand cosine similarity formula

    Cosine similarity measures the cosine of the angle between two vectors. Orthogonal vectors have similarity 0.
  2. Step 2: Analyze given vectors

    emb1 is [1,0,0], emb2 is [0,1,0]. They are perpendicular, so similarity is 0.
  3. Final Answer:

    0.0 -> Option D
  4. Quick Check:

    Orthogonal vectors similarity = 0.0 [OK]
Quick Trick: Orthogonal vectors have cosine similarity zero [OK]
Common Mistakes:
MISTAKES
  • Assuming similarity is 1 for any vectors
  • Confusing dot product with cosine similarity
  • Expecting error due to shape

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes