Linear algebra for AI: vectors and matrices
Computers can't read words or photos directly: everything is turned into numbers arranged in lists and tables. Linear algebra is the maths of those lists (vectors) and tables (matrices). Almost every AI calculation, from a product recommendation to ChatGPT's next word, is mostly matrices being multiplied very fast on GPUs.
Everyday analogy
An Excel sheet. One cell is a single number (a scalar), one row is a vector, and the whole sheet is a matrix. An AI model is a giant spreadsheet calculation, repeated billions of times.
Example
One member's record as a vector: [age 42, years insured 6, claims last year 3, premium 4,800]. Stack 10,000 members together and you have a 10,000 × 4 matrix: 10,000 rows, 4 columns.
The four building blocks
| Name | What it is | Example | In AI |
|---|---|---|---|
| Scalar | A single number | 5 | A learning rate, a bias |
| Vector | A list of numbers (1 dimension) | [42, 6, 3] | One customer; one word's embedding |
| Matrix | A table of rows × columns (2 dimensions) | 3 rows × 4 columns | A data set; the weights of one layer |
| Tensor | Numbers in 3 or more dimensions | A colour photo: height × width × 3 colours | Images, video, batches of data |
Operations you'll meet
| Operation | What it does | Example |
|---|---|---|
| Addition | Adds matching positions; sizes must match | [1, 2] + [3, 4] = [4, 6] |
| Scalar multiplication | Multiplies every entry by one number | 2 × [1, 3] = [2, 6] |
| Dot product | Multiplies matching positions and adds them up, giving one number | [1, 2, 3] · [4, 5, 6] = 4 + 10 + 18 = 32 |
| Matrix multiplication | Rows of the first × columns of the second; inner sizes must match | (2 × 3) times (3 × 2) gives a 2 × 2 result |
| Transpose | Swaps rows and columns | A 2 × 3 matrix becomes 3 × 2 |
| Identity matrix | 1s on the diagonal, 0s elsewhere; multiplying by it changes nothing | Works like the number 1 |
| Inverse | The "undo" matrix: A × A⁻¹ gives the identity | Used to solve systems of equations |
| Length (norm) | How long a vector is | [3, 4] has length √(9 + 16) = 5 |
Where AI uses it
| In AI | The linear algebra behind it |
|---|---|
| One neuron | A dot product: inputs · weights + bias |
| A whole layer | One matrix multiplication handles every neuron at once |
| Search by meaning (RAG, recommendations) | Cosine similarity: the dot product divided by the two lengths, measuring how closely two vectors point the same way |
| Linear regression | Solving a system of equations, written as y = Xβ |
| Shrinking data (PCA) | Eigenvectors find the directions where the data spreads most; keep the top few |
Spending on [groceries, travel, health]
Customer A = [2, 0, 1]
Customer B = [4, 0, 2] (same habits, twice the size)
Customer C = [0, 3, 0] (only travels)
A · B = 2×4 + 0×0 + 1×2 = 10
|A| = √5 ≈ 2.24 |B| = √20 ≈ 4.47
cos(A, B) = 10 ÷ (2.24 × 4.47) = 1.0 → identical pattern
A · C = 0 → cos(A, C) = 0 → nothing in commonEigenvectors and PCA, in plain words
An eigenvector of a matrix is a direction that the matrix only stretches or shrinks, without turning. The eigenvalue says by how much. In PCA (principal component analysis), the eigenvectors of the data's covariance matrix are the directions where the data varies most, and the biggest eigenvalues show which directions matter. Keeping the top few lets you squeeze, say, 100 columns into 10 with little information lost. More in Dimensionality reduction.
You don't calculate these by hand. Libraries such as NumPy and PyTorch, running on GPUs, do it. What matters is knowing what each operation means, so the jargon in AI articles makes sense.
Try it yourself
About 10 minDo linear algebra in a spreadsheet
- In Excel or Google Sheets, type 2, 0, 1 in A1:C1 and 4, 0, 2 in A2:C2.
- In D1 type
=SUMPRODUCT(A1:C1,A2:C2)to get the dot product. - Try
=MMULTon two small tables to multiply matrices.
What you should see: The dot product is 10, the same number as the worked example. You've just done the core calculation inside every neuron.
Use any free AI chatbot (ChatGPT, Claude, Gemini or Copilot) where needed. Never paste private or confidential information.