Back to Curriculum

Linear Algebra with NumPy

📚 Lesson 6 of 15 ⏱️ 45 min

Linear Algebra with NumPy

45 min

NumPy provides powerful linear algebra functions through the linalg module, enabling matrix operations essential for scientific computing, machine learning, and data analysis. The linalg module includes functions for matrix multiplication, decomposition, solving linear systems, and computing matrix properties. Understanding linear algebra functions enables advanced numerical computing. Linear algebra is fundamental to many scientific applications.

Common operations include matrix multiplication (np.dot(), @ operator), determinants (LA.det()), eigenvalues/eigenvectors (LA.eig()), matrix inversion (LA.inv()), and solving linear equations (LA.solve()). Matrix multiplication is fundamental to linear algebra. Determinants indicate matrix invertibility. Eigenvalues/eigenvectors reveal matrix properties. Understanding these operations enables matrix computations. These operations are essential for many algorithms.

Matrix multiplication can be done with np.dot(A, B) or the @ operator (A @ B). The @ operator is more readable and is the recommended approach. Matrix multiplication requires compatible dimensions (columns of first = rows of second). Understanding matrix multiplication enables matrix computations. Matrix multiplication is fundamental to linear algebra.

NumPy provides matrix decomposition functions: LU decomposition (LA.lu()), QR decomposition (LA.qr()), SVD (LA.svd()), and Cholesky decomposition (LA.cholesky()). Decompositions enable solving systems, computing inverses, and understanding matrix structure. Understanding decompositions enables advanced linear algebra. Decompositions are essential for numerical stability.

Solving linear systems (Ax = b) can be done with LA.solve() for square systems or LA.lstsq() for overdetermined systems (least squares). Solving systems is fundamental to many scientific problems. Understanding system solving enables solving real-world problems. System solving is essential for many applications.

Best practices include using @ operator for matrix multiplication, checking matrix properties (determinant, rank) before operations, using appropriate decomposition for numerical stability, handling singular matrices appropriately, and understanding when to use solve() vs lstsq(). Understanding linear algebra functions enables advanced numerical computing. Linear algebra is essential for scientific computing and machine learning.

Key Concepts

  • NumPy provides powerful linear algebra functions through linalg module.
  • Common operations: matrix multiplication, determinants, eigenvalues, solving systems.
  • Matrix multiplication uses np.dot() or @ operator.
  • Matrix decompositions enable numerical stability and analysis.
  • Linear algebra functions are essential for scientific computing and ML.

Learning Objectives

Master

  • Performing matrix multiplication and operations
  • Computing matrix properties (determinant, rank, trace)
  • Finding eigenvalues and eigenvectors
  • Solving linear systems and using decompositions

Develop

  • Understanding linear algebra concepts
  • Designing numerical algorithms
  • Appreciating linear algebra's role in computing

Tips

  • Use @ operator for matrix multiplication—it's more readable.
  • Check matrix determinant before computing inverse (singular matrices).
  • Use appropriate decomposition for numerical stability.
  • Use LA.solve() for square systems, LA.lstsq() for overdetermined systems.

Common Pitfalls

  • Not checking matrix properties, causing errors with singular matrices.
  • Using wrong dimensions for matrix multiplication.
  • Not using appropriate decomposition, causing numerical instability.
  • Not understanding when to use solve() vs lstsq().

Summary

  • NumPy provides powerful linear algebra functions through linalg module.
  • Matrix operations include multiplication, determinants, eigenvalues.
  • Matrix decompositions enable numerical stability.
  • Linear algebra functions are essential for scientific computing.
  • Understanding linear algebra enables advanced numerical computing.

Exercise

Perform linear algebra operations using NumPy.

import numpy as np
from numpy import linalg as LA

# Create matrices
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

print("Matrix A:")
print(A)
print("Matrix B:")
print(B)

# Matrix multiplication
C = np.dot(A, B)
print("Matrix multiplication (A * B):")
print(C)

# Alternative matrix multiplication
C_alt = A @ B
print("Matrix multiplication using @:")
print(C_alt)

# Matrix properties
print("Determinant of A:", LA.det(A))
print("Trace of A:", np.trace(A))
print("Rank of A:", LA.matrix_rank(A))

# Eigenvalues and eigenvectors
eigenvals, eigenvecs = LA.eig(A)
print("Eigenvalues:", eigenvals)
print("Eigenvectors:")
print(eigenvecs)

# Matrix inverse
A_inv = LA.inv(A)
print("Inverse of A:")
print(A_inv)

# Verify inverse
identity = np.dot(A, A_inv)
print("A * A^(-1) (should be identity):")
print(identity)

# Solve linear equations Ax = b
b = np.array([5, 6])
x = LA.solve(A, b)
print("Solution to Ax = b:")
print("x =", x)

# Verify solution
result = np.dot(A, x)
print("A * x (should equal b):", result)

# Singular Value Decomposition (SVD)
U, s, Vt = LA.svd(A)
print("SVD of A:")
print("U:")
print(U)
print("Singular values:", s)
print("V^T:")
print(Vt)

Code Editor

Output