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-squareSVD 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.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
