Vectors and Matrices

What

A vector is an ordered list of numbers. A matrix is a 2D grid of numbers (rows × columns).

vector: [3, 1, 4]          shape: (3,)
matrix: [[1, 2],            shape: (2, 3)
         [3, 4],
         [5, 6]]

Why it matters

  • A single data point (row) = vector of features
  • A dataset = matrix where rows are samples, columns are features
  • Model weights = vectors or matrices
  • Images = 3D matrices (height × width × channels)

Everything in ML is matrix math under the hood.

Key ideas

  • Shape — (rows, cols). Knowing shapes is 80% of debugging ML code
  • Transpose — flip rows and columns. Shape (m, n) → (n, m)
  • Element-wise ops — add, subtract, multiply matching elements
  • Broadcasting — NumPy’s rule for ops on different shapes (smaller array stretches)

In NumPy

import numpy as np
 
v = np.array([3, 1, 4])           # vector
M = np.array([[1, 2], [3, 4]])    # 2x2 matrix
 
M.shape        # (2, 2)
M.T            # transpose
M + M          # element-wise addition
M * 2          # scalar multiplication