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

Matrices

A matrix is a 2D grid of numbers — rows and columns. In ML, your entire dataset is a matrix: each row a sample, each column a feature. A model's prediction is what happens when that matrix is combined with a weight vector through matrix-vector multiplication.

Your Dataset, As a Matrix

\[ X = \begin{bmatrix} 1200 & 3 \\ 800 & 2 \\ 1500 & 4 \end{bmatrix} \]

This \(3 \times 2\) matrix (\(m \times n\): \(m\) rows/samples, \(n\) columns/features) represents 3 houses, each with square-footage and bedroom-count as features — exactly what X.shape reports in scikit-learn.

Matrix-Vector Multiplication — How a Linear Model Actually Predicts

A 1 2 3 4 × x 5 6 = result 17 39 row 1: 1×5 + 2×6 = 17 row 2: 3×5 + 4×6 = 39

Each output value is the dot product of one row of A with the vector x.

Formula

\[ (A\vec{x})_i = \sum_{j=1}^{n} A_{ij}\, x_j \]

\(A_{ij}\) is the entry in row \(i\), column \(j\) of matrix \(A\). Each output entry is literally a dot product between one row of \(A\) and the vector \(\vec{x}\) — this is exactly how a linear layer computes predictions for every sample in one matrix operation, instead of looping row by row.

import numpy as np

A = np.array([[1, 2],
              [3, 4]])
x = np.array([5, 6])

result = A @ x          # matrix-vector multiplication (@ is the matmul operator)
print(result)             # [17 39]

Why This Matters for ML

  • Predicting for an entire dataset at once — predictions = X @ weights — computes every sample's prediction in a single vectorized operation, instead of a slow Python loop
  • A neural network layer is just repeated matrix multiplication followed by an activation function
  • PCA relies entirely on matrix operations — the covariance matrix and its eigenvectors

Common Mistakes

  • Trying to multiply matrices with incompatible shapes — the number of columns in the first matrix must equal the number of rows in the second (or the length of the vector).
  • Assuming matrix multiplication is commutative — in general, \(AB \neq BA\).

Interview Relevance

Q: "Why is it faster to predict with matrix multiplication than looping through rows in Python?" NumPy's matrix multiplication runs in optimized, compiled C/BLAS code and processes the whole computation in one call, avoiding Python's per-iteration interpreter overhead — the same vectorization principle from NumPy for ML.

Practice Question

Given \(A = \begin{bmatrix}2 & 0\\0 & 3\end{bmatrix}\) and \(\vec{x}=[4, 5]\), compute \(A\vec{x}\) 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

Matrices – FAQs

Quick answers about learning Matrices in Machine Learning.

This free note from CodingNow 2.0 explains Matrices 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 Matrices, is 100% free with no signup required.
With focused practice, most students grasp Matrices 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