ENع
←Data and features
Lesson 02 · Foundations

Data and features

10 min9 steps5 questionsBeginner
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 2 · Interactive
01 / 09

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
Loading figure…
or ← →
Read the lesson step by step
  1. 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
  2. One 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,000
  3. The 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 table
  4. Features 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 job
  5. The 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 learning
  6. An 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 feature
  7. The 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] 400
  8. Hold 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 1
  9. A 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

Quiz Unlocks after the last step.