Stop 03 · Linear Algebra & CalculusTopics · 1/9

Vectors, Matrices & Dot Products

Part of the Machine Learning Roadmap

In short

To a model, a house, a selfie or a sentence is just a list of numbers. Vectors hold one example, matrices hold many. Once you see data this way, ML code reads much more easily.

A vector is a list of numbers, like one row of features, and a matrix is a table of them, like a whole dataset. The dot product multiplies two vectors element by element and adds up the results, which is exactly what a linear model does to make a prediction.

The purple blob points at neat rows of red cinema seats with an arrow-shaped pointer.

Think of it like this

Cinema hall seats

Your ticket says row F, seat 12. One row of seats is like a vector: numbers in a line, each with a position. The whole hall, rows and columns together, is a matrix. To find anyone, you just need their row and column.

Watch it happen

A flat described as a vector

3
0
1200
1
2
2
7
3

1/4One flat becomes a vector of 4 numbers: bedrooms, square feet, bathrooms and floor.

The big ideas

What you need to know

A vector is a list with meaning

[3, 1200, 2] can be a point in space or an arrow from the origin. Its length is the number of features.

A matrix is rows of vectors

Rows are examples, columns are features. Shape (500, 4) means 500 flats, 4 numbers each.

Shape errors are the most common bug

Most numpy and PyTorch errors say shapes don't match. Print .shape whenever something breaks.

Embeddings are vectors too

An LLM turns each word into a vector with hundreds of numbers. Similar meanings end up as nearby vectors.

See it in code

Walk through the code

python
1import numpy as np2 3flat = np.array([3, 1200, 2])            # bedrooms, sqft, baths4flats = np.array([[3, 1200, 2],5                  [2,  850, 1],6                  [4, 1800, 3]])7 8print(flats.shape)       # (3, 3): 3 flats, 3 features9print(flats[1])          # [  2 850   1]  second flat10print(flats[:, 1])       # [1200  850 1800]  the sqft column11 12weights = np.array([5, 0.04, 3])         # made-up weights13print(flats @ weights)   # [ 69.  47. 101.]  one score per flat

1/4A single example is a 1-D array, a vector.

The last line is a tiny linear model: multiply each feature by a weight and add up. Every flat gets a score in one go.

Save yourself the pain

Common mistakes

Oops

Mixing up rows and columns, so features end up as examples.

Do this instead

Keep the convention: rows are examples, columns are features. Check .shape before training.

Oops

Treating a vector of shape (3,) and a matrix of shape (3, 1) as the same thing.

Do this instead

They behave differently in maths. Use .reshape(-1, 1) when a library asks for a column.

Your mission

Turn your friends into a matrix

~15 min

Quick check

Your dataset has 1,000 customers and 12 features each. What shape should the matrix be?

Go deeper

  1. 1Compute a dot product by hand for two 3-number vectors, then check it with NumPy.
  2. 2Watch 3Blue1Brown's 'Essence of Linear Algebra' chapters 1 to 3.
  3. 3Write a house price as a dot product of features and weights.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge