Vectors and Matrices
A vector is an ordered list of numbers, such as the fees for three items or the 384 numbers of an embedding. A matrix is a grid of numbers with rows and columns, such as many students' orders, or the weights of a neural network layer. Linear algebra is the math of combining them, and it is how models process large amounts of data in a few fast operations.
Dot Products and Matrix Multiplication
| Operation | Syntax | Description |
|---|---|---|
| Dot product | a @ b (vectors) | It is used to multiply matching numbers and add them: one number |
| Matrix × vector | M @ v | It is used to take a dot product of each row with the vector |
| Matrix × matrix | A @ B | It is used to combine two transformations; inner sizes must match |
| Transpose | M.T | It is used to swap rows and columns |
| Shape | M.shape | It is used to check rows and columns before multiplying |
Example
import numpy as np
prices = np.array([45, 30, 15]) # lab, field trip, library fees
srihitha = np.array([1, 2, 1]) # how many of each she pays for
print("Dot product, her total: $", srihitha @ prices)
orders = np.array([[1, 2, 1], # Srihitha
[1, 0, 1], # Advitha
[0, 3, 0]]) # Navika
print("Matrix @ vector, every total:", orders @ prices)
print("Shapes:", orders.shape, "@", prices.shape, "->", (orders @ prices).shape)
# A layer of a neural network is a matrix multiplication: 3 inputs -> 2 outputs
x = np.array([1.0, 0.5, -1.0])
W = np.array([[0.2, -0.4, 0.1],
[0.7, 0.3, -0.5]])
print("Layer output W @ x:", W @ x)
print("Transpose shape:", W.T.shape)Dot product, her total: $ 120
Matrix @ vector, every total: [120 60 90]
Shapes: (3, 3) @ (3,) -> (3,)
Layer output W @ x: [-0.1 1.35]
Transpose shape: (3, 2)Explanation: The dot product multiplies Srihitha's quantities by the prices and adds them: 45 + 60 + 15 = $120. Putting all three sisters' orders in a matrix gives every total in one operation. In a neural network, W @ x turns 3 inputs into 2 outputs: each output is a dot product of one row of weights with the inputs, such as 0.2 × 1 − 0.4 × 0.5 + 0.1 × (−1) = −0.1.
Where You Meet Them in ML
| ML idea | Linear algebra |
|---|---|
| A data table | A matrix: one row per example, one column per feature |
| An embedding | A vector, compared with dot products (see "Similarity and Cosine Distance") |
| A network layer | A weight matrix times an input vector |
| A batch of inputs | A matrix, processed in one multiplication |
Common Mistakes
Using * instead of @. * multiplies element by element; @ is matrix multiplication.
Ignoring shapes. Check .shape before and after each operation.
Thinking the order doesn't matter. A @ B and B @ A are usually different, or impossible.
Interview Corner
What is a dot product, and where is it used in ML?
The sum of the products of matching entries of two vectors. It is used in every network layer, in linear models, and to compare embeddings.
When can two matrices be multiplied?
When the number of columns in the first equals the number of rows in the second; an (m, n) matrix times an (n, p) matrix gives an (m, p) matrix.
Practice
Exercise: add a fee. Add a fourth item, sports at $60, to prices, and a fourth column to orders (Srihitha 1, Advitha 0, Navika 1). Predict each total by hand first, then run the code.
Check your understanding:
- What does a dot product return?
- Why can a (3, 3) matrix multiply a vector of length 3?
- How is a network layer a matrix multiplication?
Key Takeaways
Quick reference
| Need | Code |
|---|---|
| Dot product | a @ b |
| Matrix × vector | M @ v |
| Element by element | a * b |
| Transpose | M.T |
| Shape | M.shape |
Was this tutorial helpful?