Data Tables & Transformations (Matrices)
The 2D grid of numbers where every ML library primarily thinks.
You already know vectors — ordered lists of numbers that represent data points. But what happens when you stack many vectors together? You get a matrix — a rectangular grid of numbers. Every spreadsheet, every dataset, every neural network layer is built on matrices.
In this article, you will learn what matrices are, how to multiply them (and why the definition is not arbitrary), and why this single operation is the computational heart of modern AI.
What Is a Matrix?
Matrix
Rows = data points, columns = features. A 1000×5 matrix stores 1000 houses with 5 properties each.
The matrix defines HOW an input vector is turned into an output vector. Weight matrices in neural networks are transformations.
Three houses with the features area, rooms, and year built form a 3×3 matrix. Each row is a house, each column is a feature.
import numpy as np
# Three houses as a 3x3 matrix
houses = np.array([
[120, 4, 1990],
[85, 3, 2005],
[200, 6, 1975]
])
print(houses.shape) # (3, 3)
print(houses[1, 2]) # 2005 — year built of house 2Misconception: Matrices Are Just Tables
Every dataset in machine learning is a matrix: rows are examples, columns are features. And every network layer is a matrix transformation.
Matrix Multiplication — Why This Definition?
Matrix multiplication looks complicated at first — but it has a deep reason: It exactly describes the composition of two transformations. Apply matrix B first, then matrix A, and the result is the matrix AB.
Each element c_ij of the result is the dot product of row i of A with column j of B. The connection to the vectors article: matrix multiplication consists of many dot products.
The inner dimensions must match. An (m×n) matrix times an (n×p) matrix produces an (m×p) matrix. If the inner dimensions do not match, multiplication is undefined.
import numpy as np
# Weight matrix W (2x3) times input vector x (3x1)
W = np.array([[0.5, -0.3, 0.8],
[0.1, 0.7, -0.2]])
x = np.array([1, 2, 3])
print(W @ x) # [2.3, 0.9]
print(W.shape, '@', x.shape, '→', (W @ x).shape)
# (2, 3) @ (3,) → (2,)Misconception: A × B = B × A
Deep Dive: Hadamard Product vs. Matrix Multiplication
Matrices in Neural Networks — Why GPUs Calculate
Every fully connected layer of a neural network computes exactly one matrix multiplication: weight matrix times input plus bias. Training means adjusting the entries of the weight matrix to reduce the error.
Shape Chain Through a Mini-Network
Instead of processing one vector at a time, many inputs are stacked into a matrix. This way, an entire batch (e.g., 32 images) is processed in ONE matrix multiplication. This is dramatically faster.
Why are GPUs so fast at this? Because each dot product in a matrix multiplication can be computed completely independently of the others. A CPU has a few very fast cores (e.g., 16). A GPU has thousands of slower cores (e.g., 10,000). For matrices — where thousands of simple calculations happen simultaneously — the GPU is dramatically superior.
import numpy as np
# Mini-network: 5 data points, 3 features
X = np.random.randn(5, 3) # Input: (5, 3)
W1 = np.random.randn(3, 4) # Layer 1: (3, 4)
W2 = np.random.randn(4, 2) # Layer 2: (4, 2)
hidden = X @ W1 # (5, 3) @ (3, 4) = (5, 4)
output = hidden @ W2 # (5, 4) @ (4, 2) = (5, 2)
print(f'Input: {X.shape} → Hidden: {hidden.shape} → Output: {output.shape}')Deep Dive: The Shape Chain in Detail
Interactive: The Most Famous 2×2 Matrix in ML
A neural network classifies inputs into categories — but how well? Quality is measured with a confusion matrix: a 2×2 matrix of True Positives, False Negatives, False Positives, and True Negatives. Adjust the parameters and observe how the four cells of the matrix shift — especially with rare events (low prevalence), the results are surprisingly counterintuitive.
A disease affects a certain proportion of the population. A test detects the disease with a certain hit rate, but also produces false-positive results in healthy people. How likely is the disease really when the test comes back positive?
Input Values
Bayes' Calculation
Result
Although the test has 95% sensitivity, the probability given a positive result is only 16.1%. This is due to the low base rate (1%): Among 1,000 tested people, there are 50 false alarms but only 10 true hits. The false positives overwhelm the real cases.
Visualized: 1,000 People Tested
Of 60 positive tests, only 10 are actually sick. This gives P(A|B) = 10/60 = 16.1%.
Key Takeaways
Quiz: Matrices
Checkpoint: Matrices
- I can name two different interpretations of a 100×5 matrix in the AI context.
- I can explain why a (3×4) matrix times a (4×2) matrix produces a (3×2) matrix and what happens to the inner dimension 4.
- I can explain why GPUs are so much faster than CPUs for neural networks, even though CPUs have higher clock speeds.