44% done
#4. Matrix and vectors
Dot product (vectors)
import numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) np.dot(a, b) # 1*4 + 2*5 + 3*6 = 32 a @ b # same, cleaner syntax
Matrix multiplication
A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) A @ B # [[19, 22], # [43, 50]]
Element-wise operations
A * B # element-wise multiply (NOT matrix mul) A + B # element-wise add A ** 2 # square every element
Transpose
A.T # [[1, 3], # [2, 4]]
Inverse
np.linalg.inv(A) # [[-2. , 1. ], # [ 1.5, -0.5]]
Determinant
np.linalg.det(A) # -2.0
Eigenvalues & eigenvectors
vals, vecs = np.linalg.eig(A) # vals: [-0.37, 5.37] # vecs: corresponding eigenvectors as columns
Solve linear system Ax = b
A = np.array([[3, 1], [1, 2]]) b = np.array([9, 8]) x = np.linalg.solve(A, b) # [2. 3.] # verify: A @ x == b
Norms
v = np.array([3, 4]) np.linalg.norm(v) # L2 norm = 5.0 np.linalg.norm(v, ord=1) # L1 norm = 7.0 np.linalg.norm(A, 'fro') # Frobenius norm for matrix
Cosine similarity
a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) cos_sim = np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)) # 0.9746 — used everywhere in NLP & recommendations
SVD (Singular Value Decomposition)
U, S, Vt = np.linalg.svd(A) # U — left singular vectors # S — singular values (diagonal) # Vt — right singular vectors transposed # used in PCA, dimensionality reduction, recommender systems
Broadcasting (no loops needed)
A = np.array([[1, 2, 3], [4, 5, 6]]) v = np.array([10, 20, 30]) A + v # [[11, 22, 33], # [14, 25, 36]] # numpy stretches v across every row automatically
Stacking vectors into a matrix
a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) np.vstack([a, b]) # [[1,2,3],[4,5,6]] — row stack np.hstack([a, b]) # [1,2,3,4,5,6] — flatten & join np.column_stack([a, b]) # [[1,4],[2,5],[3,6]] — col stack