NumPy Linear Algebra Cheat Sheet
Reference for NumPy matrix operations, linear algebra functions like inverse, determinant, eigenvalues, and SVD, plus array broadcasting rules.
2 PagesIntermediateMar 10, 2026
Array & Matrix Basics
Create and inspect arrays used as vectors and matrices.
python
import numpy as npA = np.array([[1, 2], [3, 4]])B = np.array([[5, 6], [7, 8]])A.T # transposeA.shape # (2, 2)np.zeros((3, 3))np.eye(3) # identity matrixnp.ones((2, 3))
Core linalg Operations
Matrix multiplication, inverse, determinant, eigenvalues, and solving systems.
python
A = np.array([[4, 2], [1, 3]])np.dot(A, B) # matrix multiplicationA @ B # matrix multiplication (preferred)np.linalg.inv(A) # matrix inversenp.linalg.det(A) # determinantnp.linalg.matrix_rank(A) # rankeigvals, eigvecs = np.linalg.eig(A) # eigenvalues/eigenvectors# Solve Ax = bb = np.array([1, 2])x = np.linalg.solve(A, b)# Singular Value DecompositionU, S, Vt = np.linalg.svd(A)np.linalg.norm(A) # Frobenius normnp.linalg.norm(b, ord=2) # L2 (Euclidean) norm
Broadcasting
Apply operations across arrays of different but compatible shapes.
python
a = np.array([1, 2, 3]) # shape (3,)b = np.array([[1], [2], [3]]) # shape (3, 1)a + b # broadcasts to shape (3, 3)M = np.random.rand(4, 3)row_means = M.mean(axis=1, keepdims=True) # shape (4, 1)centered = M - row_means # broadcasts across columns
Key Functions
The linalg functions you'll reach for most often.
- np.dot / @- Matrix multiplication; use @ for readability with 2D+ arrays
- np.linalg.inv- Computes the matrix inverse; raises LinAlgError for singular matrices
- np.linalg.solve- Solves Ax = b directly, more stable and faster than computing inv(A) @ b
- np.linalg.eig- Returns eigenvalues and eigenvectors of a square matrix
- np.linalg.svd- Singular Value Decomposition, used in PCA and dimensionality reduction
- broadcasting- NumPy's rule for applying operations across arrays of compatible but different shapes without copying data
- np.linalg.norm- Computes vector/matrix norms (L1, L2, Frobenius) for magnitude or regularization calculations
Pro Tip
Prefer np.linalg.solve(A, b) over np.linalg.inv(A) @ b - solving the linear system directly is both faster and numerically more stable than explicitly computing the matrix inverse.
Was this cheat sheet helpful?
Previous
Pandas Advanced (GroupBy & Pivot Tables) Cheat Sheet
Next
Data Wrangling with Pandas Cheat Sheet
Explore Topics
#NumPyLinearAlgebra#NumPyLinearAlgebraCheatSheet#DataScience#Intermediate#ArrayMatrixBasics#CoreLinalgOperations#Broadcasting#KeyFunctions#Functions#DataStructures#MachineLearning#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance