…
…
AI Fundamentals · Lesson 5 / 31 · Data, features, labels
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.
Elena put the help desk archive of 600 questions into two tables: questions.csv (the text of the question, the unit it went to, the channel it came from) and features.csv (numbers taken from the text). "How will the model read this?" she asks. In this lesson you will learn the language of the table, and you will find a label mistake that slipped through in the archive.
In Lesson 3 you built a count table for six questions: how many times each word appeared in each unit. That was a table too, but we had not yet named its parts. Before Elena's archive of 600 questions goes to a model, everyone needs to speak the same language: what each column is for, what the model will look at, what it will predict, and which columns will not be used at all. At the end of the lesson you will fill in the "table card" below. For now it is empty.
| Question | Answer |
|---|---|
| What is a row? | ? |
| Which column is the label, and what kind of problem is it? | ? |
| Which columns are the features? | ? |
| Which columns will not be used, and why? | ? |
| Who gave the label, and where can it be wrong? | ? |
In this lesson you will:
In machine learning, almost every job starts with a table. Each row of the table is an example: in Elena's table, a row is one question that one student wrote. The columns are what we know about that example, and they fall into two groups. The clues the model looks at are called features: how many words the question has, how many aid words it contains, which channel it came from. The answer the model will learn to predict is called the label. In Elena's table the label is the unit column: enrollment, aid, residence, courses, or it.
Below are the first three rows of Elena's second file, features.csv. The text of the question is not here; instead there are numbers taken from the text. aid_k is the number of aid words in the question, residence_k the number of residence words, and courses_k the number of courses words. The last column is the label.
| id | word_count | aid_k | residence_k | courses_k | unit |
|---|---|---|---|---|---|
| 1 | 8 | 2 | 0 | 0 | aid |
| 2 | 7 | 0 | 0 | 1 | courses |
| 3 | 6 | 1 | 0 | 0 | aid |
There is an important difference here. In the training data, the label is written in every row, because these questions went to a unit in the past and the result is known. When a new question arrives, there is no label; the model has to predict it. So during training the model sees both the question and the answer, and while it is in use it sees only the question.
You can think of a row as a small card, with each column name written next to its value. The code below keeps one row as such a card (a dictionary) and separates the label from it. You do not need to write the code; reading the lines and looking at the output is enough.
Example 1 (case): Splitting a row into features and label
row = {"word_count": 8, "aid_k": 2, "residence_k": 0, "unit": "aid"}
label = row.pop("unit")
print("Features:", row)
print("Label:", label)OutputFeatures: {'word_count': 8, 'aid_k': 2, 'residence_k': 0}
Label: aidWhat is left after the label is removed is exactly how a new question reaches the model: the numbers are there, the unit is not. Try these steps one by one on the card below.
Think of a row as a dictionary: touch the features and the label.
Press an operation; predict what Python will say first.
No operations yet. Press a button above.
Which column becomes the label is not decided by the table; we decide it, and that decision shapes the problem from the start. If we pick the unit column of the same table as the label, we build a router: "Which unit should this question go to?" If we pick the channel column, we build a very different model: "Which channel did this question come from?" Elena does not need that; for her, the channel can be at most a clue. So before you touch the data, the first job is to be clear about one question: what exactly do we want to predict?
Below is a small table of four rows. Change the value of target and see which answers the model will learn to choose from.
Change the label column: what does the model learn to predict?
Pick a value; predict the output first, then look.
1rows = [2 {"word_count": 8, "channel": "email", "unit": "aid"},3 {"word_count": 7, "channel": "web", "unit": "courses"},4 {"word_count": 6, "channel": "web", "unit": "aid"},5 {"word_count": 7, "channel": "mobile", "unit": "enrollment"},6]7target = "unit"8answers = sorted(set(r[target] for r in rows))9print("The model predicts", target + ":", answers)
The model predicts unit: ['aid', 'courses', 'enrollment']
Faded lines didn't run with this value. The label is a choice. If you pick unit, you build a router; if you pick channel, you build a very different model. If you pick a number such as the word count as the label, the model no longer learns a category but learns to say a number.
The results of the three tries show two different kinds of problem. In Lesson 2 you saw that a system that predicts says either a category or a number; now we can name the two kinds. When the label is one of a few categories (aid, residence, courses…), the problem is called classification. Elena's router is one. When the label is a number (how many hours a question takes to answer, how many minutes a bus is late), the problem is called regression (predicting a number). The kind is decided not by the columns in the table but by the label you choose: the same table can give you both a classification problem and a regression problem. In this course we will mostly work with classification.
Elena also added to the table how many hours it took to answer each question, and she wants the model to predict it. What kind of problem is this?
It is tempting to make every column a feature: the more information, the better the model, right? No. Before you make a column a feature, you ask one question: *will this information be available at the moment a new question arrives?*
In Elena's extended archive there is also a column called "the staff member who answered the question". This column gives away the unit almost perfectly, because each staff member sits in a single unit: every question that Aisha answers is an aid question. But when a new question arrives, we do not yet know who will answer it; that is exactly what we want to find out. Making such a column a feature is like looking at the answer key during an exam: the model looks great in training and falls apart in real life. The "reply time" column comes from the same exam; it cannot be known before the question is answered.
The channel, on the other hand, passes the exam: at the moment a question arrives, we know whether it came from the web, the mobile app, or email. But the channel says very little about the unit; aid questions and residence questions come through every channel. The channel can be a feature, but it is no miracle. There is also the id column: the row's serial number. It is known when the question arrives, but it carries no information about the unit; the number of question 89 does not tell you whether it is about aid or residence.
Match each column of Elena's extended table with its role.
For each row, pick its match from the list, then check.
aid_kword_countchannelunitanswered_byreply_hoursidAfter choosing the features, there is one more good habit: before building the model, look at how many examples each label has. If there are no examples of a unit, the model can never learn that unit; if there are very few, it will find it hard to learn.
Example 2 (case): Counting the labels
labels = ["aid", "courses", "aid", "enrollment", "residence", "aid", "it", "residence"] counts = {} for lab in labels: counts[lab] = counts.get(lab, 0) + 1 print(counts)
Output{'aid': 3, 'courses': 1, 'enrollment': 1, 'residence': 2, 'it': 1}This is a small trial with eight rows. When Elena ran the same count on all 600 questions, she got this table: 159 enrollment, 140 aid, 136 courses, 131 residence, and only 34 it. The IT questions are a minority; the model may find them harder to recognize than the others. Now try the count yourself.
Try it now· not graded; doesn't affect progress
The answer to the second try is important: a model that learns from an archive with only aid labels will say "aid" to every question, because it has seen no other answer. A model cannot go beyond the answers it has seen in the label column.
Everything we have discussed so far assumed that the label exists. Learning from labeled examples is called supervised learning; the word "supervised" comes from imagining a supervisor who writes the correct answer next to every example. All the models we build in this course are of this kind.
But sometimes there is no label. What if Elena's archive had never been routed to any unit? Something can still be done: a program can group the 600 questions into piles by how similar they are to each other. Questions with similar words fall into the same pile. Grouping similar examples without using labels is called unsupervised learning. Its limit is clear: piles form, but none of them carries the name "aid". The program only says "these look alike"; it is a person who looks at a pile and says "these are aid questions."
It is easier to see these three differences away from Elena's desk. A music app keeps the following information for every listening session:
| listen_min | genre | skips | liked | next_listen_min |
|---|---|---|---|---|
| 42 | pop | 1 | yes | 38 |
| 5 | jazz | 7 | no | 0 |
| 27 | rock | 2 | yes | 31 |
| 12 | pop | 5 | no | 9 |
| 55 | rap | 0 | yes | 60 |
Three separate questions can be built from the same table. For "Will this listener like the song?", the label is liked; the answer is yes or no, so it is classification. For "How many minutes will they listen in the next session?", the label is next_listen_min; the answer is a number, so it is regression. For "Group the listeners by similarity", there is no label; this is unsupervised learning, and the names of the groups ("morning runners", "late-night jazz fans") are again given by a person.
Example 3 (outside the case): Two labels in a music app
sessions = [
{"listen_min": 42, "genre": "pop", "skips": 1,
"liked": "yes", "next_listen_min": 38},
{"listen_min": 5, "genre": "jazz", "skips": 7,
"liked": "no", "next_listen_min": 0},
{"listen_min": 27, "genre": "rock", "skips": 2,
"liked": "yes", "next_listen_min": 31},
{"listen_min": 12, "genre": "pop", "skips": 5,
"liked": "no", "next_listen_min": 9},
{"listen_min": 55, "genre": "rap", "skips": 0,
"liked": "yes", "next_listen_min": 60},
]
for target in ["liked", "next_listen_min"]:
answers = sorted(set(s[target] for s in sessions))
print(target, "->", answers)Outputliked -> ['no', 'yes']
next_listen_min -> [0, 9, 31, 38, 60]The first label has two categories; the second is a number, and for a new listener a value never seen before, between 0 and 60 (for example 25), can also be predicted. There is also a third kind, where a system learns by trying and getting rewards (reinforcement learning); we will not use it in this course.
Elena ran a program that splits 600 unlabeled questions into 5 piles by similarity. What does the program give her?
A label is not information that sits in the data by itself and is always right. The staff at the desk wrote every unit name in Elena's archive: on a busy day, among hundreds of questions arriving one after another, sometimes in a hurry. People get tired, click the wrong row, and mix up two similar units. A label is a human decision, and a human decision can be wrong.
Looking closely at the archive, there is a real example. Here are two rows from questions.csv side by side:
Example 4 (case, real rows): The same question, two different labels
| id | question | channel | unit |
|---|---|---|---|
| 89 | Good afternoon, what's dorm sign-in time? | courses | |
| 105 | What's dorm sign-in time? | mobile | residence |
The two questions ask the same thing; row 105 was correctly written to the residence unit, while row 89 says courses. A person sees this mistake at a glance. The model cannot: for it, row 89 is a training example like any other, and it learns that the words "dorm sign-in time" sometimes go to the courses unit. If mistakes like this pile up, new residence questions with the same words will also be sent to the courses unit. The model cannot know whether labels are right or wrong; it learns whatever it sees.
It is also worth noting that row 89 came in by email. Whether wrong labels gather in a particular group ("residence questions that came by email") will come back later, in the lesson about who the data represents. Here it is enough to notice. The accept, fix, reject decision you built in Lesson 4 for the assistant's answers works here too: for a row with a wrong label, fixing it is most often the best response.
While browsing the archive, you see that the question "Good afternoon, what's dorm sign-in time?" has the label courses. What do you do?
Pick an option and see what happens. There are no points; you can try the other options too.
Features, like labels, can carry mistakes. Look at row 2 of features.csv: the question is "Good afternoon, computing my average? Many thanks." and the it_k value in the table is 1. The program that extracts the numbers saw the letters *comput* inside the word *computing* and took it for an IT word (computer, password). But the question belongs to the courses unit. The model does not know where this number came from; it uses whatever is written in the table. Features are only as good as the rule that extracts them. In the next lesson we will build this rule ourselves and see where such mistakes come from.
Now put what you have seen so far in order, as the steps of setting up a prediction problem.
Put the steps of setting up a prediction problem in order.
Click the lines in order; to undo one you placed wrong, click it in the list below.
Shuffled lines
Your order
Now you can fill in the table card. A row is one question from one student. The label is the unit column, and because it names one of five categories, the problem is classification. The features are the columns word_count, aid_k, residence_k, enrollment_k, courses_k, it_k, and channel. answered_by and reply_hours are not used, because they are not known when the question arrives; id stays out too, because it carries no information. The staff at the desk gave the labels, and in at least one row, a residence question that came by email, the label was wrongly written as courses.
If all the values in a column are numbers, the problem becomes regression when that column is chosen as the label; otherwise it is classification.
def find_type(values): if all(isinstance(v, (int, float)) for v in values): return "regression (predicting a number)" return "classification" table = { "unit": ["aid", "courses", "aid", "enrollment"], "channel": ["email", "web", "web", "mobile"], "reply_hours": [24, 2, 5, 2], } for column, values in table.items(): print(column, "->", find_type(values))
Outputunit -> classification
channel -> classification
reply_hours -> regression (predicting a number)Next to each column we write the answer to "is it known when the question arrives?"; features are chosen only from the known ones.
known_on_arrival = {
"word_count": ,
: ,
: ,
: ,
: ,
}
features = [c c, known known_on_arrival.items() known]
left_out = [c c, known known_on_arrival.items() known]
(, features)
(, left_out)Leaving the label among the features. If the unit column is also given as a feature when the model is trained, the model reads the answer directly and looks 100 percent correct in training. But on a new question the unit is not written, so this success is worth nothing. The same trap is set by columns like answered_by that almost give away the unit. The first question of someone who sees 100 percent in training should not be "great" but "did the answer stay among the features?" The label is always kept apart from the features.
Next lesson: where did the aid_k numbers in the table come from? We will turn a sentence into numbers ourselves.
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.
OutputFeatures: ['word_count', 'channel', 'aid_k']
Not used: ['answered_by', 'reply_hours']