Interview's MateData Science
Interview's Mate

Data Science

Python → ML → MLOps

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Science topics
Lesson 57/ 160
03. Mathematics & Statistics

Vectors, Matrices & Matrix Multiplication

Build linear-algebra fluency for features, embeddings, and model parameter math.

BeginnerVectors · Matrices3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

A vector is a list of numbers, and multiplying them pairs up the numbers, multiplies each pair and adds everything up.

Think of it like…

A price list and a shopping basket: the dot product of the vector of prices with the vector of quantities is the bill. Matrix multiplication does that for many baskets and many price lists at once.

Where you’ve seen it

A model predicts delivery time from three features: distance (km), items and rush flag, with weights w = [2.0, 0.5, 8.0] and intercept 10. For three orders stacked in X = [[5, 2, 0], [12, 6, 1], [3, 1, 0]], the predictions are Xw + 10 = [(5×2 + 2×0.5 + 0) + 10, (12×2 + 6×0.5 + 8) + 10, (3×2 + 1×0.5 + 0) + 10] = [21, 45, 16.5] minutes. The shapes are (3 × 3) · (3,) → (3,): one matrix product predicts every order with no Python loop.

Words you’ll hear

Dot product
Sum of element-wise products of two equal-length vectors; a scalar.
Matrix multiplication
(m × k) · (k × n) → (m × n); entry (i, j) is row i dotted with column j.
Transpose
Flipping rows and columns: (A ᵀ)ᵢⱼ = Aⱼᵢ.
Norm
A vector's length: ‖v‖ = √(v · v) for the Euclidean norm.

Visual diagram

Loading interactive diagram

Theory & deep dive

Vectors, dot products and norms

A vector v ∈ ℝⁿ can be read as a point or an arrow. Adding vectors adds entries; scaling multiplies each entry. The dot product a · b = Σ aᵢbᵢ measures alignment: it's large and positive when vectors point the same way, zero when they're perpendicular, negative when they oppose. The norm ‖a‖ = √(a · a) is the length; dividing a vector by its norm gives a unit vector. The angle between vectors satisfies cos θ = (a · b) / (‖a‖ ‖b‖), the cosine similarity used for embeddings, recommendations and text: for a = [1, 2, 3] and b = [4, 5, 6], a · b = 32, ‖a‖ = √14 ≈ 3.74, ‖b‖ = √77 ≈ 8.77, so cos θ ≈ 32 / 32.8 ≈ 0.975 (nearly the same direction).

In a linear model each prediction is the dot product of a feature vector and the weight vector, plus an intercept. The distance between two points is the norm of their difference, which underlies k-nearest neighbours and clustering.

Cosine similarity in NumPy

a @ b / (np.linalg.norm(a) * np.linalg.norm(b)) returns 0.9746 for a = [1, 2, 3], b = [4, 5, 6].

Key takeaway: Dot products measure alignment; norms measure length; cosine combines them.

Matrix multiplication and its meaning

For A (m × k) and B (k × n), the entry (AB)ᵢⱼ is the dot product of row i of A with column j of B, so the result is (m × n). Example: A = [[1, 2], [3, 4]] and B = [[5, 6], [7, 8]] give AB = [[1×5 + 2×7, 1×6 + 2×8], [3×5 + 4×7, 3×6 + 4×8]] = [[19, 22], [43, 50]]. Multiplication is not commutative (BA = [[23, 34], [31, 46]] here), but it is associative, so (AB)C = A(BC), which lets frameworks reorder products for efficiency. Special matrices: the identity I (IA = A), the transpose Aᵀ, and the inverse A⁻¹ (when it exists, AA⁻¹ = I).

Interpretations: a matrix is a linear transformation (rotate, scale, project); a product chains transformations; X w applies weights to all rows; XᵀX is the matrix of feature dot products (proportional to the covariance matrix when X is centred), which appears in the normal equation for linear regression, w = (XᵀX)⁻¹Xᵀy (solved numerically with np.linalg.lstsq or solve, not by inverting). A neural network layer is activation(X @ W + b), and embedding lookups are products with one-hot vectors. In NumPy and pandas, @ or np.matmul is the matrix product while * multiplies element-wise, a very common mix-up.

Predictions for all orders at once

X = np.array([[5, 2, 0], [12, 6, 1], [3, 1, 0]]); w = np.array([2.0, 0.5, 8.0]); y_hat = X @ w + 10 returns [21. , 45. , 16.5], with shape (3,).

Key takeaway: Row-by-column dot products; shapes (m×k)(k×n)→(m×n); not commutative.

How it works, step by step

Step 1 of 4
Plan

1. Write down the shapes

Note each array's shape and check that the inner dimensions of a product match.

(3 × 3) · (3,) → (3,)
Key numbers at this step
Mismatch
= ValueError

When to use it & trade-offs

What it is

A vector is an ordered list of numbers (one customer's features, a word embedding, a set of model weights); a matrix is a rectangular grid of numbers (a dataset with one row per observation and one column per feature). The dot product of two vectors multiplies matching entries and sums them: [1, 2, 3] · [4, 5, 6] = 1×4 + 2×5 + 3×6 = 32. Matrix multiplication combines rows of the first matrix with columns of the second: an (m × k) matrix times a (k × n) matrix gives an (m × n) matrix, and the inner dimensions must match. A linear model's predictions for all rows are one product: ŷ = Xw.

Why it matters: Nearly every machine-learning algorithm is linear algebra underneath: linear and logistic regression, PCA, neural network layers, embeddings and recommendations. Understanding shapes and what a product means lets you read model code, debug dimension errors, vectorise computations (replacing loops with X @ w) and reason about similarity (dot products and cosine similarity).

When to reach for it

  • Computing predictions for many rows at once
  • Similarity between embeddings
  • Understanding neural network layers

When to avoid or be careful

  • Using * when you mean @
  • Python loops over rows when a product works
  • Inverting matrices explicitly to solve equations

Products in NumPy

Dot / matrix product (@)
Best for: Linear models, projections, similarity

Strengths: Fast BLAS-backed computation; concise.

Watch out: Needs matching inner dimensions.

Element-wise product (*)
Best for: Scaling or masking entries

Strengths: Simple; broadcasts.

Watch out: Not a matrix product.

Outer product (np.outer)
Best for: Building matrices from two vectors

Strengths: Gives all pairwise products.

Watch out: Produces an m × n matrix; memory-heavy for big vectors.

Interview signals & numbers to know

When this topic comes up

  • “What is the shape of this product?”
  • “Why do I get a dimension mismatch?”
  • “Explain the normal equation”
  • Cosine similarity
  • Vectorisation

Benchmarks & rules of thumb

32 -
dot product of [1, 2, 3] and [4, 5, 6]
[[19, 22], [43, 50]] -
the example matrix product
0.975 -
cosine similarity of those vectors

Common mistakes & follow-ups

Common interview mistakes

  • Multiplying a (3 × 2) by a (3 × 2) matrix and getting a shape error instead of transposing one.
  • Assuming AB = BA.
  • Using np.linalg.inv(X.T @ X) for regression on near-collinear features.

Likely interviewer follow-ups

  • What is the shape of the product of an (m × k) and (k × n) matrix?
  • What does the dot product tell you about two vectors?
  • How does matrix multiplication relate to a neural network layer?
Interview tip: State shapes aloud when explaining any matrix expression. It's the fastest way to show you can reason about the maths and catch bugs.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Vectors, Matrices & Matrix Multiplication with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
A/B Testing & Experiment Design
Next topic
Eigenvalues, Eigenvectors & SVD