The np.linalg submodule covers determinants, inverses, eigenvalues, norms, and solving linear systems—foundational for ML and physics simulations.
Common functions
np.linalg.norm(v)— vector/matrix normnp.linalg.det(M)— determinant (square matrices)np.linalg.inv(M)— matrix inversenp.linalg.solve(A, b)— solve Ax = bnp.linalg.eig(M)— eigenvalues and eigenvectors
Solve linear system
import numpy as np
A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
x = np.linalg.solve(A, b)
print(x)
Numerical stability
Prefer solve over computing inverse explicitly. Ill-conditioned matrices need specialized routines in SciPy.
Important interview questions and answers
- Q: When is inverse ill-advised?
A: Large, sparse, or near-singular matrices—inverse is slow and unstable. - Q: norm default?
A: Frobenius norm for matrices; L2 for vectors unless ord specified.
Self-check
- Solve 2×2 system Ax=b with given A and b.
- What function returns eigenvalues?
Pitfall: Prefer solve over computing matrix inverse.
Interview prep
- solve vs inv?
solve Ax=b directly—more stable than inverse.
- det zero?
Singular matrix—no unique inverse.