Exercise: Adoption Pipeline

Write three functions that build a ColumnTransformer to fill, scale and one-hot encode Happy Tails' 800 pets, put it in a pipeline with logistic regression, and train it on the training pets only, and let pytest check the steps, their settings, what each step learned, and a guess for a pet with a blank weight and a new color.

Freelancer · AI Basics · Python · Updated

By

Happy Tails Pet Adoption wants to know which new arrivals may wait a long time for a home, so it can feature them early, and its 800 past pets are the examples to learn from. Their records mix numbers, categories and blanks. You'll write the three functions behind the center's model: one that builds the preparation for every kind of column, one that puts it in a pipeline with a model, and one that trains that pipeline on the training pets only. Tests call each function and check what it gives back, and a notebook comes with the exercise for looking at the data while you work.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-ml-adoption-pipeline, also linked from the Exercise box beside the lesson. Clone it into your python-course folder. In VS Code's terminal:

cd ~/python-course
git clone https://github.com/ask-codey/exercise-ml-adoption-pipeline.git

Git makes a folder, exercise-ml-adoption-pipeline, with the exercise code inside. Open that folder in VS Code (File > Open Folder), open its terminal with Terminal > New Terminal, and make the exercise's virtual environment. On Windows (PowerShell):

python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt

On a Mac (zsh):

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

The prompt starts with (.venv) once the virtual environment is on. The exercise's README says what each command does, and what to do if PowerShell won't run the second one.

The folder holds:

  • pets.csv: the 800 pets, our own made-up data, the same file the chapter's lessons use. 40 weights and 24 colors are blank.
  • seed_data.py: the script that made the CSV, with the pattern it planted. You don't need to run it: the committed CSV is what keeps everyone's numbers the same.
  • adoption_model.py: the three functions, for you to finish, with NUMBER_COLUMNS (age_years, weight_lbs and has_photo), CATEGORY_COLUMNS (species, size and color) and LABEL (adopted_in_30_days), ready to use.
  • explore.ipynb: a notebook for looking at the pets and trying your code. Choose the .venv kernel with Select Kernel (top right) before you run it. The tests don't read it.
  • report.py: trains the model with your three functions and prints what it learned and one guess. Ready to use.
  • tests/: the tests, which call each function and check what comes back, and conftest.py, which gives each test a fresh copy of the pets. You don't need to change them.
  • requirements.txt and pytest.ini: the packages the exercise needs, at exact versions, and where pytest finds the tests and your code.

What to do

Every function in adoption_model.py is already declared with its name and parameters, and for now it stops with a NotImplementedError that says Not built yet. The docstring under each one says what it should do:

def build_adoption_model(number_columns, category_columns, seed):
    """Return a Pipeline (not fitted yet) with two steps, in this order:

    1. "prepare": build_preprocessor(number_columns, category_columns).
    2. "model": LogisticRegression(random_state=seed).

    LogisticRegression's default solver doesn't use random_state, but every class
    that takes one gets one in this course, so pass seed on.
    """
    raise NotImplementedError("Not built yet.")

In each function, replace the raise NotImplementedError("Not built yet.") line with your own code:

  1. build_preprocessor(number_columns, category_columns) returns a ColumnTransformer, not fitted yet, with two parts in this order: "numbers", a pipeline of SimpleImputer(strategy="median") then StandardScaler() on the columns in number_columns; and "categories", a pipeline of SimpleImputer(strategy="most_frequent") then OneHotEncoder(handle_unknown="ignore") on the columns in category_columns. Every other column is dropped, the ColumnTransformer's default. It uses the lists it's given: build_preprocessor(["age_years"], ["species"]) works on those two columns only.
  2. build_adoption_model(number_columns, category_columns, seed) returns a Pipeline, not fitted yet, with two steps: "prepare", the ColumnTransformer from build_preprocessor, and "model", LogisticRegression(random_state=seed).
  3. train_adoption_model(pets, seed) splits the pets with train_test_split: every column but pet_id and LABEL as X, the LABEL column as y, a quarter held back for testing, stratify=y, and seed passed on as random_state. It builds the model with NUMBER_COLUMNS, CATEGORY_COLUMNS and seed, fits it on the training part only, and returns model, X_test, y_test. With seed 42: 600 training pets, 200 test pets, and a learned median weight of 11.25 pounds, the training pets' median. (All 800 pets' median is 11.3, which is what a model fit on every pet would learn.)

Check your work

Run the tests from the exercise folder, with the virtual environment on:

python -m pytest

At first all 18 tests fail. That's expected: each function you finish turns its tests green. A failing test's message says what it called, what it expected and what came back, for example the medians the numbers part learned should return [3.2, 11.25, 1.0], but it returned [3.3, 11.3, 1.0], which is what a pipeline fit on all 800 pets would learn. No test checks how accurate the model is on the test pets, which depends on which pets were held back: the tests check the steps, their order and settings, that each step learned from the training pets only, and that the model guesses for a pet with a blank weight and a color it never saw.

To see your functions at work on the whole file, run python report.py.

Hints

  • Look before you write. Run the notebook's first cells: pets.isna().sum() shows which columns have blanks, and the value_counts() cell shows every category.
  • Two small pipelines inside one big one. Different Steps for Different Columns builds the same ColumnTransformer: each part is a (name, steps, columns) triple, and each part's steps can be made with make_pipeline.
  • Names come from you. The ColumnTransformer's parts are named "numbers" and "categories", and the outer pipeline's steps "prepare" and "model", so build the outer one with Pipeline([("prepare", ...), ("model", ...)]), as Pipelines: Every Step, Every Time does.
  • Use your own functions. build_adoption_model calls build_preprocessor, and train_adoption_model calls build_adoption_model.
  • Split first, then fit. train_test_split(X, y, test_size=0.25, stratify=y, random_state=seed) gives back four parts; fit the pipeline on the two training parts only.

When you're done

Every test is green: pytest's last line says 18 passed. And python report.py prints:

1) pets: 800
2) training pets: 600
3) test pets: 200
4) median weight learned: 11.25
5) most frequent color learned: white
6) columns the model gets: 15
7) test accuracy: 0.755
8) a puppy with no weight and a cream coat, chance of adoption: 0.92

Line 4 is the training pets' median, not all 800 pets'. Line 6 counts 3 scaled numbers and 12 one-hot columns, with intake_month dropped. Line 8 is a guess for a pet whose weight is blank and whose color the data never had: the pipeline filled one and ignored the other. In the notebook, try cross_val_score on build_adoption_model(NUMBER_COLUMNS, CATEGORY_COLUMNS, 42) and the training pets: how far is its mean from line 7?

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. Hand In Your Work with Git walks through each step.

  1. On GitHub, click +, then New repository. Name it (for example exercise-ml-adoption-pipeline), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-ml-adoption-pipeline.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-ml-adoption-pipeline, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

A pipeline that prepares every kind of column and trains on the training rows alone is the shape of almost every model you'll hand to someone else: it takes a raw row, blanks and new categories included, and gives a guess.

Key terms

Related