The math toolkit: vectors and matrices
Describe things as lists of numbers, compare them with the dot product, and multiply matrices - the arithmetic AI runs on.
- Represent a real-world thing as a vector of features
- Compute and interpret the dot product
- Multiply a matrix by a vector as a batch of dot products
Computers can’t see a house, a song or a sentence. They see numbers. So the first step in almost every AI system is to turn each thing into a vector: an ordered list of numbers.
A house might become : 1800 square feet, 3 bedrooms, 2 bathrooms, 12 years old. Each position always means the same thing, so two houses can be compared number by number. Each number is a feature, and a vector with 4 numbers lives in 4-dimensional space. Real AI vectors often have hundreds or thousands of dimensions; we draw 2-D ones because we can see them.
Vectors add position by position. Drawn as arrows, that means putting them tip to tail:
The dot product
The most important operation in all of AI is the dot product: multiply matching positions, then add everything up.
For and : . The result is a single number that measures how much two vectors point the same way: large and positive when they agree, zero when they’re at right angles, negative when they point in opposite directions.
Try it
Drag a vector
Drag the tip of vector a; vector b stays fixed at (4, 1). Make the dot product as large as you can, then make it exactly 0, then negative. Watch the angle between the arrows each time: what angle gives 0?
1def dot(a, b):
2 return sum(x * y for x, y in zip(a, b))
3
4print(dot([3, 4], [4, 1]))
5print(dot([1, 0], [0, 1]))
6print(dot([2, 3], [-2, -3]))16 0 -13
Matrices: many vectors at once
A matrix is a grid of numbers - a stack of vectors. A spreadsheet of 100 houses with 4 features each is a 100 × 4 matrix; a grayscale photo is a matrix of pixel brightnesses.
Multiplying a matrix by a vector is just one dot product per row:
That is how a model scores a whole batch of examples at once: each row is one example, the vector holds the weights, and out come all the scores. Graphics chips (GPUs) do millions of these multiply-and-adds in parallel, which is why they made deep learning practical.
1houses = [
2 [1800, 3, 2],
3 [950, 2, 1],
4 [2400, 4, 3],
5]
6# Price weights learned by some model: dollars per square foot, per bedroom, per bathroom.
7weights = [150, 10000, 5000]
8
9for house in houses:
10 price = sum(feature * weight for feature, weight in zip(house, weights))
11 print(house, "->", price)[1800, 3, 2] -> 310000 [950, 2, 1] -> 167500 [2400, 4, 3] -> 415000
Key takeaways
AI turns everything into vectors: ordered lists of numbers, one per feature.
The dot product measures how much two vectors agree: positive, zero or negative.
A matrix is a grid of numbers; matrix × vector is one dot product per row - a whole batch of predictions at once.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Python
Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.
Compute a dot product
Read two lines, each a vector of space-separated numbers of the same length. Print their dot product, then same direction, perpendicular or opposite direction depending on whether it is positive, zero or negative.
- Agreeing
- Right angle
- Opposite
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Score a batch with a matrix
The first line holds the model’s weights. Every following line is one example (a row of the matrix) with the same number of features. Print each example’s score - the dot product of the row with the weights - one per line.
- Two by two
- House prices
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…