Lab 1 of 4about 35 minutes
Search a store by meaning
It uses what you learned in Vectors, Adding and scaling vectors, The dot product and Distance and cosine similarity.
Start here
Run itYour ratings and your friends' from the videos: a comedy first, then a horror movie, each from minus two to plus two. Each friend is one row of friends.
1Every friend's score
Run itFrom The dot product
@ takes the dot product of every row of friends with you, at once. Run the cell.
Max scores 6, more than Sam's 5, and Sam rates exactly like you. Max's arrow is longer: it wins without agreeing more. Keep that in mind.
2Max, by hand
Your turnFrom The dot product
Work out Max's dot product with you yourself, from the numbers you[0], you[1], max_rating[0] and max_rating[1]. Then hold it against what @ gave.
Hint
Multiply the comedy numbers, multiply the horror numbers, then add the two products.
One way to do it
max_rating = friends[3]
by_hand = you[0] * max_rating[0] + you[1] * max_rating[1]
print(by_hand, np.allclose(by_hand, you @ max_rating))3Length and distance
Your turnFrom Distance and cosine similarity
First your own length, by Pythagoras: square each of your numbers, add them, take the square root with np.sqrt. Then every friend's distance from you: np.linalg.norm(..., axis=1) measures the length of each row. Which row is each friend's gap from you?
Hint
friends - you takes you away from every row at once, as NumPy did with arrays in Python for AI.
One way to do it
your_length = np.sqrt(you[0] ** 2 + you[1] ** 2)
distances = np.linalg.norm(friends - you, axis=1)
print(round(float(your_length), 3), np.allclose(your_length, np.linalg.norm(you)))
print(dict(zip(names, distances.round(3).tolist())))4Shrink, then the dot product
Your turnFrom Adding and scaling vectors and Distance and cosine similarity
Jo likes exactly what you like, half as strongly, and still loses to Max by distance and by the dot product. Shrink every arrow to length 1 so only the direction is left. Shrinking is scaling: divide each arrow by its own length. lengths holds each friend's length, one for each row.
Hint
Each friend is divided by the length on its own row, and you by your length from step 3.
One way to do it
lengths = np.linalg.norm(friends, axis=1, keepdims=True)
unit_friends = friends / lengths
unit_you = you / your_length
cosines = unit_friends @ unit_you
print(dict(zip(names, cosines.round(2).tolist())))5The store
Run itFrom Vectors and Distance and cosine similarity
A store has 21 products. A real model, EmbeddingGemma 2, turned each product's line into 768 numbers, its embedding, and five searches too. Run the cell.
Every product has length 1, so all three measures give one order.
The file also holds the first search in 256 numbers. Try products @ np.array(found["short"]["row"]): NumPy stops, ValueError: … (size 256 is different from 768). A search and its products must come from one model, at one size.
6Search by meaning
Your turnFrom The dot product and Distance and cosine similarity
Score every product against the search "My hands are cold outside in winter" with the dot product, and keep the positions of the three best in top. np.argsort(-scores) puts the positions in order, highest score first. Then do the same by distance, where the smallest comes first.
Hint
Every row's dot product with the search is one @. Every product's gap from the search is a product minus the search.
One way to do it
hands = searches["hands"]
scores = products @ hands
top = np.argsort(-scores)[:3]
gaps = np.linalg.norm(products - hands, axis=1)
top_by_distance = np.argsort(gaps)[:3]
print([store["names"][i] for i in top])['Wool gloves, lined with fleece', 'A thick knitted scarf', 'A soft mask for your eyes']7A search mixed from two
On your ownFrom Adding and scaling vectors and The dot product
On your own now. Someone wants a gift for a keen cook who also grows vegetables. Mix the cook search and the garden search, half of each, into mixed. Then keep the names of the three products with the best dot product in top_mixed, a list.
Hint
A mix is a linear combination: scale each search by 0.5, then add them. mixed is shorter than length 1, but every score shrinks by the same amount, so the order stays the same.
One way to do it
mixed = 0.5 * searches["cook"] + 0.5 * searches["garden"]
mixed_scores = products @ mixed
top_mixed = [store["names"][i] for i in np.argsort(-mixed_scores)[:3]]
print(top_mixed)Run on Pyodide 314, Python 3.14 and NumPy 2.4. Checked on .
