Bird
0
0

Given two vectors A = [2, 2, 1] and B = [1, 0, 1], what is the cosine similarity computed by the code below?

medium📝 Predict Output Q5 of 15
NLP - Text Similarity and Search
Given two vectors A = [2, 2, 1] and B = [1, 0, 1], what is the cosine similarity computed by the code below? import numpy as np A = np.array([2, 2, 1]) B = np.array([1, 0, 1]) cos_sim = np.dot(A, B) / (np.linalg.norm(A) * np.linalg.norm(B)) print(round(cos_sim, 2))
A0.71
B0.75
C0.50
D0.85
Step-by-Step Solution
Solution:
  1. Step 1: Calculate dot product

    dot(A,B) = 2*1 + 2*0 + 1*1 = 2 + 0 + 1 = 3.
  2. Step 2: Calculate norms and cosine similarity

    norm(A) = sqrt(2²+2²+1²) = sqrt(4+4+1) = sqrt(9) = 3; norm(B) = sqrt(1²+0+1²) = sqrt(2) ≈ 1.414.
    Cosine similarity = 3 / (3 * 1.414) ≈ 3 / 4.242 ≈ 0.707; round(0.707, 2) = 0.71.
  3. Final Answer:

    0.71 -> Option A
  4. Quick Check:

    Cosine similarity calculation = 0.71 [OK]
Quick Trick: Calculate dot product and divide by product of norms [OK]
Common Mistakes:
MISTAKES
  • Forgetting to take vector norms
  • Incorrect dot product calculation
  • Rounding errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes