Python / Data Science Essentials Interview Questions
How is NumPy linear algebra used in data science applications?
Linear algebra underpins almost all of machine learning — from computing gradients to PCA to solving systems of equations. NumPy's linalg submodule provides production-grade implementations of the core operations.
import numpy as np # --- Solving a system of linear equations: Ax = b --- # 2x + y = 8 # x + 3y = 11 A = np.array([[2, 1], [1, 3]]) b = np.array([8, 11]) x = np.linalg.solve(A, b) print(x) # [2.6 2.8] â verify: A @ x â b # --- Matrix decompositions --- M = np.array([[3, 1], [1, 3]], dtype=float) # Eigenvalue decomposition eigenvalues, eigenvectors = np.linalg.eig(M) # eigenvalues = [4. 2.], eigenvectors (columns) = principal directions # Singular Value Decomposition â used in PCA, recommendation systems X = np.random.default_rng(42).random((100, 5)) # 100 samples, 5 features X -= X.mean(axis=0) # centre U, S, Vt = np.linalg.svd(X, full_matrices=False) # S = singular values (square roots of eigenvalues of X^T X) # Vt rows = principal components # Project onto first 2 components: X_pca = X @ Vt[:2].T # shape (100, 2) # --- Norms --- v = np.array([3.0, 4.0]) np.linalg.norm(v) # 5.0 â L2 norm np.linalg.norm(v, ord=1) # 7.0 â L1 norm # --- Matrix rank, determinant, inverse --- np.linalg.matrix_rank(A) np.linalg.det(A) np.linalg.inv(A) # only for square non-singular matrices np.linalg.pinv(A) # Moore-Penrose pseudoinverse for non-square
SVD is the engine behind PCA: the right singular vectors (rows of Vt) are the principal components, and the singular values tell you how much variance each component explains. Using full_matrices=False (economy SVD) is essential for tall matrices — it skips computing the large, unused portions of U.
More Related questions...