Đại học Kinh tế Quốc dân – National Economics UniversityMICRO4ASIA
🏆 Arena
Sign inSign up
MICRO4ASIAThe six micro-credentials

Courses are delivered on the Limio learning platform. Limio

← Fundamental of Machine Learning

You are viewing a public preview

Sign in to save your progress, do assignments, join discussions and get feedback.

Sign in
Module 1: Data Preparation and Model Foundations

1.6 Data Table Terminology and Data Splitting

Print / Save as PDF

1.6 Data Table Terminology and Data Splitting

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.

Key points

TermWhat it is on the table
Sample (object, record, item, observation)One row
Feature (attribute)One input column
Target / labelThe column to be predicted (for example price)
Feature vectorOne row, excluding the target column
  • Data is split into a Train set, a Validation set, and a Test set; the Train set is the largest part.

Study notes

By the end of this lesson you can:

  • Classify each column of the housing table by feature type and identify the target.
  • Use the standard vocabulary: sample, feature, target, feature vector, and their synonyms.
  • Write the feature vector of a given row.
  • Explain the roles of the train, validation and test sets and split a dataset into all three.

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).

Reading the housing table

The lecture returns to the house price table with the columns square_feet, tier, city, has_garden and price.

ColumnType
square_feetNumerical, continuous
tier (Affordable, Mid-range, Luxury)Categorical, ordinal
city (Hanoi, Ho Chi Minh City, Hue)Categorical, nominal
has_gardenCategorical, boolean
priceThe target: a continuous number we want to predict

Practise classifying every column of any dataset like this. You will use the skill constantly.

Essential vocabulary

Different books use different words for the same ideas. Learn the groups once:

ConceptSynonymsIn the table
Sampleobject, record, item, observation, instance, data pointOne row (one house)
Featureattribute, input variable, predictorAn input column: square_feet, tier, city, has_garden
Targetlabel, output variable, dependent variableThe column to predict: price

Feature vector

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.

Splitting the data: train, validation, test

Before training, divide the full dataset into three separate parts. The lecture compares them to a course:

SetSizeRoleClassroom analogy
Train setThe largest portionThe data the model actually learns fromPractice exercises
Validation setSmallerChecking progress while experimenting: choosing algorithms and settingsA practice quiz midway through the course
Test setSmaller, held outUsed once at the very end for a final, honest, unbiased scoreThe 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.

Worked example

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.

Common mistakes

  • Including the target in the feature vector. The target is what you predict; it can never be an input.
  • Evaluating on training data. The score will be far too optimistic because the model has already seen those rows.
  • Using the test set to tune the model. Tuning belongs to the validation set; the test set is used once, at the end.
  • Getting lost in synonyms. Attribute, predictor and feature are the same idea; so are label and target; so are record, observation and sample.

Summary

  • Each row is a sample; each input column is a feature; the column to predict is the target.
  • A feature vector is one row's feature values, without the target.
  • Split data into train (largest), validation and test sets.
  • Validation is for comparing and tuning; test is the final, one-time exam.
  • Never evaluate a model on the data it was trained on.

Flashcards · 1.6 Data Table Terminology and Data Splitting

9 words
WordMeaningExample
  • Sample

    One row of a dataset, representing one individual example; also called record, observation, object or item.

    One house in the housing table.

  • Feature (attribute)

    An input column used to make a prediction; also called predictor or input variable.

    square_feet, tier, city, has_garden

  • Target (label)

    The column whose value the model tries to predict; also called output or dependent variable.

    price

  • Feature vector

    All feature values of one sample packed into a list, excluding the target.

    [65, "Mid-range", "Hanoi", "No"]

  • Train set

    The largest part of the data, used by the model to learn.

    70% of the rows.

  • Validation set

    A held-out part used to compare models and tune settings during experiments.

    Like a practice quiz midway through a course.

  • Test set

    A part kept untouched until the end and used once for an honest final score.

    Like a final exam with unseen questions.

  • train_test_split()

    scikit-learn function that randomly splits features and target into two parts.

    train_test_split(X, y, test_size=0.3, random_state=42)

  • Optimistic evaluation

    A misleadingly high score obtained by testing a model on data it was trained on.

    Near-perfect accuracy on rows the model memorised.