NumPy Linear Algebra

Data ScienceNumPyFree Lesson

Advertisement

Introduction

NumPy's linalg module provides standard linear algebra operations.

Matrix Operations

import numpy as np

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

# Matrix multiplication
C = A @ B  # or np.dot(A, B)

# Element-wise
C = A * B

# Transpose
A.T

Linear Algebra Functions

import numpy as np

A = np.array([[1, 2], [3, 4]])

# Determinant
np.linalg.det(A)

# Inverse
np.linalg.inv(A)

# Eigenvalues and vectors
eigenvalues, eigenvectors = np.linalg.eig(A)

# Solve Ax = b
b = np.array([1, 2])
x = np.linalg.solve(A, b)

Decompositions

# SVD
U, s, Vt = np.linalg.svd(A)

# QR decomposition
Q, R = np.linalg.qr(A)

# Cholesky (for SPD matrices)
L = np.linalg.cholesky(A @ A.T + 1e-6 * np.eye(2))

Practice Problems

  1. Solve system of linear equations
  2. Compute matrix inverse
  3. Find eigenvalues and eigenvectors
  4. Perform SVD on image
  5. Implement least squares regression

Advertisement

Need Expert Python Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement