By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Linear algebra is the language of vectors and matrices that lets us compactly represent data, model parameters, and transformations. In ML every feature set becomes a matrix X, every weight vector w, and many algorithms (PCA, linear regression, neural‑net back‑prop) are just matrix‑vector arithmetic. For example, a recommendation engine for a streaming service stores user‑item interactions in a sparse matrix R; factorizing R with SVD uncovers latent “taste” dimensions that power personalized suggestions.
X = df.values
X -= X.mean(axis=0)
X /= X.std(axis=0)
U, s, VT = np.linalg.svd(X, full_matrices=False)
eigvals, eigvecs = np.linalg.eigh(X.T @ X)
explained = np.cumsum(s2) / np.sum(s2)
X_reduced = X @ VT.T[:, :k]
Mistake: Applying SVD to non‑centered data. Correction: Center the matrix first; otherwise the first singular vector captures the mean rather than true variance.
Mistake: Confusing eigenvectors of X with those of the covariance matrix. Correction: For PCA you need eigenvectors of XᵀX (or C), not of X itself; they are the right singular vectors V.
Mistake: Using the full‑rank inverse (np.linalg.inv) on a singular or ill‑conditioned matrix. Correction: Use the pseudo‑inverse (np.linalg.pinv) or add ridge regularization (αI).
np.linalg.inv
np.linalg.pinv
αI
Mistake: Dropping too many components because the scree plot looks “flat”. Correction: Compute cumulative explained variance and keep enough components to retain ≥90 % (or domain‑specific) variance.
Mistake: Assuming SVD is “fast enough” for huge sparse matrices. Correction: Switch to truncated SVD (sklearn.utils.extmath.randomized_svd) or use sparse‑aware libraries (SciPy’s svds).
sklearn.utils.extmath.randomized_svd
svds
Q: Your text‑classification matrix X has 100 000 documents × 50 000 terms and is extremely sparse. Which linear‑algebra tool should you use to obtain a 100‑dimensional representation? A: Truncated SVD (sklearn.decomposition.TruncatedSVD) because it works directly on sparse matrices and avoids forming the dense covariance.
sklearn.decomposition.TruncatedSVD
Q: After fitting a linear regression with the normal equation, you get a warning about a singular matrix. What’s the quickest fix? A: Add a small ridge term (αI) to XᵀX before inversion, i.e., use w = (XᵀX + αI)⁻¹ Xᵀy.
Q: In PCA you notice the first principal component explains 70 % of variance, but the second only 5 %. Should you keep the second component? A: Yes, if the cumulative variance after the second component reaches your target (e.g., 90 %); otherwise you may drop it.
np.dot(a, b)
y = X @ w
w = np.linalg.inv(X.T @ X) @ X.T @ y
w = np.linalg.inv(X.T @ X + α*np.eye(n)) @ X.T @ y
svd = TruncatedSVD(n_components=k); X_red = svd.fit_transform(X)
X_pinv = np.linalg.pinv(X)
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.