Exercise: Explain the Energy Bill

Write three functions that measure which features Greenway Home Energy's random forest leans on with permutation importance on its test homes, pick the few that matter most, and turn them into four plain sentences for a homeowner, and let pytest check the call's settings, the sorting and its ties, and every sentence.

Freelancer · AI Basics · Python · Updated

By

Greenway Home Energy's customers keep asking why their estimates are what they are, and the team wants every answer to come from the model's own numbers, in the same plain words each time. You'll write the three functions behind that answer: one that measures what the forest leans on, one that picks the few features that matter most, and one that turns them into sentences a homeowner can read. Tests call each function and check what it gives back, and a notebook comes with the exercise for trying things as you work.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-ml-explain-the-energy-bill, 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-explain-the-energy-bill.git

Git makes a folder, exercise-ml-explain-the-energy-bill, 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:

  • home_energy.csv: the 700 homes, our own made-up data, the same file the chapter's lessons use.
  • 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.
  • bill_model.py: the model to explain, ready to use: split_homes(homes, seed) gives 560 training homes and 140 test homes, and build_bill_model(seed) gives the chapter's pipeline, one-hot encoding and a random forest of 200 trees.
  • explain.py: the three functions, for you to finish, with NAMES (each column's name in a homeowner's words), ready to use.
  • explore.ipynb: a notebook for looking at the homes, trying your code and drawing the partial dependence from How a Guess Changes with One Feature. Choose the .venv kernel with Select Kernel (top right) before you run it. The tests don't read it.
  • report.py: trains the forest, measures it and explains it with your three functions, printing what each step gave. 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 homes. 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 explain.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, with examples:

def top_features(table, n):
    """Return the names of the first n features in a sorted importance table, as a list.

    Leave out every feature whose importance is 0 or less: shuffling it didn't make
    the guesses worse, so it isn't something the model leans on. If fewer than n
    features are left, return them all.

    With the lesson's table (size_sqft 16.31, insulation 10.82, occupants 4.15,
    heating 2.83, has_pool 1.67, year_built -0.01):
    top_features(table, 3)  -> ["size_sqft", "insulation", "occupants"]
    top_features(table, 10) -> ["size_sqft", "insulation", "occupants", "heating", "has_pool"]
    """
    raise NotImplementedError("Not built yet.")

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

  1. importance_table(model, X_test, y_test, seed) calls permutation_importance on the fitted model and the test homes with n_repeats=10, random_state=seed and scoring="neg_mean_absolute_error", so each importance is in dollars added to the average miss. It returns a DataFrame with one row per column of X_test and three columns: feature (the column's name), importance (importances_mean) and spread (importances_std), not rounded. The rows are sorted largest importance first; when two importances are equal, the features go in alphabetical order; and the rows are numbered 0, 1, 2 and so on in their new order.

  2. top_features(table, n) returns the names of the first n features in the table, as a list, leaving out every feature whose importance is 0 or less: ["size_sqft", "insulation", "occupants"] for n of 3 with the lesson's table, and only the five above 0 for n of 10.

  3. plain_summary(top, mae) returns four sentences, as a list of strings. Given ["size_sqft", "insulation", "occupants"] and an average miss of 11.27, it returns exactly:

    This model estimates a home's monthly energy bill.
    On homes it hadn't seen, its estimates were off by about $11 a month on average.
    It leans most on the home's size, its insulation and how many people live there.
    That's what the model uses, not proof of what causes a bill.
    

    The average miss is rounded to whole dollars with round(). Each feature's words come from NAMES. One feature reads It leans most on its insulation.; two read It leans most on its insulation and its heating.; three or more get commas between them and and before the last. With no features at all, the third sentence is It doesn't lean clearly on any one feature.

The importance is measured on the test homes, never the training homes: that's what makes it say which columns help on homes the model never saw (Permutation Importance).

Check your work

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

python -m pytest

At first all 16 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 top_features(the lesson's table, 3) should return ['size_sqft', 'insulation', 'occupants'], but it returned ['size_sqft', 'insulation']. No test checks how important a column "should" be: the importance tests compare your table with scikit-learn's own permutation_importance, called with the same settings and the same seed, so the answers are exact.

To see your functions explain the real forest, run python report.py. It takes a little while: 200 trees, and each column shuffled 10 times.

Hints

  • Look before you write. Run the notebook's first cells: homes.head(10) shows the columns, and the cell that trains the model gives you model, X_test and y_test to try your functions on.
  • One call, three settings. Permutation Importance shows the call. Its result has importances_mean and importances_std, one number per column, in the order of X_test.columns.
  • Sort by two columns. sort_values(["importance", "feature"], ascending=[False, True]) sorts by importance from largest down, and by name from A to Z when two importances are equal. reset_index(drop=True) numbers the rows again from 0.
  • Pick rows with a condition. table[table["importance"] > 0] keeps the rows above 0, and .head(n) the first n of them; list(...) turns a column into a plain list.
  • Words, not column names. NAMES["size_sqft"] is the home's size. ", ".join(...) puts commas between a list of words.

When you're done

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

1) homes: 700
2) training homes: 560
3) test homes: 140
4) average miss on the test homes: 11.27
5) size_sqft: 16.31 (spread 1.01)
6) insulation: 10.82 (spread 1.08)
7) occupants: 4.15 (spread 0.54)
8) heating: 2.83 (spread 0.40)
9) has_pool: 1.67 (spread 0.33)
10) year_built: -0.01 (spread 0.18)
11) the top three: ['size_sqft', 'insulation', 'occupants']
12) This model estimates a home's monthly energy bill.
13) On homes it hadn't seen, its estimates were off by about $11 a month on average.
14) It leans most on the home's size, its insulation and how many people live there.
15) That's what the model uses, not proof of what causes a bill.

Now compare lines 5 to 10 with what seed_data.py planted: which column has no effect of its own, and where did it land? In the notebook, train a forest without the insulation column (Permutation Importance shows how) and make the table again: what happens to year_built, and what would sentence 14 say then? Would that sentence be good advice for a homeowner?

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-explain-the-energy-bill), 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-explain-the-energy-bill.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-explain-the-energy-bill, 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 model's explanation built from its own numbers, measured on homes it never saw and said in a homeowner's words, is one a team can repeat for every customer and defend to every one of them.

Key terms

Related