Data Tables & Transformations (Matrices)

The 2D grid of numbers where every ML library primarily thinks.

Fundamentals 12 min Intermediate May 11, 2026

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

AnalogyDefinition
Think of a spreadsheet with only numbers: rows are entries (houses), columns are properties (area, rooms, year built). That IS a matrix. Each row is a vector, and all vectors together form the grid.

Example

Real spreadsheets can have text, formulas, and empty cells. A mathematical matrix contains only numbers, has a fixed size, and supports algebraic operations like multiplication.
Matrix as Data Table

Rows = data points, columns = features. A 1000×5 matrix stores 1000 houses with 5 properties each.

Matrix as Transformation

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 2

Misconception: Matrices Are Just Tables

Matrices are far more than data storage. Their real power lies in representing transformations. A weight matrix in a neural network does not store data — it defines HOW inputs are transformed into outputs.

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.

1
Take row i of matrix A.
2
Take column j of matrix B.
3
Compute the dot product (multiply element by element and add up).
4
The result goes at position (i, j) of the result matrix C.
Matrix Multiplication
c_ij = a_i1·b_1j + a_i2·b_2j + … + a_in·b_nj

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.

Dimension Rule
(m × n) · (n × p) = (m × p)

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

Matrix multiplication is NOT commutative. A × B generally gives a different result from B × A. Often, only one of the two products is even defined because the dimensions do not match. Order matters!

In NumPy, * is the elementwise product (Hadamard) and @ is matrix multiplication. They give completely different results! The most common beginner mistake: using * when @ is intended.

import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

print(A * B)   # [[ 5, 12], [21, 32]] — elementwise
print(A @ B)   # [[19, 22], [43, 50]] — 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.

Neural Network Layer
output = W × input + bias

Shape Chain Through a Mini-Network

Input (5 data points) 5 × 3
Weights W1 3 × 4
Hidden Layer 5 × 4
Weights W2 4 × 2
Output 5 × 2

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.

312 TFLOPS
A100 GPU
~1 TFLOPS
CPU
~300×
Speedup

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}')

The inner dimension disappears at each multiplication — like canceling fractions. (5×3) @ (3×4) = (5×4): the 3 cancels out. Then (5×4) @ (4×2) = (5×2): the 4 cancels out. Data flows through the network layer by layer, and dimensions change in a controlled way.

# Shape chain visualized:
# (batch × features_in) @ (features_in × features_out) = (batch × features_out)
#   5 × 3               @          3 × 4              =       5 × 4
#   5 × 4               @          4 × 2              =       5 × 2
# The inner dimension must always match!

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.

Scenario: Medical Test

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

%
%
%
Examples:

Bayes' Calculation

P(B)= P(B|A) × P(A) + P(B|¬A) × P(¬A)
P(B)= 0.9500 × 0.0100 + 0.0500 × 0.9900 = 0.0590
P(A|B)= P(B|A) × P(A) / P(B)
P(A|B)= 0.9500 × 0.0100 / 0.0590 = 0.1610

Result

16.1%
P(A|B) — Probability of disease given a positive test
Before (Prior)
1%
After (Posterior)
16.1%
Bayes factor: The test has increased by a factor of 16.1 the probability (from 1% to 16.1%).
The Bayes Paradox

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

10
Sick & tested positive
(True Positive)
1
Sick & tested negative
(False Negative)
50
Healthy & tested positive
(False Positive)
941
Healthy & tested negative
(True Negative)

Of 60 positive tests, only 10 are actually sick. This gives P(A|B) = 10/60 = 16.1%.

Key Takeaways

  • A matrix is a rectangular grid of numbers — it can represent a dataset (rows = examples) or a transformation (input → output).
  • Matrix multiplication consists of dot products between rows and columns. It is defined this way because it describes the composition of linear transformations — not an arbitrary convention.
  • Every neural network layer computes output = W × input + bias — one matrix multiplication. GPUs excel at this because every output element can be computed independently.

Quiz: Matrices

Question 1 / 4
Not completed

A dataset has 500 images, each described by 3 features. What shape does the corresponding matrix have?

Select one answer
Answer Key: 1) B · 2) B · 3) B · 4) B

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.