Bird
0
0

Identify the error in this code that tries to compute semantic similarity:

medium📝 Debug Q14 of 15
NLP - Text Similarity and Search
Identify the error in this code that tries to compute semantic similarity:
from sklearn.metrics.pairwise import cosine_similarity

emb1 = [0.1, 0.2, 0.3]
emb2 = [0.1, 0.2, 0.3]
sim = cosine_similarity(emb1, emb2)
print(sim)
Aemb1 and emb2 should be 2D arrays, not 1D lists
Bcosine_similarity function does not exist in sklearn
Cembeddings must be strings, not numbers
Dprint statement syntax is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check input format for cosine_similarity

    cosine_similarity expects 2D arrays (like [[...]]), but emb1 and emb2 are 1D lists.
  2. Step 2: Confirm other options

    cosine_similarity exists, embeddings are numeric vectors, and print syntax is correct in Python 3.
  3. Final Answer:

    emb1 and emb2 should be 2D arrays, not 1D lists -> Option A
  4. Quick Check:

    Input shape must be 2D arrays [OK]
Quick Trick: cosine_similarity needs 2D arrays, not 1D lists [OK]
Common Mistakes:
MISTAKES
  • Passing 1D lists instead of 2D arrays
  • Thinking embeddings must be text
  • Misunderstanding print syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes