← All notes
Course chapters
On this page

Lesson 01 · Foundational mathematics

What is a vector?

Computers only understand numbers. How can they tell that a cat and a tiger are more alike than a cat and a goldfish?

1. An awkward question

Imagine you are writing a program that should answer this question:

“Of these three animals — a cat, a tiger and a goldfish — which two are most alike?”

You immediately say: the cat and the tiger. Your reasons might be “they are both felines”, “they look alike”, or “they both pounce on prey”. But here is the problem: a computer does not understand “feline” or “look alike”. It only understands numbers. You cannot put a cat inside a CPU; you can only give it numbers.

Pause and think

If you could only use numbers to describe an animal to a computer, how would you do it? Before scrolling down, really think about it for 30 seconds.

I've thought about it — show the answerHide answer

A natural idea is to choose a measurable attribute and turn each animal into a number. For example, “size”: a goldfish is small, a cat is not very big, and a tiger is large. Once we have numbers, we can subtract them; once we can subtract them, we can compare “which animals are closer together”. Let us follow this line of thinking.

2. The simplest solution: one number

Give each animal a “size score” between 0 and 1. A goldfish scores 0.020.02, a cat 0.250.25, a dog 0.450.45, a crocodile 0.500.50 and a tiger 0.900.90. Put them on a number line:

Size →00.51Goldfish0.02Cat0.25Dog0.45Crocodile0.50Tiger0.90
Figure 1-1. Each animal becomes a single size score on a number line. Notice how close the dog and crocodile are.

Something remarkable happens: “how alike are they?” becomes something we can calculate. The smaller the difference between two numbers, the more “alike” the animals are:

d(cat,tiger)=∣0.25−0.90∣=0.65d(cat,goldfish)=∣0.25−0.02∣=0.23\begin{aligned} d(\text{cat},\text{tiger}) &= |0.25-0.90| = 0.65 \\ d(\text{cat},\text{goldfish}) &= |0.25-0.02| = 0.23 \end{aligned}

Wait — according to this algorithm, the cat and the goldfish are more alike? Something seems wrong, but do not throw the idea out just yet. This approach has done at least one important thing right: it turned “comparing two things” into “comparing two numbers”.

We want to keep that idea. The real problem lies elsewhere.

3. When a dog resembles a crocodile

Look again at the dog and the crocodile in Figure 1-1. According to our algorithm:

d(dog,crocodile)=∣0.45−0.50∣=0.05d(\text{dog},\text{crocodile}) = |0.45-0.50| = 0.05

That is the smallest distance on the entire number line.

The limitation of this approach. In a world of “one number”, the dog and the crocodile are a perfect match — simply because they have similar weights. When we compress an animal into a single number, we lose too much information: temperament, habits, how dangerous it is… all of it disappears.

Pause and think

Without giving up the idea of describing things with numbers, what is the smallest improvement you can make?

I've thought about it — show the answerHide answer

If one number is not enough, use two. Keep the size score and add a “ferocity score”: a dog is gentle, 0.200.20; a crocodile is fierce, 0.900.90. Once each animal has two numbers, it is no longer a point on a number line, but a point on a plane.

4. From a number line to a plane

Give each animal two numbers, (size,ferocity)(\text{size},\text{ferocity}). Use the first as its horizontal coordinate and the second as its vertical coordinate:

Size →Ferocity ↑
d≈0.70d\approx0.70
Goldfish (0.02, 0.02)Cat (0.25, 0.30)Dog (0.45, 0.20)Crocodile (0.50, 0.90)Wolf (0.55, 0.75)Tiger (0.90, 0.85)
Figure 1-2. A second dimension pulls the dog and crocodile apart. Wolves, tigers and crocodiles form a group of fierce animals in the upper-right region.

The dog and the crocodile are still close together on the horizontal axis, but the vertical axis pulls them apart. Using the Pythagorean theorem — we will leave the details for the next lesson — their distance changes from 0.050.05 to approximately 0.700.70. Problem solved.

Now notice what you have just done: you have written an animal as an ordered collection of numbers:

vdog=[0.45, 0.20](size, ferocity)\mathbf{v}_{\text{dog}} = [0.45,\,0.20] \qquad \text{(size, ferocity)}

In mathematics, this collection of numbers is called a vector.

The number of values is called its dimensionality. What we have just created is a two-dimensional vector. The same vector has three faces, all describing the same thing: a list of numbers (what the computer sees), a point on a plane (what we draw), and an arrow from the origin to that point (a particularly useful view for the operations in the next lesson).

Try it yourself: the animal map. Drag the “?” and see which animal is closest.

Animal map — move the mystery animal

Swipe across the diagram to see all animals, or use the sliders below.

Size →Ferocity ↑GoldfishCatDogCrocodileWolfTiger?

Nearest animal: Cat · Distance: 0.18

The question mark is a mystery animal. Its position declares its two numbers. Similarity is the distance between two points.

5. Keep adding dimensions

Two dimensions have limitations too. How do you distinguish a cat from a fox if their size and ferocity are similar? Add another dimension: “is it domesticated?” What about animals that fly or swim? Add more. Whenever you find something you cannot distinguish, add another dimension. Three numbers describe a point in three-dimensional space. With four numbers or a hundred, we can no longer draw it, but mathematics does not mind: the distance formula still works, and we can still compare which things are more alike.

This is exactly what modern AI does. In large language models such as GPT, each word is represented as a vector — with not two dimensions, but thousands (GPT-3 used 12,288). Nobody manually assigns meanings such as “size” and “ferocity” to each dimension. The model learns these dimensions from enormous amounts of text. But the idea is the same as the one you have developed today:

The takeaway. A vector is a “digital photograph” of something: an ordered collection of numbers captures its features, turning the vague intuition of “how alike are they?” into a distance we can calculate.

What you have built in this lesson:

  • Vector: an ordered collection of numbers, such as [0.45,0.20][0.45, 0.20].
  • Dimensionality: the number of values; each dimension is a way of looking at something.
  • The three faces of a vector: a list of numbers, a point in space, and an arrow starting at the origin.
  • Similarity as distance: the closer two vectors are, the more alike the things they represent.

But how exactly should we calculate “distance”? Apart from distance, are there other ways to measure similarity? What does adding two vectors mean — what kind of animal would “cat + dog” be? Take these questions with you to the next lesson.

Check your understanding

Now that you have finished the lesson, check the main ideas. Choose an answer and select “Submit answer” to see the correct option and an explanation.

1. Computers work with numbers. What is the fundamental purpose of using vectors to represent things such as cats and tigers?
2. Which statement about a vector as a point in space, or an arrow, is correct?