SciPy Linear Algebra
Linear Algebra with SciPy
SciPy's linalg module provides comprehensive linear algebra functionality built on top of NumPy. It includes routines for matrix operations, decompositions, solving linear systems, and much more.
Key Features Covered:
- Matrix Operations: Multiplication, inversion, transpose
- Linear Systems: Solving Ax = b equations
- Matrix Decompositions: LU, Cholesky, Eigen, SVD
- Advanced Operations: Determinants, norms, pseudoinverse
- Practical Applications: Linear regression, data analysis
linalg is more comprehensive than NumPy's linear algebra module and includes additional advanced functionalities.1. Basic Matrix Operations
Fundamental matrix operations including multiplication, inversion, and transpose.
Key Functions:
linalg.inv()- Matrix inversenp.dot()- Matrix multiplication.T- Matrix transpose*- Element-wise multiplication
Applications:
- Transformations
- Coordinate systems
- Computer graphics
2. Solving Linear Systems
Solve systems of linear equations using linalg.solve() and related functions.
Methods for Solving Linear Systems:
- linalg.solve() - General solver (recommended)
- linalg.inv() - Using matrix inverse (less efficient)
- linalg.lstsq() - Least squares solution
- Decomposition methods - LU, Cholesky, QR
3. Matrix Decompositions
Matrix decompositions break matrices into simpler components for efficient computation and analysis.
LU Decomposition:
- A = P × L × U
- Solving linear systems
- Matrix inversion
Cholesky Decomposition:
- A = L × Lᵀ
- Symmetric matrices only
- Numerically stable
Eigen Decomposition:
- A = Q × Λ × Q⁻¹
- Principal component analysis
- Dynamical systems
4. Advanced Linear Algebra Operations
Advanced operations including determinants, norms, and singular value decomposition.
Matrix Properties:
linalg.det()- Determinantlinalg.matrix_rank()- Ranklinalg.norm()- Matrix normslinalg.cond()- Condition number
SVD Applications:
- Dimensionality reduction
- Matrix approximation
- Pseudoinverse calculation
- Principal component analysis
5. Practical Application: Linear Regression
Apply linear algebra to solve real-world problems like linear regression using the normal equation.
Linear Regression Methods:
- Normal Equation: θ = (XᵀX)⁻¹Xᵀy
- Pseudoinverse: θ = X⁺y
- QR Decomposition: More numerically stable
- SVD: Handles rank-deficient matrices
Performance Comparison
Quick Reference Table
| Operation | Function | Description | Use Case |
|---|---|---|---|
| Linear System | linalg.solve(A, b) | Solve Ax = b | Equations solving |
| Matrix Inverse | linalg.inv(A) | Find A⁻¹ | Theoretical work |
| LU Decomposition | linalg.lu(A) | A = P × L × U | Multiple solves |
| Cholesky | linalg.cholesky(A) | A = L × Lᵀ | Symmetric matrices |
| Eigen Decomposition | linalg.eig(A) | A = QΛQ⁻¹ | PCA, dynamics |
| SVD | linalg.svd(A) | A = UΣVᵀ | Dimension reduction |
| Determinant | linalg.det(A) | det(A) | Invertibility check |
| Pseudoinverse | linalg.pinv(A) | Moore-Penrose inverse | Rank-deficient systems |
🎯 Key Takeaways
- Use
linalg.solve()instead of inverse for linear systems - Choose appropriate decomposition based on matrix properties
- SVD is powerful for rank-deficient and ill-conditioned matrices
- Linear algebra forms the foundation for machine learning and data science
- Always verify results and consider numerical stability