Lesson 1 of 118 min
Vectors
A vector is a list of numbers in order: drawn as an arrow it is a move, as a point it places a thing.
After this lesson you can read a vector as a point, as an arrow and as a row of data.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
A row of a table is a vector
The penguins come from the palmerpenguins project, as a CSV file on GitHub. Pick the four measurement columns, take the first row, and turn it into a NumPy array. That array is the vector from the video: four numbers in order, one side of four.
import pandas as pd
url = "https://raw.githubusercontent.com/allisonhorst/palmerpenguins/main/inst/extdata/penguins.csv"
df = pd.read_csv(url)
cols = ["bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g"]
first = df[cols].iloc[0].to_numpy()
print(first)
print(first.shape)[ 39.1 18.7 181. 3750. ] (4,)
Drop the rows with gaps
Two of the 344 penguins were never measured. Their numbers are NaN, "not a number", and a vector with a NaN in it spoils every sum it goes into. So real code drops those rows first. That leaves the 342 points of the video.
print(len(df))
df = df.dropna(subset=cols)
print(len(df))344 342
Each kind's middle
In the video, a new penguin at [46, 15] landed among the Gentoos, whose middle is [47.5, 15.0]. This is where the middles come from: group the rows by kind, and take the mean of each column.
middles = df.groupby("species")[["bill_length_mm", "bill_depth_mm"]].mean()
print(middles.round(1))bill_length_mm bill_depth_mm species Adelie 38.8 18.3 Chinstrap 48.8 18.4 Gentoo 47.5 15.0
The prompt, and one row of the table
Real code adds one thing to the video's: prompt_name, which tells EmbeddingGemma 2 you want to compare meanings, as its model card asks. The model is from Google, free under the Apache 2.0 license. vectors[0] is the table's first row: the cat sentence's embedding, with the video's first four numbers.
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("google/embeddinggemma-2")
vectors = model.encode(
["My cat is asleep on the sofa.", "A kitten naps on the couch.", "My car is parked on the street."],
prompt_name="SentenceSimilarity",
)
print(vectors.shape)
print(vectors[0][:4].round(3))(3, 768) [-0.013 -0.051 -0.007 -0.021]
Close, number by number
The video's average gap: take one list away from the other, drop the minus signs with abs, and take the mean. The kitten sentence's gap is about half the car sentence's. With short sentences it does not always go this way: "I am very hungry." comes out nearer "I am very tired." than "I could eat a horse."
cat, kitten, car = vectors
print(abs(cat - kitten).mean().round(4))
print(abs(cat - car).mean().round(4))0.0093 0.0172
Run on Python 3.13, pandas 3.1.1, NumPy 2.5.5 and sentence-transformers 6.3.0. Checked on .
Quiz
Two questions on this lesson. Answer as often as you like.
1 of 2
A model turns "My cat is asleep on the sofa." into a list of numbers. Whose list is closer to it: "A kitten naps on the couch." or "My car is parked on the street."?
2 of 2
A walk is written east first, then north: [3, 2] is three blocks east and two north. Where does the walk [1, -2] end?
The ideas in this lesson
- Vector
- A list of numbers in order, drawn as an arrow for a move or a point for a place.
- Dimensions
- Each number in a vector is one direction, and how many numbers it has is its dimensions.
- Embedding
- The vector a model makes for a sentence or a photo, with close numbers for alike meanings.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
Linear Algebra for AI
What math is really going on inside an AI model? Mostly, multiplying numbers and adding them up. Hi, I'm Mohi Fayden. That math is called linear algebra. It's about lists of numbers, and tables of them. This course shows it in pictures. No proofs. By the end, you'll see how AI turns words into numbers. And what one layer of a network does with them. To start, you need school math and a little Python. Every lesson has its own page on faydenai.com, with a quiz. There's a lab every few lessons, and a project at the end.
The puzzle
Here's a puzzle. Take this sentence. My cat is asleep on the sofa. And two more. A kitten naps on the couch. My car is parked on the street. The car sentence shares four words with the first. The kitten sentence shares only two. An AI model turns each sentence into a list of numbers. Which list is closer to the first: the car's or the kitten's?
A walk
Keep your guess.
First: what is a list like that?
How do you write down a walk across town, with numbers? Say you walk three blocks east, then two blocks north. That's two numbers, in order: three, two. The first number is always east. The second is always north.
The arrow
Now draw one arrow, from where you start to where you end. A list of numbers, drawn like this... is called a vector.
So what if you swap the two numbers? Two, three.
Same corner?
No. Two east, three north: another corner.
Each place in the list has its own direction.
A minus sign
And a minus sign? Where does minus two, one take you?
Two blocks west, then one north.
A minus sign turns that direction around. So a minus on the second number would go south.
A point
Do you always need the whole arrow? No.
Keep only where the walk ends: a point, at three, two. An arrow, when the vector is a move. A point, when it places a thing. One list... two pictures.
Penguins
But what if the numbers aren't east and north? Here's a table of real penguins, measured in Antarctica. The first penguin's bill is about thirty-nine millimeters long. From top to bottom, it's about nineteen deep. Put bill length across and bill depth up. The penguin is a point. And every other penguin in the table is a point too.
Close numbers
What do you see?
Three groups: three kinds of penguin, Adelie, Chinstrap and Gentoo. And nobody told the picture which penguin is which kind.
Penguins of one kind have close numbers. So their points land close.
Here's a new penguin. Its bill is forty-six long, and fifteen deep. Where does it land?
Among the Gentoos, whose bills are long but not very deep.
Forty-six is near their forty-seven and a half, and fifteen is near their fifteen.
Close numbers, close points.
A third number
What about the other numbers in the row?
The first penguin's flipper is a hundred and eighty-one millimeters long.
One more number is one more direction.
The page tips back into a space, and every point rises by its flipper.
How many numbers a vector has... is called its dimensions.
This one has three.
A row of data
And the fourth number? It's the penguin's weight, in grams.
No picture has a fourth direction, but the list works just the same. In NumPy, it's an array with one side of four. The whole row is a vector with four dimensions. A row of data... is a vector.
Back to the sentences
Four numbers already have no picture. A model that reads sentences uses hundreds. So, back to the cat and the kitten.
An embedding
How does an AI model hold a sentence?
A model from Google reads: My cat is asleep on the sofa. It gives back seven hundred and sixty-eight numbers. So it's a vector with that many dimensions: like the penguin's row, only longer. Here are the first four. A vector like this, made by a model for a sentence... is called an embedding.
The answer
So, whose numbers are closer? Close means what it meant for the penguins: each number near the other's.
Here are the kitten sentence's first four numbers, under the cat's.
And the car's.
The kitten's numbers are closer.
And it holds for all the numbers, not just these four.
Number by number, the gap to the kitten sentence is about half the gap to the car sentence.
If you said the car, you counted shared words, the way a simple word search does.
This model learned, from many examples, to give sentences that mean alike... close numbers.
A photo
And a photo?
The same model gives a photo seven hundred and sixty-eight numbers too.
Here's one: a cat asleep on a sofa.
Its numbers are closer to the cat sentences. Not the car.
So a search can find a photo from the words you type. It looks for the closest numbers.
In code: a table of embeddings
Here's the model in real code. These two lines load it from Google. Then we give it all three sentences at once. What comes back?
One table of numbers. Three rows, one for each sentence, and seven hundred and sixty-eight numbers in each row. NumPy's shape says it: three, seven hundred and sixty-eight.
Each row is one sentence's embedding. A search app that finds by meaning does this once for everything it holds: every page, every product. It keeps the whole table. A store with ten thousand products keeps ten thousand rows. Then, when you type, it looks for the rows closest to yours.
One list, three pictures
So here's what you saw. A walk is two numbers, drawn as an arrow. A penguin is a point, and its whole row is a vector with four dimensions. And a model turns a sentence or a photo into an embedding: hundreds of numbers, close when the meanings are close.
A list of numbers
So a vector is a list of numbers, in order. And AI turns sentences and photos into lists like this. Now try it on this lesson's page, and take the quiz. Swap the first penguin's numbers. Where does it land?
You use this lesson in Lab 1: Search a store by meaning, after Distance and cosine similarity.
Facts last checked on .
