All projects

Machine Learning & MLOps

Building Energy Forecasting

Case study: A city aiming for carbon neutrality

Measuring how much energy a building uses takes time and money. Can we predict it from a simple description of the building?

Explore the map
Real model output
295 test buildings in Seattle. Solid ring: measured energy. Dashed ring: the model's estimate.

The problem

Case study: a city aiming for carbon neutrality needs to know how much energy its buildings use, but metering and reporting every building is slow and costly. The question is whether a model can predict a building's yearly energy use from what is already on file: its type, floor area, age, number of floors and location.

The data is real: the City of Seattle's open data on building energy for 2016, with 3,376 buildings. The project covers non-residential buildings only. The target is the total energy used on site in a year, corrected for the weather.

What the demo does

The map shows the 295 test buildings, the ones kept aside and never used to train the model. Each dot is colored by how close the prediction came: within 25%, 25 to 50% off, or more than 50% off. Tap one to see its measured energy use next to the prediction, in MWh per year, and the gap in percent. A checkbox adds the buildings used for training, without comparison, since the model has already seen them.

The form lets you describe a building: type (19 to choose from), floor area, year built, floors, neighborhood, and whether it uses natural gas or district steam. The estimate appears as you type, in MWh per year and in average US homes.

Everything runs in the browser. The model is a list of coefficients, 10 KB of data, and the demo files weigh 42 KB once compressed, plus the map library.

Explore the map

Results

0.73R² on 295 test buildings
The model accounts for 73% of the differences in energy use between buildings it never saw. 1 would be perfect, 0 no better than giving every building the same figure. Floor area does most of the work: shuffling that one column wipes out almost all of it.
1,023 MWhAverage error per building and year
About the yearly energy use of 45 average US homes. In the dataset's unit: 3.49 million kBtu. Big buildings make big errors; for comparison, the median test building uses 803 MWh a year.
57%Median error
Half the buildings are predicted within 57% of their measured use, and only about 1 in 4 within 25%. That is good enough to rank buildings and choose which ones to look at first, not to estimate a single building.

The data was cleaned down to 1,473 buildings, then split 80/20: 1,178 to train, 295 to test. On the test buildings, the final model (a Ridge regression):

  • R² 0.73: it accounts for 73% of the differences in energy use between buildings.
  • Average error 3.49 million kBtu, or 1,023 MWh per building and year, about what 45 average US homes use in a year. The root mean squared error is 7.33 million kBtu (2,148 MWh); it is higher because the largest buildings produce the largest errors.
  • Median error 57%: half the buildings are predicted within 57% of their real use, and 26% within 25%.

So the model is not precise for any single building. It is better at ordering them. When I checked this for the demo, 23 of the 30 test buildings that use the most energy were among the 30 the model ranks highest. Floor area does most of that work: shuffling it alone lowers R² by 0.72. The next most useful features, the floor area per floor and the type of use, each lower it by about 0.04.

I tried four model families before tuning. A baseline that always predicts the median scores an R² of -0.10 on the test set. A support vector regression with default settings did no better. Plain linear regression scored 0.76 on the test set but only 0.47 in cross validation: its score depended a lot on which buildings it was tested on. A random forest scored 0.64 in both, and 0.95 on its own training data, a clear case of overfitting. I then tuned both families with a grid search, using Ridge (a linear regression with a penalty on large coefficients) for the linear side. Ridge with alpha = 10 had the lowest test RMSE of the tuned models: 7.33 million kBtu, against 8.23 million for the best random forest. Untuned linear regression did slightly better on this particular test set (7.00 million), one more reason to judge models on cross validation (see the last section). For Ridge, predicting the logarithm of energy use and converting back made the error much worse (R² 0.20).

Limits:

  • 21 of the 295 test predictions are zero or negative, mostly small warehouses and offices. A linear model on raw energy figures can go below zero; the demo shows "≤ 0" and says why.
  • Neighborhood weighs a lot. The same described building gets anywhere from 622 to 1,620 MWh depending on where it is. A typical small or mid-sized office (3,450 m²) is estimated at 1,055 MWh, when the real median for that type is 624 MWh.
  • The data says nothing about how a building is used day to day: opening hours, equipment, insulation. Much of the remaining error probably comes from there.

How it's built

Architecture, stack and technical choices.
Architecture

Seattle open data 2016
3,376 buildings

Non-residential, compliant,
valid target: 1,473

Drop columns that
give away the answer

24 features:
size, use, age, location

Impute, scale, one-hot
149 columns

Ridge, alpha 10

model.json, 10 KB

predict.js in the browser

Map: measured vs predicted

Describe your building

Diagram as text
flowchart LR
    A["Seattle open data 2016<br/>3,376 buildings"] --> B["Non-residential, compliant,<br/>valid target: 1,473"]
    B --> C["Drop columns that<br/>give away the answer"]
    C --> D["24 features:<br/>size, use, age, location"]
    D --> E["Impute, scale, one-hot<br/>149 columns"]
    E --> F["Ridge, alpha 10"]
    F --> G["model.json, 10 KB"]
    G --> H["predict.js in the browser"]
    H --> I["Map: measured vs predicted"]
    H --> J["Describe your building"]

Stack

  • Python
  • pandas
  • NumPy
  • scikit-learn
  • Matplotlib
  • seaborn
  • JavaScript
  • MapLibre GL
  • OpenFreeMap
  • uv

The notebook:

  • Scope: multifamily and other residential buildings removed (3,376 to 1,629), only buildings whose report was marked compliant (1,510), a positive target and at least one floor (1,489), then the 0.5% lowest and highest consumers removed (1,473). Each step is logged with its count.
  • Leakage: I removed every column that gives the answer away or comes from it. That means energy per square foot, electricity, gas and steam consumption, greenhouse gas emissions, the ENERGY STAR score and the other version of total energy. Only what is known before measuring stays: size, use, age, location, mixed use, and whether the building has gas or steam at all.
  • New features: building age, floor area per floor, parking share, share of the main and second uses, a mixed-use flag. I then dropped columns that repeated others, such as the year built (the same information as the age) and the total floor area (0.98 correlation with the building's floor area).
  • One scikit-learn pipeline does the preparation: median imputation (0 for a missing second or third use), standard scaling, one-hot encoding capped at 30 categories per column. Because it sits inside the pipeline, each cross-validation round fits the preparation on its training folds only.
  • Grid searches with 5-fold cross validation: 576 combinations for the random forest, 28 for Ridge, scored on RMSE or MAE.

The demo:

  • The original repository saved no model, so I rebuilt it with a script that replays the notebook step by step. It keeps the same number of rows at every step, the same split and the same 149 columns, and lands within 0.0014% of the notebook's error. The tiny gap comes from the iterative solver.
  • The coefficients, scaling values and category encodings are exported to JSON. A JavaScript function with no dependency computes the prediction. A test compares it with scikit-learn on the 295 test buildings and 4 edge cases (missing values, unknown and rare categories): the largest relative difference is 2e-13.
  • Units: kBtu converted to MWh (1 kWh = 3,412 Btu, US Energy Information Administration). One average US home uses 76.8 million Btu a year, about 22.5 MWh (EIA, 2020 residential survey).
  • Map: MapLibre GL with OpenFreeMap tiles, which are free and need no key. On a phone, one finger scrolls the page and two fingers move the map.

Next steps

  • Choose the model on cross validation and keep the test set for one final check. On cross validation, the tuned random forest had the lower error (6.71 million kBtu against 7.38 million for Ridge); Ridge was kept because it did better on the test set, which means the test set helped pick the model.
  • Predict energy per square meter instead of the total, then multiply by floor area. Size would be built in, and the model could focus on what differs between buildings of the same size. I would try it with gradient boosting (XGBoost), which I listed as a next step at the end of the project.
  • Measure what the city would use the model for. If the goal is to pick which buildings to audit first, the right score is how many of the biggest consumers the model finds, not R² alone.
  • Clean the categories. The raw data spells some neighborhoods two ways ("BALLARD" and "Ballard"), and the model treats them as two places. The "Other" type groups 234 very different buildings.

Code and notebooks

Portfolio assistant AI

Answers are written by an AI from the content of this site. They can be wrong: check the sources it links. Questions are kept 90 days to improve the assistant, without your IP address. How it works