Vectors, Matrices and Tensors
The language every ML algorithm speaks. From a single number to multi-dimensional arrays — visual intuition first, formula second.
Every ML algorithm is secretly just matrix math.
When you call model.fit(X_train, y_train) in scikit-learn, what happens inside? The algorithm multiplies matrices, adds vectors, and adjusts numbers. When a neural network processes an image, it converts the pixels into a matrix and multiplies it through dozens of layers. When an LLM reads your prompt, it converts every word into a vector of 768 or 4096 numbers.
You don't need to do this math by hand. NumPy and PyTorch do it for you. But if you don't understand what a matrix multiplication produces and why it's useful, you'll hit errors you can't debug, make architecture decisions you can't explain, and hit a ceiling you can't break through.
This page teaches three things: what scalars, vectors, matrices, and tensors are (with pictures), what the key operations do and why ML needs them, and how to work with them in NumPy — the library all of ML is built on.
From one number to billions — four levels
There are four levels of data structure in ML. Each one is just the previous one extended into another dimension. Once you understand this hierarchy, tensors stop being scary.
Scalar — just one number
A scalar is a single value. No direction, no structure. In ML, scalars show up as: a loss value (0.34), a learning rate (0.001), a single prediction (37.8 minutes), a model accuracy (0.92).
Vector — a list of numbers with meaning
A vector is a 1D array of numbers. In ML, every single data point is a vector. A DoorDash order with 4 features — distance, time of day, restaurant prep time, traffic score — is a vector of 4 numbers: [3.2, 2, 15, 7].
A word in an LLM is also a vector — called an embedding. The word "king" might be represented as a vector of 768 numbers. Those numbers encode meaning — words with similar meanings have vectors that point in similar directions in space.
Matrix — your entire dataset in one object
A matrix is a 2D array — rows and columns. In ML, your training data is a matrix. Each row is one example (one order). Each column is one feature (distance, time, etc.). 1,000 orders with 4 features → a matrix with shape (1000, 4).
Tensor — matrices stacked into higher dimensions
A tensor is the general term for arrays with any number of dimensions. A scalar is a 0D tensor. A vector is a 1D tensor. A matrix is a 2D tensor. Beyond 2D, people usually just say "tensor."
The most common 3D tensor in ML is a batch of images. A single colour image is a 2D grid of pixels — but it has 3 colour channels (Red, Green, Blue). So one image is a 3D tensor: height × width × channels. A batch of 32 images adds another dimension: (32, 224, 224, 3).
Four operations ML uses constantly
You don't need all of linear algebra. ML uses the same four operations over and over. Understanding these four deeply is enough to follow any ML paper, debug any shape error, and understand how data flows through a neural network.
1. Dot product — the similarity measure
The dot product takes two vectors of the same length and returns a single number. Multiply the matching elements, then add everything up. That number measures how much the two vectors "point in the same direction."
This is exactly how a neural network neuron works — it takes all your input features, multiplies each one by a learned weight, and adds everything up. One dot product = one neuron's output.
2. Matrix multiplication — the core of neural networks
When you multiply two matrices, you're doing many dot products at once. Each row of the first matrix dots with each column of the second. This is how a neural network layer transforms all your training examples simultaneously in one shot.
The shape rule is the most important thing to memorise here: to multiply matrix A (shape m×n) by matrix B (shape n×p), the inner dimensions must match (both n), and the result has shape (m×p).
3. Transpose — flip rows and columns
The transpose of a matrix flips it diagonally — rows become columns, columns become rows. Shape (m×n) becomes (n×m). You'll use this constantly to fix shape errors.
4. Broadcasting — apply an operation without repeating yourself
Broadcasting lets NumPy apply an operation between arrays of different shapes without creating copies. The smaller array is conceptually "stretched" to match the larger one.
In ML, you use this every time you normalise a dataset — you subtract the mean and divide by the standard deviation across 1,000 rows using just one line.
The shape cheatsheet — what every array means
In ML, shapes carry meaning. Once you know the convention, reading a shape like (32, 3, 224, 224) immediately tells you: 32 images in the batch, 3 colour channels, 224×224 pixels. Here's the full reference.
The most important debugging habit: whenever you get a shape error or unexpected result, immediately print the shape of every array involved. 90% of ML bugs come down to a wrong shape somewhere in the pipeline.
You don't do this math by hand at work — but you read shapes constantly
No production ML job asks you to multiply matrices with a pencil. What it asks constantly is to read a shape, know what it represents, and spot the moment it stops making sense. This is one of the few skills from this page that shows up in literally every ML-adjacent role, not just one.
Debugging why a training job crashed overnight — nine times out of ten the traceback ends in a shape mismatch between the feature matrix and the model's expected input.
Reshaping a raw pull from the warehouse — one row per order, one column per feature — into the (n_samples, n_features) matrix every sklearn model expects.
Reading a paper's architecture diagram and translating "hidden dimension 512" directly into the shape of a weight matrix before writing a single line of code.
Choosing an embedding model and immediately needing to know whether it produces a 384-number vector or a 1536-number one, because that shape has to match everywhere it gets stored and compared.
Five things people get wrong about vectors, matrices, and tensors
Everything on this page reduces to four operations — dot product, matrix multiply, transpose, and broadcasting — and NumPy or PyTorch executes every one of them for you. What separates a productive ML engineer from a stuck one is not the ability to multiply matrices by hand; it is the habit of knowing what a shape like (1000, 4) represents and noticing immediately when a shape stops making sense. That is a reading skill, not a calculation skill, and it is entirely learnable without a math degree.
A tensor is just the general word for an array with any number of dimensions — a scalar is a 0-dimensional tensor, a vector is 1-dimensional, a matrix is 2-dimensional, and 'tensor' is simply what people call it once you go past two. PyTorch and TensorFlow named their core data type 'Tensor' because it is the one word that covers every case above without needing a different name for each dimension count — not because the underlying object is mathematically exotic.
In raw Python, yes — a 4D array would mean four nested loops to touch every element. NumPy and PyTorch exist specifically so you almost never write that loop. Broadcasting, the colon-slicing shown throughout this page, and vectorised operations let you write one line — X.mean(axis=0), X @ W — that operates across every dimension at once, at C or GPU speed instead of Python speed. Reaching for a manual loop over array elements is usually the sign something is being done the slow, wrong way.
Every row in a training table is a vector. Every dataset fed to model.fit() is a matrix. Every batch of images or token sequence a transformer processes is a tensor. There is no layer of the ML stack — from a scikit-learn script to a trillion-parameter LLM — where this structure is optional or theoretical. The vocabulary might disappear behind a library call, but the data underneath it never stops being exactly this.
High-level APIs hide the matrix multiplication, not the shape requirements. model.fit(X, y) will still raise an error the moment X and y disagree on the number of rows, or a Dense layer will still fail the moment its input dimension does not match the previous layer's output. This page's own closing rule holds regardless of which library sits on top: the large majority of ML bugs people hit are shape mismatches, and the fix is always the same — print the shape and look.
Vectors, matrices, and tensors — 5 questions interviewers actually ask
They are the same idea extended by one dimension each time. A scalar is a single number with zero dimensions. A vector is a one-dimensional list of numbers — one training example, or one word embedding. A matrix is a two-dimensional grid — rows and columns, like an entire training dataset where each row is an example and each column is a feature. A tensor is the general term for any of these — people specifically use the word 'tensor' once you go beyond two dimensions, like a batch of images with shape (batch, height, width, channels).
A single colour image is not just a flat grid of pixels — it has a colour channel dimension on top of height and width, typically red, green, and blue, so one image alone is already 3-dimensional: (height, width, channels). Training almost never happens on one image at a time; a batch of, say, 32 images adds a fourth dimension in front, giving a shape like (32, 224, 224, 3). The extra dimensions are not decorative — they are exactly what lets a GPU process 32 images with one matrix operation instead of 32 separate ones.
Broadcasting lets NumPy or PyTorch apply an operation between arrays of different shapes by conceptually stretching the smaller one to match the larger one, without ever actually copying that data in memory. The classic example is normalising a dataset: subtracting a (n_features,) mean vector from an (n_samples, n_features) matrix in a single line, instead of writing a loop that repeats the subtraction once per row. It matters for performance because that single vectorised line runs as compiled, often parallelised code, while the equivalent Python loop would run orders of magnitude slower.
First, print both shapes directly — print(A.shape, B.shape) — because the error message alone rarely tells you which of the two operands is wrong. Then apply the shape rule: for A @ B to work, A's number of columns must equal B's number of rows, and the result takes A's row count and B's column count. If the inner dimensions don't match, the fix is almost always either transposing one operand with .T or checking further upstream for where that array's shape was built incorrectly in the first place — a reshape, a concatenation, or a feature that got appended as a separate array instead of joined into the matrix.
Representing an entity as a vector of, say, 768 numbers lets a model encode many aspects of meaning simultaneously — direction and magnitude in that 768-dimensional space becomes a stand-in for semantic similarity. Two words with similar meanings end up with vectors that point in similar directions, so distance metrics like cosine similarity or Euclidean distance between two embedding vectors give a numeric measure of how related two things are — which is exactly the mechanism behind recommendation systems suggesting similar products and search engines matching a query to semantically related documents, not just exact keyword matches.
You can now read ML code without getting lost.
Every time you see an ML model being built — a layer in PyTorch, a fit call in sklearn, an attention mechanism in a Transformer — you now understand the underlying structure. Data is stored in arrays. Arrays have shapes. Layers multiply matrices. Outputs are also matrices.
The next module takes this one step further — matrix multiplication and linear transformations. You'll see exactly how data changes shape as it flows through a neural network layer, and why the choice of matrix dimensions determines what a layer can learn.
How a neural network layer transforms your data — visualised step by step.
🎯 Key Takeaways
- ✓Scalar = one number. Vector = list of numbers. Matrix = 2D grid. Tensor = any N-dimensional array. They are nested — each is the previous extended by one dimension.
- ✓In ML: one data point = one vector. Your entire dataset = a matrix. A batch of images = a 4D tensor (batch, height, width, channels).
- ✓Dot product: multiply matching elements and sum — this is exactly what one neuron does. np.dot(features, weights) or features @ weights.
- ✓Matrix multiply shape rule: (m × n) @ (n × p) → (m × p). Inner dimensions must match. If they don't, you have a shape error — transpose one of the matrices.
- ✓Broadcasting lets you subtract a mean vector of shape (4,) from a matrix of shape (1000, 4) without a loop. NumPy stretches the smaller array automatically.
- ✓The one debugging habit: print(array.shape) immediately when something is wrong. 90% of ML bugs are shape mismatches.
Discussion
0Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.