Vectors
A list of numbers as a point or an arrow in space.
Why it matters
A computer can't look at a customer, a word or a photo. It can only do sums. So before any AI model can work with something, that thing has to become a list of numbers. That list is called a vector. A customer becomes [orders, average spend, days since last visit]. A word becomes a list of a few thousand numbers that capture its meaning. Once you can picture these lists as arrows, the big ideas later in the course (similarity search, neural networks, attention) stop being mysterious. They are all things you do to arrows.
What you'll be able to do
- See a vector three ways: a list of numbers, a point, and an arrow
- Read vector notation: bold letters, components, dimension
- Turn records like customers or campaigns into feature vectors with a fixed order
- Explain why every input to a model ends up as a vector
What it covers
- Start with a map
- Any number of numbers
- Vectors in Python
- Turning records into vectors
- Worked example: three customers as points
- Why this matters for AI
Start with a map
Imagine giving directions in a city laid out on a grid: "3 blocks east, 1 block north." Those two numbers, in that order, pin down exactly one place. Swap them ("1 east, 3 north") and you end up somewhere else.
That pair of numbers is a vector. A vector is just a list of numbers where the order matters. [3, 1] and [1, 3] are different vectors, the same way the two sets of directions lead to different corners.
You can picture the same vector in three ways:
- A list of numbers, like
[3, 1]. This is how code stores it. - A point: walk 3 across and 1 up, and that spot is the vector.
- An arrow from the starting corner
[0, 0]to that point. An arrow has a direction (which way it points) and a length (how far it goes). Those two things are what make vectors useful for comparing things.
One vector, three views. Drag the tip and watch the list, the point and the arrow change together.
Show the answer
No: the order is part of the meaning, so it leads somewhere else
A vector is an ordered list. Same numbers in a different order is a different vector.
Any number of numbers
Directions on a map need two numbers. But a vector can hold as many as you like. A shop might describe a customer with three: how many orders they placed, how much they spend on average, and how many days since their last visit.
customer = [12, 64.50, 9]
↑ ↑ ↑
orders spend days since last order
The count of numbers in a vector is its dimension. [3, 1] has dimension 2. The customer vector has dimension 3. The word vectors that power AI search often have 1,536 numbers. You can't draw 1,536 directions, but every rule you'll learn works the same way. You practice on flat 2-number vectors you can see, then trust the same rules in bigger spaces.
In plain words: a vector is an ordered list of numbers. Its dimension is how many numbers are in the list. Each position always means the same measurement.
Go deeper How mathematicians write vectors
Math books write a vector as a bold lowercase letter, such as . The numbers inside are its components, written with small numbers underneath:
Read it as: "v is the vector whose first component is 3 and whose second component is 1."
A vector with components is said to live in , pronounced "R n". The stands for "real numbers" (ordinary numbers, decimals included). So [3, 1] is in , a flat plane, and a 1,536-number word vector is in .
Vectors in Python
In Python, the NumPy library stores vectors as arrays. An array with one row of numbers is a vector. Its shape tells you its dimension.
One thing trips everyone up: math counts positions from 1, but Python counts from 0. So the first component, which a math book calls , is v[0] in code.
import numpy as np v = np.array([3, 1]) print(v, v.shape, v.ndim) print("v1 =", v[0], " v2 =", v[1]) customer = np.array([12, 64.50, 9]) # orders, average order value, days since last order print(customer.shape)
shape printed (2,) for the map vector and (3,) for the customer: two numbers and three numbers. ndim is 1 because each is a single row.
customer = [3, 120, 0.5] print(len(customer))
Show the answer
3
len counts the items in the list, and there are 3. A vector with three numbers lives in three dimensions, however large the numbers are.
Show the answer
v[1]
Python counts from 0: v[0] is 8, v[1] is 2, v[2] is 5.
Turning records into vectors
To hand data to a model, you choose which measurements to use (the features) and a fixed order for them. Then every record becomes a vector in that same order. Think of it as a form where each box always asks the same question.
The list of feature names matters as much as the numbers. [12, 64.5, 9] means nothing on its own. With the names beside it, it describes a customer.
import numpy as np import requests FEATURES = ["spend", "clicks", "conversions"] campaigns = requests.get("https://api.gradient.test/v1/campaigns").json()["data"] vectors = {c["id"]: np.array([c[f] for f in FEATURES], dtype=float) for c in campaigns} for cid, v in list(vectors.items())[:3]: print(cid, v)
Every campaign is now a point in a three-direction "campaign space". Campaigns that behave alike sit close together. That one idea, similar things are nearby points, is what search engines, recommendations and AI memory are built on.
Watch out: the features use very different units. Spend runs into thousands of dollars; conversions are in tens. Measured as distances, spend would drown out everything else. Phase 3 shows how to put features on a fair scale.
Worked example: three customers as points
Three customers, described by [orders, average spend]. Which two are most alike? Turn them into vectors and measure the straight-line distance between each pair.
import numpy as np ana = np.array([2, 40.0]) ben = np.array([3, 45.0]) cai = np.array([20, 15.0]) print("ana to ben:", round(float(np.linalg.norm(ana - ben)), 1)) print("ana to cai:", round(float(np.linalg.norm(ana - cai)), 1)) print("ben to cai:", round(float(np.linalg.norm(ben - cai)), 1))
Ana and Ben are close (about 5.1 apart): few orders, similar spend. Cai is far from both: many cheap orders. np.linalg.norm measures how long an arrow is; here it measures the gap between two customers. You'll meet it properly in the next lesson. For now, notice the picture: alike customers are nearby points.
Show the answer
3
Dimension is how many numbers the vector holds: price, rating and stock make 3.
Why this matters for AI
| Thing | As a vector | How many numbers |
|---|---|---|
| A customer in a model that predicts who will leave | orders, spend, days since last visit, ... | tens |
| A word or document "embedding" | numbers that capture meaning | hundreds to thousands |
| A 512 × 512 color photo | the red, green and blue value of every pixel | 786,432 |
| A word inside a language model | its current state | thousands |
The rest of this phase is a toolkit for working with these lists. You'll learn to add and stretch them, measure their length, compare their directions, and transform them. Each operation is one line of Python and a picture you can draw on paper.
Practice what you just read
- 3 code examples you can edit and run
- a practice exercise that checks your code
- a 5-question quiz
- 4 review cards for spaced repetition
Try a quiz question
What's the dimension of an embedding stored as a NumPy array with shape (1536,)?