…
…
AI Fundamentals · Lesson 9 / 31 · Measuring a model
PreviewIn this lesson you will
You can read the explanation without an account. The exercises and quiz at the end of the lesson are for members.
The router is ready. It counts the keywords in a question, looks at the 3 nearest neighbors, and names a unit. Elena's manager asks one thing: "How good is this model?" In this lesson you will answer in two ways: first with a single number (74 / 100), then with a table that shows which unit gets mixed up with which.
By the end of the lesson you will be able to hand the manager this short report. The first line is the overall result of the model; the lines below show how many of each unit's own questions it got right:
OutputAccuracy: 74 / 100 = 0.74
enrollment 19 / 21
aid 22 / 24
residence 3 / 22
courses 28 / 29
it 2 / 4Don't worry about what these lines mean yet. You will work out each one yourself as the lesson goes on.
Trying a few questions and seeing the right unit feels reassuring, but the questions you try are the ones that come to your mind. If you picked easy questions, the model looks great. It might still be wrong on a whole kind of question you never thought of. To measure, you have to compare the model's answers with questions whose correct unit is known, one by one.
Elena had prepared for this from the start. As with the 12 held-out questions in lesson 8, but at a larger scale, she set aside the last 100 of the 600 questions in the archive without showing them to the model; the model stored only the first 500. The correct unit of these held-out questions is written in the archive, but the model has never seen them, just like the questions that will arrive tomorrow. We will talk about why we measure with unseen questions in lesson 11; for now, think of these 100 questions as an exam paper.
The steps of measuring are always the same: for each held-out question the model makes a prediction, and you put the prediction next to the question's true unit. If the two are the same, the prediction is correct; if they differ, it is wrong.
Put the steps of measuring a model in the right order.
Click the lines in order; to undo one you placed wrong, click it in the list below.
Shuffled lines
Your order
The simplest measure is accuracy: the number of correct predictions divided by the total number of predictions. Let's start with a small measurement. Elena picked 10 of the held-out questions. In the top list you see the true unit of each question, and in the bottom list the model's prediction in the same order.
Example 1 (case): Accuracy on 10 questions
actual = ["enrollment", "aid", "residence", "residence", "courses", "aid", "residence", "courses", "enrollment", "it"] predicted = ["enrollment", "aid", "courses", "courses", "courses", "aid", "residence", "courses", "enrollment", "courses"] correct = 0 for a, p in zip(actual, predicted): if a == p: correct += 1 print("Correct:", correct, "/", len(actual)) print("Accuracy:", correct / len(actual))
OutputCorrect: 7 / 10
Accuracy: 0.7You don't need to read the code. It does the same job you would do by hand. Walk through the lists pair by pair: question 1 is enrollment-enrollment (correct), question 2 is aid-aid (correct), question 3 is residence-courses (wrong), and so on. Seven of the ten pairs match: accuracy is 7 / 10 = 0.7.
In the real measurement on 100 questions, the model named the true unit for 74 of them: accuracy 74 / 100 = 0.74, or 74 percent. The calculation is so easy that accuracy goes on the first line of every report. When you write it, give the fraction too. "74 percent" alone does not say how many questions it was measured on. A model that gets three of four questions right also says "75 percent", while 74 / 100 shows that the result rests on 100 questions.
Another model got 90 of 120 held-out questions right. What is its accuracy?
Don't expect 100 percent. Remember the questions left to a human in lesson 3: the archive has 30 questions that fit two units. One employee sent "Can you deduct my dorm fee from my scholarship?" to aid, another sent it to residence. When people disagree on a question, the model hits the "right" answer only by luck. In fact, 5 of the 26 mistakes come from questions like this. A good model is not a model that never makes mistakes; it is a model whose mistakes we understand.
The weak side of accuracy is that it throws all the mistakes into a single bag. There are 26 mistakes, but which ones? Are they spread evenly over the five units, or are they all in one place? The number does not say.
The model got 74 of the 100 held-out questions right. 22 of these 100 questions were residence questions. How many residence questions did the model get right?
The inside of the report card is the confusion matrix. Each row is a true unit, and each column is the unit the model named. The number in a cell is how many questions truly belonged to the row's unit and were sent by the model to the column's unit. Let's first build a small table from the 10 questions of Example 1.
Example 2 (case): The table for 10 questions
Each question is a pair: the true unit picks the row, the predicted unit picks the column, and you put a tally mark in that cell. Question 3 (true residence, predicted courses) goes into the courses column of the residence row. After ten tally marks the table looks like this:
| actual \ predicted | enrollment | aid | residence | courses | it |
|---|---|---|---|---|---|
| enrollment | 2 | 0 | 0 | 0 | 0 |
| aid | 0 | 2 | 0 | 0 | 0 |
| residence | 0 | 0 | 1 | 2 | 0 |
| courses | 0 | 0 | 0 | 2 | 0 |
| it | 0 | 0 | 0 | 1 | 0 |
There are 3 questions in the residence row: one went to the right place and two went to courses. The only question in the it row also went to courses. These two cells are exactly the three mistakes of Example 1.
Here is the table for Elena's 100 questions:
| actual \ predicted | enrollment | aid | residence | courses | it | total |
|---|---|---|---|---|---|---|
| enrollment | 19 | 0 | 0 | 2 | 0 | 21 |
| aid | 0 | 22 | 2 | 0 | 0 | 24 |
| residence | 0 | 0 | 3 | 19 | 0 | 22 |
| courses | 0 | 1 | 0 | 28 | 0 | 29 |
| it | 1 | 0 | 0 |
Before you read a number from the table, know which question you are asking. Each question below looks at a different part of the table.
Match each question with where its answer is in the table.
For each row, pick its match from the list, then check.
The cells running from the top left to the bottom right are called the diagonal. They are the places where the row and the column are the same unit, so they hold the correct predictions. Add up the diagonal: 19 + 22 + 3 + 28 + 2 = 74. Accuracy sits inside the table: the sum of the diagonal divided by the sum of all cells, 74 / 100. Every number outside the diagonal is a type of mistake. A row total tells you how many questions a unit really received, and a column total tells you how many questions the model sent to that unit.
The report at the start of the lesson also comes from this table: for each unit, you divide the diagonal cell by the row total.
Example 3 (case): A report from the table
units = ["enrollment", "aid", "residence", "courses", "it"] # Row: true unit, column: the model's prediction table = [ [19, 0, 0, 2, 0], [0, 22, 2, 0, 0], [0, 0, 3, 19, 0], [0, 1, 0, 28, 0], [1, 0, 0, 1, 2], ] correct = sum(table[i][i] for i in range(5)) total = sum(sum(row) for row in table) print("Accuracy:", correct, "/", total, "=", correct / total) for i, unit in enumerate(units): print(unit, table[i][i], "/", sum(table[i]))
OutputAccuracy: 74 / 100 = 0.74
enrollment 19 / 21
aid 22 / 24
residence 3 / 22
courses 28 / 29
it 2 / 4The residence row stands out. Try reading it.
The residence row of the table is: enrollment 0, aid 0, residence 3, courses 19, it 0. What does this row say?
Columns talk too. Add up the courses column from top to bottom: 2 + 0 + 19 + 28 + 1 = 50. The model said "courses" for 50 of the 100 questions, yet there are only 29 real courses questions. Someone who looks at the courses row sees 28 correct out of 29 and is happy. An employee in the courses unit sees that 22 of the 50 questions on their desk belong to other units. The row answers "where did this unit's questions go?" and the column answers "who ended up in this unit?" They can tell two very different stories about the same unit.
What the 74 percent was hiding has come out: enrollment, aid, and courses questions mostly reach their place, but residence questions almost all go to the courses unit. The biggest cell outside the diagonal, the most confused pair, is residence → courses (19). The next biggest mistake is only 2.
The table does not give the cause; it tells you where to look. When Elena followed that row, she found the cause. In lesson 5 you saw that some residence questions that arrived by email were wrongly labeled courses. In the archive, the questions that contain only one residence word have the same vector, and 3 of the 59 records with that vector are labeled courses. When a new residence question arrives, all of these records are at distance zero, and the model sorts neighbors at equal distance alphabetically: because "courses" comes before "residence", the first three neighbors are always those three wrongly labeled records. Without the table, this mistake would have been lost inside the 74 percent.
The manager has seen the table and says: "74 percent is a good result, let's go live." What do you say?
Pick an option and see what happens. There are no points; you can try the other options too.
Two models can have the same accuracy and still make very different mistakes. Below is the 10-question measurement from Example 1. Change the model's prediction for the third question (true unit residence) and watch the accuracy and the list of mistakes.
Change the model's prediction for question 3: how do the accuracy and the list of mistakes change?
Pick a value; predict the output first, then look.
1third = "courses"2actual = ["enrollment", "aid", "residence", "residence", "courses",3 "aid", "residence", "courses", "enrollment", "it"]4predicted = ["enrollment", "aid", third, "courses", "courses",5 "aid", "residence", "courses", "enrollment", "courses"]6correct = 07mistakes = {}8for a, p in zip(actual, predicted):9 if a == p:10 correct += 111 else:12 mistakes[(a, p)] = mistakes.get((a, p), 0) + 113print("Accuracy:", correct / len(actual))14for (a, p), count in mistakes.items():15 print(a, "->", p, count)
Accuracy: 0.7 residence -> courses 2 it -> courses 1
Faded lines didn't run with this value. When the prediction is "residence", accuracy goes from 0.7 to 0.8. With "courses" and "aid" the accuracy is the same (0.7), but the list of mistakes is different: in one, two residence questions go to courses; in the other, one goes to courses and one to aid. Same number, different mistakes.
Accuracy counts only the diagonal; it does not look at where the numbers outside the diagonal sit. So when you compare two models, you do not stop at the accuracy number; you put their tables side by side.
The confusion matrix is not specific to the help desk. It is built the same way in any system that sorts things into classes.
Example 4 (outside the case): Fruit sorting machine
A fruit seller is testing a machine that looks at fruit with a camera and sorts it as apple, pear, or plum. Here is the table for a test with 60 fruits:
| actual \ predicted | apple | pear | plum | total |
|---|---|---|---|---|
| apple | 18 | 2 | 0 | 20 |
| pear | 3 | 12 | 5 | 20 |
| plum | 0 | 4 | 16 | 20 |
The diagonal is 18 + 12 + 16 = 46; accuracy is 46 / 60, about 0.77. The biggest cell outside the diagonal is the plum column of the pear row: 5 pears went to the plum crate, so the most confused pair is pear → plum. Only 12 of the 20 pears are correct in the pear row; the seller should check the setup on pears first.
Looking at each row, which fruit does the machine sort correctly most often?
As you can see, the method is the same: the diagonal gives the correct predictions, the biggest cell outside the diagonal gives the most confused pair, and the row shows each class's own success.
The table in Example 4 is read the same way as the report in Example 3.
fruits = ["apple", "pear", "plum"] # Row: true fruit, column: what the machine said table = [ [18, 2, 0], [3, 12, 5], [0, 4, 16], ] correct = sum(table[i][i] for i in range(3)) total = sum(sum(row) for row in table) print("Accuracy:", correct, "/", total, "=", round(correct / total, 3)) for i, fruit in enumerate(fruits): print(fruit, table[i][i], "/", sum(table[i]))
OutputAccuracy: 46 / 60 = 0.767
apple 18 / 20
pear 12 / 20
plum 16 / 20The filled cells outside the diagonal are listed one by one, and the most frequent mistake is put on top. The six lines add up to 26, which is all the mistakes.
units = ["enrollment", "aid", "residence", "courses", "it"] table = [ [19, 0, 0, 2, 0], [0, 22, 2, 0, 0], [0, 0, 3, 19, 0], [0, 1, 0, 28, 0], [1, 0, 0, 1, 2], ] mistakes = [] i, actual (units): j, predicted (units): i != j table[i][j] > 0: mistakes.((table[i][j], actual, predicted)) count, actual, predicted (mistakes, reverse=): (actual, , predicted, count)
Mixing up the row and the column. An intern was counting a table from a list of five questions and wrote each pair the wrong way around: the prediction first, then the truth. The code gives no error, but the table is flipped and you blame the wrong unit:
actual = ["residence", "residence", "residence", "courses", "courses"] predicted = ["courses", "courses", "residence", "courses", "courses"] table = {} for a, p in zip(actual, predicted): table[(p, a)] = table.get((p, a), 0) + 1 print("residence -> courses:", table.get(("residence", "courses"), 0)) print("courses -> residence:", table.get(("courses", "residence"), 0))
Outputresidence -> courses: 0
courses -> residence: 2Someone reading the output would say "courses questions are going to residence". But in the lists, two residence questions went to courses, and not a single courses question went to the wrong place. The correct key is table[(a, p)]: the true unit first (row), then the prediction (column). When you read a table by hand, ask the same question: "In which true unit's row is this number?"
Next lesson: so far we counted all mistakes as equal. But missing a student's aid question and leaving one extra question for Elena do not cost the same. We will look at which mistake is more expensive.
Exercises and quiz are for members
This lesson has 7 coding exercises and a 10-question quiz. Sign up free to solve them, save your progress and move on to the next lesson.
| 2 |
| 4 |
Outputresidence -> courses 19
enrollment -> courses 2
aid -> residence 2
it -> enrollment 1
it -> courses 1
courses -> aid 1