You are viewing a public preview
Sign in to save your progress, do assignments, join discussions and get feedback.
After this unit you will: be able to read a data table in the language of machine learning and know which parts the data is split into.
| Term | What it is on the table |
|---|---|
| Sample (object, record, item, observation) | One row |
| Feature (attribute) | One input column |
| Target / label | The column to be predicted (for example price) |
| Feature vector | One row, excluding the target column |
By the end of this lesson you can:
Key idea: Never evaluate a model on the same data it was trained on. Split the data into a train set (to learn), a validation set (to compare and tune) and a test set (used once at the end for an honest score).
The lecture returns to the house price table with the columns square_feet, tier, city, has_garden and price.
| Column | Type |
|---|---|
square_feet | Numerical, continuous |
tier (Affordable, Mid-range, Luxury) | Categorical, ordinal |
city (Hanoi, Ho Chi Minh City, Hue) | Categorical, nominal |
has_garden | Categorical, boolean |
price | The target: a continuous number we want to predict |
Practise classifying every column of any dataset like this. You will use the skill constantly.
Different books use different words for the same ideas. Learn the groups once:
| Concept | Synonyms | In the table |
|---|---|---|
| Sample | object, record, item, observation, instance, data point | One row (one house) |
| Feature | attribute, input variable, predictor | An input column: square_feet, tier, city, has_garden |
| Target | label, output variable, dependent variable | The column to predict: price |
A feature vector is all the feature values of one sample, packed together as a list (a vector in mathematics). It does not include the target. For the highlighted house (65 square feet, Mid-range, Hanoi, no garden), the feature vector is [65, "Mid-range", "Hanoi", "No"]. Its price, 2.9, is the target. A model makes a prediction from one feature vector at a time, so the term appears constantly once you start coding.
Before training, divide the full dataset into three separate parts. The lecture compares them to a course:
| Set | Size | Role | Classroom analogy |
|---|---|---|---|
| Train set | The largest portion | The data the model actually learns from | Practice exercises |
| Validation set | Smaller | Checking progress while experimenting: choosing algorithms and settings | A practice quiz midway through the course |
| Test set | Smaller, held out | Used once at the very end for a final, honest, unbiased score | The final exam with unseen questions |
Why bother? A model tested on the data it learned from will look far better than it really is, because it has effectively memorised those examples. Only fresh, unseen data tells you how it will perform in the real world. If you remember one thing from this module, remember this rule.
Split 200 houses into 70% train, 15% validation and 15% test by calling train_test_split twice, then show one feature vector.
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
rng = np.random.default_rng(0)
houses = pd.DataFrame({
"square_feet": rng.integers(40, 120, 200),
"tier": rng.choice(["Affordable", "Mid-range", "Luxury"], 200),
"city": rng.choice(["Hanoi", "Ho Chi Minh City", "Hue"], 200),
"has_garden": rng.choice(["Yes", "No"], 200),
"price": rng.uniform(1.5, 6.0, 200).round(1),
})
X = houses.drop(columns="price") # features
y = houses["price"] # target
# 70% train, 30% left over
X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.30, random_state=42)
# split the 30% in half: 15% validation, 15% test
X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.50, random_state=42)
print(len(X_train), len(X_val), len(X_test))
house = {"square_feet": 65, "tier": "Mid-range", "city": "Hanoi", "has_garden": "No", "price": 2.9}
feature_vector = [house[c] for c in ["square_feet", "tier", "city", "has_garden"]]
print(feature_vector)
Expected output:
140 30 30
[65, 'Mid-range', 'Hanoi', 'No']
The train set is the largest part (140 rows). The validation and test sets have 30 rows each. The feature vector leaves out price, because that is what the model must predict.
Flashcards · 1.6 Data Table Terminology and Data Splitting
9 wordsOne row of a dataset, representing one individual example; also called record, observation, object or item.
An input column used to make a prediction; also called predictor or input variable.
The column whose value the model tries to predict; also called output or dependent variable.
All feature values of one sample packed into a list, excluding the target.
The largest part of the data, used by the model to learn.
A held-out part used to compare models and tune settings during experiments.
A part kept untouched until the end and used once for an honest final score.
scikit-learn function that randomly splits features and target into two parts.
A misleadingly high score obtained by testing a model on data it was trained on.