0
0
Prompt Engineering / GenAIml~10 mins

Vector similarity metrics in Prompt Engineering / GenAI - 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 cosine similarity between two vectors.

Prompt Engineering / GenAI
from numpy import dot
from numpy.linalg import norm

def cosine_similarity(vec1, vec2):
    return dot(vec1, vec2) / (norm(vec1) [1] norm(vec2))
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication between the norms.
Dividing one norm by the other instead of multiplying.
2fill in blank
medium

Complete the code to compute the Euclidean distance between two vectors.

Prompt Engineering / GenAI
import numpy as np

def euclidean_distance(vec1, vec2):
    return np.sqrt(np.sum((vec1 - vec2) [1] 2))
Drag options to blanks, or click blank then click option'
A+
B**
C*
D//
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication '*' instead of exponent '**'.
Using integer division '//' which causes errors.
3fill in blank
hard

Fix the error in the code to calculate the Manhattan distance between two vectors.

Prompt Engineering / GenAI
import numpy as np

def manhattan_distance(vec1, vec2):
    return np.sum(np.abs(vec1 [1] vec2))
Drag options to blanks, or click blank then click option'
A/
B+
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction inside the absolute value.
Using multiplication or division which are incorrect here.
4fill in blank
hard

Fill both blanks to create a dictionary of word vectors filtered by length greater than 3.

Prompt Engineering / GenAI
words = ['cat', 'house', 'tree', 'sun']
vectors = {'cat': [1,2], 'house': [3,4], 'tree': [5,6], 'sun': [7,8]}
filtered_vectors = {word: vectors[word] for word in words if [1] [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself instead of its length.
Using less than '<' instead of greater than '>'.
5fill in blank
hard

Fill all three blanks to create a dictionary of words and their vector lengths filtered by length greater than 3.

Prompt Engineering / GenAI
words = ['dog', 'elephant', 'bird', 'ant']
vectors = {'dog': [1,1,1], 'elephant': [2,2,2], 'bird': [3,3,3], 'ant': [4,4,4]}
result = [1]: [2] for word in words if len(word) [3] 3
Drag options to blanks, or click blank then click option'
Aword
Blen(vectors[word])
C>
Dword.upper()
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word as key without uppercase.
Using the word length instead of vector length as value.
Using '<' instead of '>' in the condition.