Represent observations
- Lesson 4
- 40 min
- Module 2: Vectors and matrices
Lesson 4 of 18 · Essential Mathematics and Statistics for AIRepresent observations
1. Numbers, notation and data summaries
2. Vectors and matrices
3. Probability and conditional reasoning
4. How models learn
5. Sampling and statistical evidence
6. Statistics for model decisions
What you will be able to do
A model cannot read a ticket, look at a photo or understand a sentence. It gets numbers. This lesson shows how one observation becomes a list of numbers and how many observations become a table. Then you can read the shape of any dataset that a model uses.
NotebookOptional: check the numbers in a notebook
The notebook is optional: this lesson works with a calculator. It has Priya’s tickets, the 8 × 8 image and the tasks in Python. Its checks say if your answer is right, without showing it. It also shows the failure case: columns in a new order keep the shape but change the meaning. It uses only Python’s standard library, so it needs no install.
Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.
Before you startWhat to know and to have ready
What you need to know
- How to read a small number below a letter, such as x₁ and x₃, from Mathematical reading skills: The summation sign, read aloud. x₁ is the first value of a list. This lesson uses two such numbers, for a row and a column.
- How to convert a value to another unit before you use it, from Mathematical reading skills: Units. Here, 200 words become 2 hundreds of words.
What you need to have ready (the notebook needs none of it)
- A calculator. You only count and multiply small numbers.
- Your
c03-notes.mdfile. You made it in the “Before you start” box of Mathematical reading skills. Add a heading:## Vectors and matrices. If you cannot find the file, make a new empty file with that name: this lesson does not use earlier answers.
The notebook was tested in Jupyter with Python 3.14.6 (standard library only).
Larkfield, again
Section titled “Larkfield, again”Larkfield is an online shop for home and garden products. About 1,200 customer messages arrive each week as tickets. Larkfield is building AI that suggests a team and a priority for each new ticket. In Module 1 you described the tickets with summaries. In this module you see how a model stores each ticket.
Where you meet this in AI
Section titled “Where you meet this in AI”- A table of customer data. A model predicts if a customer stops buying. It gets one row for each customer: number of orders, days since the last order, number of returns. Each row is a list of numbers.
- A photo. A customer uploads a photo of a broken lamp. The computer stores the photo as a grid of brightness values, one number for each pixel.
- Search with embeddings. A search system turns each sentence into a list of a few hundred numbers, called an embedding. Sentences with a similar meaning get similar lists. You use this idea in Lesson 3.
- A batch in training. A model learns from a few examples at a time, called a batch. A batch of 32 photos with 784 pixels each is a table with 32 rows and 784 columns.
In all four cases, the data is numbers in a fixed order. The words for this are scalar, vector and matrix.
From one ticket to a list of numbers
Section titled “From one ticket to a list of numbers”Priya, Larkfield’s data scientist, wants a simple model that scores how urgent a ticket is. First, she chooses four things to measure for each ticket. A measured property of an observation is a feature. An observation is one item that you measure: here, one ticket.
| Feature | What it counts | T-4101 |
|---|---|---|
| Length | Hundreds of words in the ticket | 2 |
| Refund words | How often “refund”, “charged” or “money back” appears | 2 |
| Past tickets | Tickets from the same customer in the last 90 days | 0 |
| Photo | 1 if the customer attached a photo, 0 if not | 1 |
Each value in the table is one number. One number alone is a scalar. “T-4101 has 2 refund words” is a scalar.
Now write the four values in a fixed order. The result is a vector: an ordered list of numbers that describes one observation.
Read it aloud as “the feature vector of T-4101 is two, two, zero, one”. A bold letter, such as x, often names a vector. The order is part of the meaning. The first position is always the length, the second position is always the refund words, and so on. The vector (2, 0, 2, 1) is a different ticket.
The number of values in a vector is its dimension. This vector has 4 features, so it has dimension 4. People also say “a 4-dimensional vector”.
A vector is also an arrow
Section titled “A vector is also an arrow”When a vector has only two numbers, you can draw it. Take two features of three tickets: refund words and past tickets. Each ticket becomes a point, and the arrow from (0, 0) to that point is its vector.
Text description of the chart
The horizontal axis is refund words, from 0 to 4. The vertical axis is past tickets, from 0 to 4. The arrow for T-4101 goes from (0, 0) to (2, 0): 2 refund words and no past tickets. The arrow for T-4102 goes to (0, 3): no refund words and 3 past tickets. The arrow for T-4103 goes to (1, 1). Tickets with similar values have tips that are close together.
You cannot draw four dimensions, but the same rules work. In Lesson 2 you add and compare these arrows. In Lesson 3 you measure how close two tips are.
From many tickets to a table of numbers
Section titled “From many tickets to a table of numbers”Priya writes the vectors of three tickets one under the other. Each ticket is one row. Each feature is one column.
| Ticket | Length | Refund words | Past tickets | Photo |
|---|---|---|---|---|
| T-4101 | 2 | 2 | 0 | 1 |
| T-4102 | 1 | 0 | 3 | 0 |
| T-4103 | 3 | 1 | 1 | 1 |
Without the labels, this is a rectangle of numbers:
A rectangle of numbers in rows and columns is a matrix. A capital letter, such as X, often names a matrix. The shape of a matrix is its number of rows, then its number of columns. This matrix has shape 3 × 4, read “three by four”.
In AI work, the usual layout is one row for each observation and one column for each feature. Then you can read the shape as a sentence: 3 tickets, 4 features each. For one week of tickets, the matrix has shape 1,200 × 4: 1,200 rows and 4,800 numbers in total.
One value in a matrix has two positions: its row and its column. means row 2, column 3. Here it is 3: ticket T-4102 has 3 past tickets.
Other data as vectors and matrices
Section titled “Other data as vectors and matrices”Tickets are tabular data: each column has a name. Images and text become numbers too, but in a different way.
A greyscale image is a matrix
Section titled “A greyscale image is a matrix”A greyscale image is a grid of pixels. Each pixel has one brightness value, from 0 (black) to 255 (white). So the image is a matrix with one row for each row of pixels.
Text description of the chart
On the left is an image of a house, 8 pixels wide and 8 pixels high. The roof is dark (value 60), the walls are mid grey (160), the window is almost white (250) and the door is very dark (40). The background is light grey (230). On the right are the same 64 values as an 8 × 8 matrix. The outlined value, in row 5 and column 3, is 250: the top-left pixel of the window.
Real images are larger:
| Image | Shape | Numbers |
|---|---|---|
| The house above | 8 × 8 | 64 |
| A small greyscale digit, a common training example | 28 × 28 | 784 |
| A customer’s colour photo, 1,000 × 750 pixels, with red, green and blue values | 1,000 × 750 × 3 | 2,250,000 |
A colour photo has three numbers for each pixel, so it is three matrices on top of each other. People call a block of numbers with more than two directions a tensor. You do not need tensors in this course. You only need to know that the shape has more than two numbers.
Many simple models need one vector for each image. They put the rows one after the other, so the 28 × 28 image becomes a vector of 784 numbers. A batch of 32 such images is then a matrix with shape 32 × 784.
A sentence is a long vector
Section titled “A sentence is a long vector”An embedding model is a model that turns a piece of text into a vector of a fixed dimension. Typical dimensions are 384, 768 or 1,536 numbers. These are typical sizes for sentence embeddings, not the size of one specific product.
Text description of the chart
The strip has 384 thin bars, from position 1 on the left to position 384 on the right. Each bar is one number. Bars above the line are positive, bars below are negative. Most values are between −0.1 and +0.1. Below the strip, you see the first values and then the text “378 more” (“381 more” on a narrow screen). We made up the values for this lesson. They are not the output of a real model.
The ticket vector and the embedding are different in one important way. In the ticket vector, each position has a name: length, refund words. In an embedding, the positions have no names. Position 17 does not mean “refund”. The meaning is in how close the whole vector is to other vectors. Two tickets about a late parcel get embeddings that are close together. Lesson 3 shows how to measure “close”.
Worked example: Priya builds the feature matrix
Section titled “Worked example: Priya builds the feature matrix”Priya turns three new tickets into a matrix. She writes every intermediate value, so that Grace, the support lead, can check it.
| Ticket | Start of the text | Words | Refund words found | Past tickets | Photo |
|---|---|---|---|---|---|
| T-4101 | “I was charged for a lamp that arrived broken. Please refund me…” | 200 | charged, refund → 2 | 0 | yes |
| T-4102 | “Where is my order? This is the third time I write…” | 100 | none → 0 | 3 | no |
| T-4103 | “The garden chair has a crack. Can I get my money back or a new one…” | 300 | money back → 1 | 1 | yes |
Step 1: convert each value to the feature’s unit. Length is in hundreds of words, so 200 words → 2. Photo “yes” → 1 and “no” → 0.
Step 2: write each ticket as a vector, in the fixed feature order (length, refund words, past tickets, photo).
- T-4101 → (2, 2, 0, 1)
- T-4102 → (1, 0, 3, 0)
- T-4103 → (3, 1, 1, 1)
Step 3: stack the vectors as rows. The result is the 3 × 4 matrix X from the section above.
Step 4: check the shape. 3 rows, because there are 3 tickets. 4 columns, because there are 4 features. Each row has exactly 4 values.
Grace asks: “What does the number in row 3, column 1 mean?” Priya answers: “Row 3 is T-4103. Column 1 is length. T-4103 has 300 words, so the value is 3.”
In Python, a professional keeps this matrix in a numpy array or a pandas DataFrame. X.shape then shows (3, 4). Python counts positions from 0, so row 3 is row index 2 in code.
Guided practice: add two tickets
Section titled “Guided practice: add two tickets”Two more tickets arrive. Turn them into rows of Priya’s matrix.
- T-4104: 400 words. “I was charged twice for one order. I want a refund for the second payment.” The customer wrote 2 tickets in the last 90 days. No photo.
- T-4105: 100 words. “Please change my delivery address.” No past tickets. No photo.
- In
c03-notes.md, add a heading:### Feature matrix. - Count the refund words in each text. Use only “refund”, “charged” and “money back”.
- Convert the length of each ticket to hundreds of words.
- Write each ticket as a vector in the order length, refund words, past tickets, photo.
- Add the two vectors as rows 4 and 5 under the three rows of X.
- Write the shape of the new matrix.
- Write which value is in row 4, column 3, and what it means.
Show the expected result
- T-4104: “charged” and “refund” → 2 refund words. Length 400 words → 4. Vector: (4, 2, 2, 0).
- T-4105: no refund words. Length 1. Vector: (1, 0, 0, 0).
| Ticket | Length | Refund words | Past tickets | Photo |
|---|---|---|---|---|
| T-4101 | 2 | 2 | 0 | 1 |
| T-4102 | 1 | 0 | 3 | 0 |
| T-4103 | 3 | 1 | 1 | 1 |
| T-4104 | 4 | 2 | 2 | 0 |
| T-4105 | 1 | 0 | 0 | 0 |
The shape is 5 × 4: 5 tickets, 4 features. Row 4, column 3 is 2: the customer of T-4104 wrote 2 tickets in the last 90 days.
Check: every row has 4 values, and every column holds only one kind of value. If a row has 3 or 5 values, a feature is missing or extra.
Independent variation: change the size
Section titled “Independent variation: change the size”Change one thing at a time and write the new shape in your notes.
- Priya adds a fifth feature: hours since the parcel arrived. What is the shape for one week of 1,200 tickets?
- In a busy week, 1,350 tickets arrive. Priya keeps the five features. What is the shape now, and how many numbers does the matrix hold?
- Larkfield trains an image model on batches of 64 greyscale photos. Each photo is 28 × 28 pixels, and Larkfield turns each photo into one vector. What is the shape of one batch?
- Each of the 1,200 tickets of one week also gets an embedding with 384 numbers. What is the shape of the matrix of embeddings?
Show the answers
- 1,200 × 5. The rows stay the same. The new feature adds one column.
- 1,350 × 5, which holds 1,350 × 5 = 6,750 numbers.
- One photo is 28 × 28 = 784 numbers, so the batch is 64 × 784.
- 1,200 × 384. One row for each ticket, one column for each position of the embedding.
Common mistake
The columns change order
Symptom: a model worked well for months. After a change to the data pipeline, it suddenly marks many short, polite tickets as urgent. No error message appears.
Cause: the pipeline now writes the columns in a different order: past tickets first, then length. The model still reads position 1 as length. A vector has meaning only through the order of its positions, so every ticket now looks like a different ticket. The shape is still 1,200 × 4, so no shape check catches it.
Fix: keep the feature names with the data, for example as column names in a table. Check the names and their order before the model scores. Also compare simple summaries with last week: if the mean of “length” suddenly equals last week’s mean of “past tickets”, the columns moved.
Check your understanding
Why is (2, 2, 0, 1) a different ticket from (2, 0, 2, 1), when both contain the same four numbers?
Show the answer
Each position in a feature vector means one feature. In the first vector, position 2 (refund words) is 2 and position 3 (past tickets) is 0. In the second vector, it is the opposite: no refund words, but 2 past tickets. The numbers are the same, but they describe a different ticket. A vector is an ordered list.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
A colleague says: 'Our dataset has shape 4 × 1,200.' Using the usual layout, what is wrong, and why does it matter?
Show the answer
The usual layout is one row for each observation and one column for each feature. 4 × 1,200 means 4 rows and 1,200 columns, so the tickets are in the columns. Most tools read each row as one observation. They would see 4 “tickets” with 1,200 features each. Turn the matrix with a transpose (Lesson 2), so that it has shape 1,200 × 4.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Predict: in a sentence embedding with 384 numbers, can you find the position that means 'refund'? Explain.
Show the answer
No. The positions of an embedding have no names. The embedding model chooses the numbers so that texts with a similar meaning get similar vectors. “Refund” is spread over many positions. You use an embedding by comparing whole vectors, not by reading one position.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
- A scalar is one number. A vector is an ordered list of numbers that describes one observation. Its dimension is how many numbers it has.
- A matrix is a rectangle of numbers. Its shape is rows × columns. In AI work, rows are usually observations and columns are features: 1,200 tickets with 4 features give 1,200 × 4.
- A greyscale image is a matrix of pixel values. A sentence embedding is a long vector, typically with a few hundred to a few thousand numbers, and its positions have no names.
- The order of the features is part of the meaning. Keep the names with the numbers.
Next, you calculate with vectors. You see how a model turns a feature vector into one score with a dot product.
Next: Vector operations