Linear Algebra with NumPy
NumPy provides a submodule
np.linalg for linear algebra operations. These are the building blocks of many AI algorithms, including neural networks, PCA, and linear regression.Matrix Multiplication (
np.dot or @)Neural networks rely heavily on matrix multiplication (forward pass).
A = np.array([[1,2],[3,4]])
B = np.array([[5,6],[7,8]])
# Two ways:
C = np.dot(A, B)
C2 = A @ B
print(C) # [[19,22],[43,50]]Transpose (
.T)Swap rows and columns.
M = np.array([[1,2,3],[4,5,6]])
print(M.T) # [[1,4],[2,5],[3,6]]Inverse and Determinant
Used in some optimization algorithms.
M = np.array([[4,7],[2,6]])
inv = np.linalg.inv(M)
det = np.linalg.det(M)Solving Linear Equations
Solve for x in A·x = b.
A = np.array([[3,1],[1,2]])
b = np.array([9,8])
x = np.linalg.solve(A, b) # x = [2,3]Why This Matters for AI
- Linear regression: Solved using matrix equations (normal equation).
- Neural networks: Each layer is a matrix multiplication plus bias.
- PCA (Principal Component Analysis): Uses eigenvalue decomposition (
np.linalg.eig).
Two Minute Drill
- Matrix multiplication:
A @ Bornp.dot(A,B). - Transpose:
array.T. - Inverse:
np.linalg.inv(M). - Solve linear systems:
np.linalg.solve(A,b).
Need more clarification?
Drop us an email at career@quipoinfotech.com
