Loading
0x10Lesson 2 of 18

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.

20 min 6-question quiz 2 code exercises
By the end of this lesson you can
  • 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,3,2,12)(1800, 3, 2, 12): 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:

aba + b
Vector addition, tip-to-tail: a = (4, 2), b = (2, 3), a + b = (6, 5).

The dot product

The most important operation in all of AI is the dot product: multiply matching positions, then add everything up.

a⋅b=a1b1+a2b2+⋯+anbna \cdot b = a_1 b_1 + a_2 b_2 + \dots + a_n b_n

For a=(3,4)a = (3, 4) and b=(4,1)b = (4, 1): 3×4+4×1=163 \times 4 + 4 \times 1 = 16. 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

Up to 15 XP

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?

ba
a = (3, 4)
b = (4, 1)
|a| = 5.00
a · b = 3×4 + 4×1 = 16
angle between a and b ≈ 39°
Positive dot product: the vectors point in broadly the same direction.
dot_product.py
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]))
Output
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:

[1234][56]=[1⋅5+2⋅63⋅5+4⋅6]=[1739]\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \begin{bmatrix} 5 \\ 6 \end{bmatrix} = \begin{bmatrix} 1 \cdot 5 + 2 \cdot 6 \\ 3 \cdot 5 + 4 \cdot 6 \end{bmatrix} = \begin{bmatrix} 17 \\ 39 \end{bmatrix}

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.

matrix_times_vector.py
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)
Output
[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 a⋅b=∑iaibia \cdot b = \sum_i a_i b_i 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.

Exercise 1

Compute a dot product

+25 XP

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
main.py
Loading editor…

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.

Exercise 2

Score a batch with a matrix

+25 XP

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
main.py
Loading editor…

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…