Matrix Operations — Core Concepts
What is a matrix in Python?
A matrix is a two-dimensional array of numbers arranged in rows and columns. In Python, the standard tool for matrix work is NumPy’s ndarray. While Python has nested lists, they lack the speed and convenience needed for real computation.
NumPy stores matrices in contiguous memory blocks and delegates arithmetic to compiled C and Fortran routines. The result: operations on a 1000×1000 matrix that would take seconds with Python loops finish in milliseconds with NumPy.
Creating matrices
NumPy provides several constructors:
np.array([[1, 2], [3, 4]])— from a list of listsnp.zeros((3, 4))— a 3×4 grid of zerosnp.ones((2, 2))— all onesnp.eye(5)— 5×5 identity matrix (ones on the diagonal)np.random.randn(3, 3)— random values from a normal distribution
Element-wise vs matrix operations
This is where beginners trip up. The * operator in NumPy does element-wise multiplication — each cell multiplied by the corresponding cell. True matrix multiplication uses the @ operator or np.matmul.
| Operation | Syntax | What it does |
|---|---|---|
| Element-wise add | A + B | Adds matching cells |
| Element-wise multiply | A * B | Multiplies matching cells |
| Matrix multiply | A @ B | Dot product of rows and columns |
| Transpose | A.T | Swaps rows and columns |
| Inverse | np.linalg.inv(A) | Finds A⁻¹ such that A @ A⁻¹ = I |
Matrix multiplication explained
Matrix multiplication combines two matrices by computing dot products. For A (m×n) and B (n×p), the result C is (m×p), where each entry C[i][j] is the sum of A’s row i multiplied element-by-element with B’s column j.
The shapes must be compatible: the number of columns in A must equal the number of rows in B. If they do not match, NumPy raises a ValueError.
Transposition
Transposing a matrix flips it along its diagonal — row indices become column indices and vice versa. A 3×5 matrix becomes 5×3 after transposition. This operation appears constantly in data science (switching between row-oriented and column-oriented data) and in mathematical formulas.
Inverse and determinant
The inverse of a square matrix A is the matrix A⁻¹ such that A @ A⁻¹ equals the identity matrix. Not every matrix has an inverse — if the determinant is zero, the matrix is singular and cannot be inverted. NumPy raises LinAlgError when you try.
The determinant, computed with np.linalg.det(A), measures how much a matrix scales space. A determinant of zero means the matrix collapses at least one dimension.
Broadcasting
When operating on matrices of different shapes, NumPy applies broadcasting rules: it stretches the smaller array to match the larger one where possible. A common example is subtracting a row mean from every row — you compute the mean as a column vector and let broadcasting handle the rest.
Common misconception
Many beginners assume np.dot and @ are always identical. For 2D arrays they are, but np.dot behaves differently on higher-dimensional arrays (it sums over the last axis of the first array and the second-to-last of the second). Stick with @ for clarity when working with matrices.
One thing to remember: The @ operator is your primary tool for matrix multiplication in modern Python — learn its shape rules and you can express surprisingly complex math in a single line.
See Also
- Python Bayesian Inference How updating your beliefs with new evidence works — and why it helps computers make smarter guesses.
- Python Convolution Operations The sliding-window trick that lets computers sharpen photos, recognize faces, and hear words in noisy audio.
- Python Fourier Transforms How breaking any sound, image, or signal into simple waves reveals hidden patterns invisible to the naked eye.
- Python Genetic Algorithms How computers borrow evolution's playbook — survival of the fittest, mutation, and reproduction — to solve problems too complicated for brute force.
- Python Linear Algebra Numpy Why solving puzzles with rows and columns of numbers is the secret engine behind search engines, video games, and AI.