Up Next: Predicting Numbers

Look back at what Module 1 taught you, from a model as a rule learned from examples to the statistics that describe a table, try a first look at a problem of your own, and see what Module 2 unlocks, a first real model that predicts a number such as a home's price.

Apprentice · AI Basics · Python · Updated

By

After this lesson you can

  • Describe what Module 2's first models will let you do with a table's features and label, such as predicting a home's price from its size

Key takeaways

  • After Module 1 you can say what a model is, when machine learning fits a problem and when it doesn't, and how a model is tested honestly against a baseline.
  • You can turn a table into features and a label, and describe each column with its middle, its spread and its correlation with the label.
  • Module 2 trains the first real models, straight lines through home prices, then measures how far off their guesses are and tries curves and trees.

At the start of this module, "machine learning" may have sounded like something that happens inside big companies. Now you know it's a rule learned from examples, you know how to check whether that rule is any good, and you can look at a table of data and say what's in it. That's the ground every model in this course stands on.

This lesson looks back at what you can do, gives you a first look to try on a problem of your own, and shows you what Module 2 adds: models that guess a number.

What you can do now

After Module 1, you can:

  • work like a machine learning project: a virtual environment with exact package versions, a notebook to explore in, a .py file of functions that pytest checks, and Git to get and hand in your work;
  • handle a table with pandas: load a CSV, take a first look with head, info and describe, count with value_counts, pick rows, and find and fill gaps;
  • say what a model is: a rule learned from examples, the kinds of machine learning, and the problems where a written rule or a query does better;
  • test honestly: training data and test data the model never saw, the most recent days held back when the data is in time order, and a baseline guess every model has to beat;
  • describe the data a model needs: X and y, the mean, median and standard deviation, percentiles, a histogram's shape, and correlation, with what it can't show.

Put it together: a first look of your own

Here's a way to prove it to yourself. Pick a question you'd like a number guessed for: how long your commute will take, how many people will come to a club's meeting, what a used bike will sell for. Then, on paper or in a notebook:

  • What's the label? The one number you want guessed, in its units.
  • What are the features? Three columns you'd know before the guess. Cross out anything only known afterward, and any ID.
  • What's the baseline? The guess with no model at all: the average of past values.
  • What would you check first? Each feature's middle and spread, and whether it moves with the label in a scatter plot.

If you can't name enough past examples, or a feature you'd know in time, that's a real answer too: When Machine Learning Is the Wrong Tool explains why.

What Module 2 unlocks

Module 2 builds the first real models: ones that guess a number. You've already met the idea behind the first one. Draw the straight line that best fits Riverside's size-and-rent scatter plot, and that line is a model:

import csv
import statistics

with open("listings.csv", newline="") as file:
    listings = list(csv.DictReader(file))

sizes = [int(listing["size_sqft"]) for listing in listings]
rents = [int(listing["monthly_rent"]) for listing in listings]
slope, intercept = statistics.linear_regression(sizes, rents)
print("Dollars per square foot:", round(slope, 2))
print("Starting point:", round(intercept, 2))
print("Guess for 900 square feet:", round(intercept + slope * 900))
Dollars per square foot: 1.43
Starting point: 148.15
Guess for 900 square feet: 1437

The seed script planted $1.50 a square foot, and a line through size alone found $1.43; Fitting a Line, Module 2's first lesson, explains what this line is and how it's chosen. Then Module 2 goes further:

  • Linear regression with scikit-learn: a home's price from its size, garage and age, with fit and predict, and train_test_split holding test homes back. The first chapter's example is Maple Grove's home sales.
  • Measuring error: how far off the guesses are, in dollars or minutes, with the mean absolute error, RMSE and R squared, always next to a baseline.
  • Beyond a straight line: curves and regression trees, for when a straight line misses the pattern, and how to compare them fairly.

By the end of it, a table goes in and a model comes out that guesses a number, with an honest measure of how far off it is.

Check yourself

2 questions to try on the lesson page.

So what

Module 1 taught you what a model needs and how to judge one; Module 2 builds them, starting with a straight line that turns a home's size into a price.

Sources

  • Python documentation, statistics.linear_regression: the slope and intercept of simple linear regression, estimated with ordinary least squares, from two sequences of the same length.

Key terms

Related