Vectors and Matrices

Updated · 5 min read
mediumPython 3.13aimathlinear-algebranumpyvectors

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

One operation computes every total
OperationSyntaxDescription
Dot producta @ b (vectors)It is used to multiply matching numbers and add them: one number
Matrix × vectorM @ vIt is used to take a dot product of each row with the vector
Matrix × matrixA @ BIt is used to combine two transformations; inner sizes must match
TransposeM.TIt is used to swap rows and columns
ShapeM.shapeIt is used to check rows and columns before multiplying

Example

Python
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)
Output
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 ideaLinear algebra
A data tableA matrix: one row per example, one column per feature
An embeddingA vector, compared with dot products (see "Similarity and Cosine Distance")
A network layerA weight matrix times an input vector
A batch of inputsA 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:

  1. What does a dot product return?
  2. Why can a (3, 3) matrix multiply a vector of length 3?
  3. How is a network layer a matrix multiplication?

Quick check: vectors and matrices

4 questions
1What is Srihitha's total, [1, 2, 1] @ [45, 30, 15]?
2What is the shape of a (2, 3) matrix times a vector of length 3?
3What does * do with two NumPy arrays?
4What does M.T do?

Key Takeaways

Quick reference

NeedCode
Dot producta @ b
Matrix × vectorM @ v
Element by elementa * b
TransposeM.T
ShapeM.shape

Was this tutorial helpful?

Lesson 116 of 186