Linear Algebra Ops
16 min
NumPy's linear algebra operations are the computational core of ML and quant work — every neural network layer and every portfolio-weights calculation ultimately comes down to dot products and matrix multiplication.
The dot product of two vectors multiplies corresponding elements and sums the results:
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*6 = 32
A portfolio's return is exactly a dot product: weight-of-each-asset times return-of-each-asset, summed. You'll use this exact pattern again in the Quant courses.
Matrix multiplication (@ or np.matmul) generalizes the dot product
to two dimensions: each element of the result is the dot product of a row
from the first matrix and a column from the second. That's why the inner
dimensions must match — A's columns must equal B's rows.
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A @ B)
# [[1*5+2*7, 1*6+2*8],
# [3*5+4*7, 3*6+4*8]]
# = [[19 22]
# [43 50]]
np.dot and @ do the same thing for 2D arrays — @ (added in Python
3.5) is generally preferred for matrix multiplication since it reads more
like standard math notation.
Write `dot_product(a, b)`, returning the dot product of two equal-length vectors `a` and `b` as a plain Python int, using `np.dot`.
Write `matrix_multiply(a, b)`, returning the matrix product of two 2D lists `a` and `b`, using `np.matmul`.
What's required for two matrices A (shape m×n) and B (shape p×q) to be multipliable as A @ B?
You can compute a dot product and a matrix product with NumPy, and know the shape-compatibility rule that determines whether two matrices can be multiplied.