🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #34

Matrix Multiplication

Matrix multiplication combines two matrices by taking dot products between the rows of one and the columns of the other — it's how data flows through every linear transformation in ML, including every layer of a neural network.

Formula

\[ (AB)_{ij} = \sum_{k=1}^{n} A_{ik}B_{kj} \]

Entry \((i,j)\) of the result is the dot product of row \(i\) of \(A\) with column \(j\) of \(B\). This only works if the number of columns in \(A\) equals the number of rows in \(B\).

Numerical Example

\[ A = \begin{bmatrix}1&2\\3&4\end{bmatrix}, \quad B = \begin{bmatrix}2&0\\1&2\end{bmatrix}, \quad AB = \begin{bmatrix}(1)(2)+(2)(1) & (1)(0)+(2)(2)\\(3)(2)+(4)(1) & (3)(0)+(4)(2)\end{bmatrix} = \begin{bmatrix}4&4\\10&8\end{bmatrix} \]
import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[2, 0], [1, 2]])

print(A @ B)   # [[ 4  4] [10  8]]
print(B @ A)   # [[ 2  4] [ 7 10]]  -- NOT the same as A @ B

Why Order Matters — Matrix Multiplication Isn't Commutative

\(AB \neq BA\) in general, as the example above shows directly (\([[4,4],[10,8]]\) vs \([[2,4],[7,10]]\)). This isn't a technicality — in a neural network, swapping the order of two weight matrices completely changes what the network computes, since each layer's output depends on the exact sequence of transformations applied.

Practical Use Cases

  • Every layer of a neural network is a matrix multiplication (inputs × weights) followed by an activation function
  • Transforming an entire feature matrix at once (e.g. applying a learned rotation/projection in PCA)
  • Composing multiple linear transformations by multiplying their matrices together

Common Mistakes

  • Assuming matrix multiplication is commutative like ordinary number multiplication — it generally isn't.
  • Confusing element-wise multiplication (A * B in NumPy) with matrix multiplication (A @ B) — these are entirely different operations with different shape requirements.

Interview Relevance

Q: "What's the difference between A * B and A @ B in NumPy?" * multiplies matching elements position-by-position (both matrices must be the same shape); @ performs true matrix multiplication (dot products of rows and columns, with the inner dimensions needing to match) — mixing these up is a very common bug.

Practice Question

Given \(A = \begin{bmatrix}1&0\\0&2\end{bmatrix}\) and \(B=\begin{bmatrix}3&1\\2&4\end{bmatrix}\), compute \(AB\) by hand, then verify with NumPy.

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Matrix Multiplication – FAQs

Quick answers about learning Matrix Multiplication in Machine Learning.

This free note from CodingNow 2.0 explains Matrix Multiplication in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including Matrix Multiplication, is 100% free with no signup required.
With focused practice, most students grasp Matrix Multiplication in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now