0
0
NLPml~10 mins

Cosine similarity in NLP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the dot product of two vectors.

NLP
dot_product = sum(a * b for a, b in zip(vec1, [1]))
Drag options to blanks, or click blank then click option'
Avec3
Bvec2
Cvec1
Dvector
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same vector twice, which calculates the dot product of a vector with itself.
2fill in blank
medium

Complete the code to calculate the magnitude (length) of a vector.

NLP
magnitude = sum(x[1]2 for x in vec) ** 0.5
Drag options to blanks, or click blank then click option'
A**
B+
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or multiplication instead of exponentiation to square elements.
3fill in blank
hard

Fix the error in the cosine similarity formula by completing the denominator.

NLP
cos_sim = dot_product / (magnitude1 [1] magnitude2)
Drag options to blanks, or click blank then click option'
A*
B/
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or division instead of multiplication in the denominator.
4fill in blank
hard

Fill both blanks to complete the dictionary comprehension that maps words to their vector lengths if length is greater than 3.

NLP
lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as value instead of its length.
Using < instead of > in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary of words mapped to their lengths, but only include words with length greater than 2.

NLP
result = [1]: [2] for [3] in word_list if len([3]) > 2}
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Cword_list
Attempts:
3 left
💡 Hint
Common Mistakes
Using the list name as loop variable.
Mixing variable names inconsistently.