Linear Algebra Basics
Python · NumPy Library
Theory
NumPy handles vectors and matrices directly. The dot product np.dot(a, b) multiplies matching elements and adds them up: prices [10, 20] and quantities [3, 2] give 70. The @ operator does matrix multiplication: a (2, 3) matrix @ a (3, 4) matrix gives (2, 4), and the inner sizes must match. Note that * is element-wise and is not matrix multiplication.
np.linalg holds the classic tools: np.linalg.inv(A) is the inverse, np.linalg.det(A) the determinant and np.linalg.solve(A, b) solves the system A x = b. For example, if 2 pens and 3 notebooks cost 85 rupees and 4 pens and 1 notebook cost 65, solve gives a pen at 11 and a notebook at 21. solve is faster and more accurate than inv(A) @ b, and raises LinAlgError for a singular matrix.
Why we use it
Linear regression, neural networks, PCA and recommendation systems are all matrix maths underneath. Knowing dot products and matrix multiplication helps you read model code and compute weighted totals in one step.
When to use it
Use @ or np.dot for weighted sums and matrix products, np.linalg.solve for systems of equations and inv only when you really need the inverse. For element-by-element products use * instead.
How it works
- Vectors or matrices
- Element-by-element product wanted? Yes: Use a * b. No: Use a @ b or np.dot.
- Inner dimensions match? Yes: Result has shape (rows of A, cols of B). No: ValueError: shapes not aligned.
- Solving A x = b? Yes: np.linalg.solve(A, b). No: Use the product directly.
- Numeric result
Syntax
np.dot(a, b) a @ b
np.linalg.inv(A) np.linalg.det(A)
np.linalg.solve(A, b)
A.TExample
A = np.array([[2, 3], [4, 1]])
b = np.array([85, 65])
print(np.linalg.solve(A, b)) # [11. 21.]Key points
- * is element-wise; @ is matrix multiplication
- For A @ B the columns of A must equal the rows of B
- np.dot of two 1-D arrays is a single number
- Prefer np.linalg.solve(A, b) over inv(A) @ b
- A determinant of 0 means the matrix has no inverse