Bird
0
0

What is the output of the following Python code snippet?

medium📝 Predict Output Q4 of 15
NLP - Text Similarity and Search
What is the output of the following Python code snippet? from sklearn.metrics.pairwise import cosine_similarity import numpy as np vec1 = np.array([[1, 0, 1]]) vec2 = np.array([[0, 1, 0]]) result = cosine_similarity(vec1, vec2) print(result[0][0])
AUndefined (error)
B1.0
C0.5
D0.0
Step-by-Step Solution
Solution:
  1. Step 1: Understand vectors and cosine similarity

    vec1 = [1,0,1], vec2 = [0,1,0]. They have no overlapping nonzero elements, so their dot product is 0.
  2. Step 2: Calculate cosine similarity

    Cosine similarity = dot(vec1, vec2) / (norm(vec1)*norm(vec2)) = 0 / (sqrt(2)*1) = 0.
  3. Final Answer:

    0.0 -> Option D
  4. Quick Check:

    Cosine similarity of orthogonal vectors = 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
  • Forgetting to normalize vectors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes