Lesson 12 of 60 · python
Linear Algebra Basics with NumPy
Duration: 30 minutes
Linear Algebra Basics
Linear algebra is at the heart of many data‑science algorithms (e.g., PCA, regression).
Why it matters
- Vectors represent features.
- Matrices represent data tables or transformations.
Performing basic operations
import numpy as np
# Dot product (vector)
v = np.array([1, 2, 3])
w = np.array([4, 5, 6])
print('Dot:', np.dot(v, w)) # or v @ w
# Matrix multiplication
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
C = A @ B
print('A @ B =\n', C)
Solving linear systems
A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
solution = np.linalg.solve(A, b)
print('Solution:', solution)
Eigenvalues & eigenvectors
vals, vecs = np.linalg.eig(A)
print('Eigenvalues:', vals)
print('Eigenvectors:\n', vecs)
Singular Value Decomposition (SVD)
U, S, VT = np.linalg.svd(A)
print('U:\n', U)
print('S:', S)
print('VT:\n', VT)