Data and features
About this lesson
Machine learning learns from data, so it pays to know exactly what data looks like to a model. Watch five real house sales from Stanford's CS229 course notes become the rows of a table, see which columns are features (the inputs) and which is the label (the answer), split one house into the numbers x and y, do the same in a few lines of Python, and hold one row back as a test set.
Data: a pile of examples
Machine learning learns from data: a collection of examples. Ours are houses sold in Portland, Oregon. Each listing records three things: the living area, the number of bedrooms and the price the house sold for.
data a collection of examples example one thing we measured: here, one house each listing records: living area in square feet bedrooms a count price what it sold for, in $1000s
Read the lesson step by step
Data: a pile of examples
Machine learning learns from data: a collection of examples. Ours are houses sold in Portland, Oregon. Each listing records three things: the living area, the number of bedrooms and the price the house sold for.
data a collection of examples example one thing we measured: here, one house each listing records: living area in square feet bedrooms a count price what it sold for, in $1000sOne example, one row
One example is one thing we have measured: here, one house. In a dataset each example becomes one row of a table. The first house: 2,104 square feet, 3 bedrooms, sold for $400,000.
area beds price 1 2104 3 400 one example -> one row price 400 means 400 thousand dollars: $400,000The dataset is a table
Adding every house gives the dataset: one row per example. We show five; the full table in Stanford's CS229 course notes has 47 Portland houses. Real datasets often hold thousands or millions of rows.
area beds price 1 2104 3 400 2 1600 3 330 3 2400 3 369 4 1416 2 232 5 3000 4 540 5 rows shown, 47 in the full CS229 tableFeatures are the inputs
Each of these columns is a feature: one measurable property of every example. Here, living area and bedrooms. Features are the inputs, what we know about a house before anyone names its price.
feature one measurable property, one column area living area in square feet beds number of bedrooms other features we could add: bathrooms, a fireplace, the age of the house; choosing them is part of the jobThe label is the answer
The last column is the label, also called the target: the answer we want a model to learn to predict. Here, the sale price in thousands of dollars. Features are usually written x, the label y.
label (target) the answer to predict, one column price sale price in $1000s notation x = the features y = the label examples with labels: supervised learningAn example as numbers
A model sees an example as numbers. The first house's features form a vector, an ordered list of numbers: x = [2104, 3]. Its label is y = 400. Every row splits the same way.
row 1 [2104, 3, 400] x = [2104, 3] features: area, beds y = 400 label: price vector: an ordered list of numbers, one per featureThe same split in Python
In Python, the list of rows splits into X, every feature vector, and y, every label. r[:2] takes the first two numbers of a row; r[2] takes the third. Printing X[0] and y[0] shows the first house.
X = [r[:2] for r in rows] y = [r[2] for r in rows] print(X[0], y[0]) X -> [[2104, 3], [1600, 3]]... y -> [400, 330, 369, 232, 540] prints [2104, 3] 400Hold some data back
Before training we hold some rows back. The training set is what the model learns from. The test set stays hidden until the end, to check the model on houses it never saw. Here: 4 rows train, 1 tests.
X_train, y_train = X[:4], y[:4] X_test, y_test = X[4:], y[4:] training set rows 1-4 the model learns from these test set row 5 kept back to check it prints 4 1A new house, no label
Later a new house comes on the market: 1,800 square feet, 3 bedrooms, price unknown. We know its features but not its label. Predicting that missing y from x is the job of the model we train.
x = [1800, 3] known: the features y = ? unknown: the label training learn from examples where y is known predicting fill in y for a new x
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01In the house dataset, what is one example?
An example is one thing that was measured, here one house. In a dataset each example is one row; the columns are its features and its label.
02We want to predict a house's sale price from its living area and number of bedrooms. Which is the label?
The label (or target) is the answer the model learns to predict: the price. Living area and bedrooms are features, the inputs.
03A row of the table is [1416, 2, 232]: area, bedrooms, price. What are its feature vector x and its label y?
The features are the first two numbers, in order, and the label is the price. Putting the label into x as well would hand the model the answer.
04In Python, rows = [[2104, 3, 400], [1600, 3, 330]]. What does [r[2] for r in rows] give?
Python counts positions from 0, so r[2] is the third number of each row, the price. That list is y, the labels. [r[:2] for r in rows] gives the features X.
05Why keep a test set apart from the training set?
Test rows still have labels, but the model never trains on them. Comparing its predictions with those labels shows how well it does on data it has not seen.
Glossary
- Data
- The examples a machine learning program learns from.
- Example
- One thing that was measured, such as one house. In a table, one row.
- Dataset
- A collection of examples, usually laid out as a table: one row per example, one column per feature (and one for the label).
- Feature
- One measurable property of every example, such as living area. Features are the inputs, written x.
- Label (target)
- The answer a model learns to predict for each example, such as the sale price. Written y.
- Feature vector
- One example's features as an ordered list of numbers, such as x = [2104, 3].
- Training set
- The examples a model learns from.
- Test set
- Labelled examples kept back from training, used to check how well the model does on data it has not seen.
Notes and sources
- The five houses are the first rows of the Portland housing table in Stanford CS229's lecture notes (A. Ng, T. Ma): living area in square feet, bedrooms, and price in thousands of dollars. The full table has 47 houses.
- The new house (1,800 square feet, 3 bedrooms) is made up for the example; it is not in the CS229 data.
- Real splits pick rows at random and are much bigger, often with a third "validation" set for tuning. With five rows, the split only shows the idea.
- Libraries such as NumPy store X as a grid of numbers with one row per example and one column per feature; the Deep Learning book calls it a design matrix.
- The Python program in the lesson is real: run it and it prints [2104, 3] 400 then 4 1.
- Stanford CS229 Machine Learning, lecture notes (A. Ng, T. Ma): the Portland housing data, input features, target variable, training set
- Goodfellow, Bengio & Courville, Deep Learning (MIT Press, 2016), chapter 5: examples, features, the design matrix, training and test sets
- Google for Developers, Machine Learning Glossary: example, feature, label, training set, test set
- Google for Developers, Machine Learning Crash Course: dividing the original dataset
- Python Tutorial: lists, indexing and slicing
- Python Tutorial: list comprehensions
Quiz Unlocks after the last step.