Features and target
A model that predicts an apartment's price needs two things: the input data and the answer to look for. That is why examples are written as a table. Each row is one sample, here an apartment. Each column is one of its properties.
| Area, m² | To center, km | Price, mln |
|---|---|---|
| 30 | 5 | 3.1 |
| 45 | 2 | 4.4 |
| 60 | 3 | 6.2 |
| 80 | 1 | 7.9 |
A feature is a column the model gets as input: the area and the distance to the center. The target is the column the model learns to predict: here, the price. There can be any number of features, but there is usually one target.
X and y
The features of all samples go into a table X: a row is a sample, a column is a feature. The target values go into a list y in the same row order. The capital X is a reminder that it is a table, the lowercase y that it is a single column.
X = [[30, 5], [45, 2], [60, 3], [80, 1]]
y = [3.1, 4.4, 6.2, 7.9]
print(len(X), len(y)) # 4 4
print(X[1], y[1]) # [45, 2] 4.4The row X[1] and the number y[1] describe the same apartment. X and y always have the same number of rows: each sample has one target value. If the row order drifts apart, the model learns from someone else's answers.
The task sets the target
Which column is the target depends on the task, not on the type of data in it. A bank wants to know whether a client will repay a loan. Its features are income, age and debt, and the target is the "repaid" column with the values "yes" and "no". A target does not have to be a number.
The same apartment table also fits another task: to predict the distance to the center, that column becomes the target.
The target value is known only in the collected examples. For a new client, it is exactly what has to be predicted. Tasks where every example has a known target value are called supervised learning. The supervision comes from the correct answers in the data.
Common mistake
The target is left among the features:
X = [[30, 5, 3.1], [45, 2, 4.4], [60, 3, 6.2], [80, 1, 7.9]] # ✗
y = [3.1, 4.4, 6.2, 7.9]The third column of X matches y. The model finds the rule "the answer is the third feature" and never errs on the examples. But a new apartment's price is unknown, so the third feature cannot be filled in. Such a model cannot be used.
X holds only what is known before the answer.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.