Why linear algebra is useful
Linear algebra appears in:
- Regression
- Optimization
- Dimensionality reduction
- Correlation and covariance
NumPy provides numpy.linalg for common operations like decompositions, inverses, and determinants.
Dot product
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.dot(a, b)) # 1*4 + 2*5 + 3*6Matrix multiplication
* on two 2D arrays is element-wise. Matrix multiplication needs @ or np.matmul (equivalently np.dot for 2D arrays).
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[10, 20], [30, 40]])
print(A @ B)
print(np.matmul(A, B))flowchart LR A["Two matrices"] --> B["@ / np.matmul
matrix multiplication"] A --> C["np.linalg.solve(A, b)
solve Ax = b"] A --> D["np.linalg.inv(A)
matrix inverse"] A --> E["np.linalg.det(A)
determinant"] A --> F["np.linalg.eig(A)
eigenvalues/eigenvectors"] A --> G["np.linalg.norm(v)
vector length"]
Transpose
import numpy as np
A = np.array([[1, 2], [3, 4]])
print(A.T)Determinant and inverse
import numpy as np
A = np.array([[1, 2], [3, 4]])
det = np.linalg.det(A)
inv = np.linalg.inv(A)
print("det:", det)
print("inv:\n", inv)Solve a system of equations
Solving A x = b directly with np.linalg.solve is faster and more numerically stable than computing inv(A) @ b.
import numpy as np
A = np.array([[2, 1], [1, 3]])
b = np.array([8, 13])
x = np.linalg.solve(A, b)
print(x)Eigenvalues and eigenvectors
import numpy as np
A = np.array([[2, 0], [0, 3]])
vals, vecs = np.linalg.eig(A)
print("eigenvalues:", vals)
print("eigenvectors:\n", vecs)Norms (vector length)
import numpy as np
v = np.array([3, 4])
print(np.linalg.norm(v)) # 5Check your understanding
What does A * B do for two 2D arrays?
The * operator is element-wise. Matrix multiplication needs @ or np.matmul.
Show answer
B โ Element-wise multiplication โ The * operator is element-wise. Matrix multiplication needs @ or np.matmul.
What does np.dot(np.array([1, 2, 3]), np.array([4, 5, 6])) return?
The dot product is 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32.
Show answer
C โ 32 โ The dot product is 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32.
To solve A x = b, which approach does the lesson recommend?
np.linalg.solve is faster and more numerically stable than computing the inverse, which can be unstable.
Show answer
B โ np.linalg.solve(A, b) โ np.linalg.solve is faster and more numerically stable than computing the inverse, which can be unstable.
What does np.linalg.norm(np.array([3, 4])) return?
The norm is the vector length, the square root of 3 squared plus 4 squared, which is 5.
Show answer
C โ 5.0 โ The norm is the vector length, the square root of 3 squared plus 4 squared, which is 5.
Next
Continue to: Statistical Functions in NumPy for mean/median/std/percentiles and basic descriptive analytics.
๐งช Try It Yourself
Exercise 1 โ Dot Product
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 โ Matrix Multiplication with @
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 โ Solve a Linear System
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.