Evaluate a chronological energy regression
Fit training-only weather/calendar regressions, compare a mean baseline and inspect held-out energy errors.
Goal and how to run
This lesson assumes basic Python knowledge. Read each method and caption before changing parameters. AI exercises are optional; no AI account is needed for the core workflow. Data lives in notebook metadata, so there is no companion data download. Keep the notebook saved in the active working folder.
Your goal is to understand the research question, execute transparent calculations, check the results, and state the limits of the conclusion.
Setup and parameters
from pathlib import Path
import base64, gzip, hashlib, json, platform
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython.display import display, HTML
from datetime import datetime
from IPython.display import display as _display
def display(value):
if isinstance(value,pd.DataFrame):
table=value.to_html(max_rows=12,escape=True)
style="<style>.vibeit-readable-table{max-width:100%;overflow-x:auto}.vibeit-readable-table table{width:max-content;max-width:none;border-collapse:collapse}.vibeit-readable-table td,.vibeit-readable-table th{white-space:nowrap!important;word-break:normal!important;overflow-wrap:normal!important}</style>"
return _display(HTML(style+'<div class="vibeit-readable-table">'+table+'</div>'))
return _display(value)
RECIPE_ID='en02-energy-regression'
OUTPUT_DIR=Path.cwd()/"results"/RECIPE_ID
plt.rcParams.update({"figure.dpi":120,"font.size":10,"axes.spines.top":False,"axes.spines.right":False,
"axes.prop_cycle":plt.cycler(color=["#18785f","#416aa6","#bc6541","#9974a5"])})
print("Python:",platform.python_version(),"; NumPy:",np.__version__,"; Pandas:",pd.__version__)
print("Working folder:",Path.cwd())
print("Core data mode: embedded snapshot; no network requests")
Python: 3.13.13 ; NumPy: 2.5.3 ; Pandas: 3.0.6 Working folder: /Users/zhiluo/Developer/vibeit-cookbook/cookbook/downloads Core data mode: embedded snapshot; no network requests
Load the embedded snapshot
def load_snapshot(recipe_id, expected_sha256, snapshot_key):
"""Read embedded data from this notebook, including a renamed copy."""
for path in sorted(Path.cwd().glob("*.ipynb")):
try:
notebook = json.loads(path.read_text(encoding="utf-8"))
metadata = notebook.get("metadata", {}).get("vibeit_cookbook", {})
blob = metadata.get("snapshots", {}).get(snapshot_key)
if metadata.get("recipe_id") != recipe_id or not blob:
continue
if blob.get("sha256") != expected_sha256:
continue
raw = gzip.decompress(base64.b64decode(blob["gzip_base64"], validate=True))
if hashlib.sha256(raw).hexdigest() != expected_sha256:
raise ValueError("Embedded snapshot checksum mismatch: " + snapshot_key)
return json.loads(raw)
except (OSError, json.JSONDecodeError):
continue
raise FileNotFoundError(
"Save/open the complete .ipynb in the active working folder. "
"The embedded snapshot could not be located for " + recipe_id)
SNAPSHOT_HASHES={'energy': '81117ee0a0706bf416bedc50f23b455ee8dd771e588c91b6a420dc3b184f638f'}
def snapshot(key):
return load_snapshot(RECIPE_ID,SNAPSHOT_HASHES[key],key)
print("Embedded snapshots:",list(SNAPSHOT_HASHES))
Embedded snapshots: ['energy']
Read the method functions
def audit_numeric_table(frame,columns):
"""Audit observed rows; never silently impute or remove them."""
if frame.empty or frame.index.has_duplicates:
raise ValueError('Expected a nonempty table with unique row IDs')
missing=[name for name in columns if name not in frame.columns]
if missing:raise ValueError('Missing required columns: '+str(missing))
values=frame[columns].to_numpy(dtype=float)
if not np.isfinite(values).all():raise ValueError('Non-finite measurements; inspect missing values explicitly')
return {'rows':len(frame),'features':len(columns),'finite':True,'duplicate_measurement_rows':int(frame[columns].duplicated().sum())}
print("Method ready: audit_numeric_table")
Method ready: audit_numeric_table
def ridge_fit_predict(train_values,train_target,test_values,penalty=1.0):
"""Scale using training statistics and solve ridge by an augmented least-squares system."""
train_values=np.asarray(train_values,dtype=float);test_values=np.asarray(test_values,dtype=float)
target=np.asarray(train_target,dtype=float)
if train_values.ndim!=2 or len(train_values)==0 or target.shape!=(len(train_values),) or penalty<0:raise ValueError('Invalid training data or penalty')
if test_values.ndim!=2 or train_values.shape[1]!=test_values.shape[1]:raise ValueError('Training/test feature dimensions differ')
if not np.isfinite(train_values).all() or not np.isfinite(test_values).all() or not np.isfinite(target).all():raise ValueError('Non-finite training data')
mean=train_values.mean(axis=0);scale=train_values.std(axis=0);scale=np.where(scale==0,1,scale)
design=np.column_stack([np.ones(len(train_values)),(train_values-mean)/scale])
test_design=np.column_stack([np.ones(len(test_values)),(test_values-mean)/scale])
regularizer=np.sqrt(penalty)*np.eye(design.shape[1]);regularizer[0,0]=0
coefficients=np.linalg.lstsq(np.vstack([design,regularizer]),np.r_[target,np.zeros(design.shape[1])],rcond=None)[0]
return dict(predictions=test_design@coefficients,coefficients=coefficients,mean=mean,scale=scale,train_predictions=design@coefficients)
print("Method ready: ridge_fit_predict")
Method ready: ridge_fit_predict
def regression_metrics(truth,predictions):
truth=np.asarray(truth,dtype=float);predictions=np.asarray(predictions,dtype=float)
if truth.shape!=predictions.shape or truth.size==0 or not np.isfinite(truth-predictions).all():raise ValueError('Invalid regression comparison')
error=predictions-truth;denominator=np.sum((truth-truth.mean())**2)
return dict(mae=float(np.mean(np.abs(error))),rmse=float(np.sqrt(np.mean(error**2))),r_squared=float(1-np.sum(error**2)/denominator) if denominator>0 else float('nan'))
print("Method ready: regression_metrics")
Method ready: regression_metrics
def iso_datetimes(values):
"""Parse source ISO dates with the standard library; keep tables as ISO strings."""
return [datetime.fromisoformat(str(value)) for value in values]
print("Method ready: iso_datetimes")
Method ready: iso_datetimes
Data and model boundary
The building record contains real ten-minute energy and environmental observations from one source building. Airport weather fields include upstream interpolation and are not colocated calibration references. Beam, circuit and PID examples are declared numerical design models; computed responses do not establish hardware performance or commissioning acceptance.
Steps
1. Reserve a chronological test interval
Use the first 70% of timestamps for fitting and the final 30% for testing. A random row split would mix nearby and seasonally related observations. Target energy is Wh per interval, and this is conditional reconstruction using contemporaneous environmental variables.
energy=snapshot("energy");frame=pd.DataFrame(energy["rows"],columns=energy["columns"])
frame["timestamp"]=frame["date"].astype(str);frame=frame.sort_values("timestamp").reset_index(drop=True)
assert len(frame)==19735 and frame.timestamp.is_unique
dates=iso_datetimes(frame.timestamp)
assert all((b-a).total_seconds()==600 for a,b in zip(dates,dates[1:]))
audit=audit_numeric_table(frame,["Appliances","lights","T1","RH_1"])
assert frame.Appliances.ge(0).all() and frame.lights.ge(0).all()
frame["appliance_average_power_W"]=frame.Appliances/(600/3600)
print("Observed rows:",len(frame),"; energy: Wh/10 min; derived interval-average power: W; source timezone unspecified")
display(frame[["timestamp","Appliances","appliance_average_power_W","T1","RH_1"]].head(8))
split=int(.7*len(frame));train=np.arange(len(frame))<split;test=~train
frame["hour_sin"]=np.sin(2*np.pi*(np.array([value.hour+value.minute/60 for value in dates]))/24)
frame["hour_cos"]=np.cos(2*np.pi*(np.array([value.hour+value.minute/60 for value in dates]))/24)
features=["T1","RH_1","T_out","RH_out","hour_sin","hour_cos"]
assert frame.loc[train,"timestamp"].max()<frame.loc[test,"timestamp"].min()
print("Training/test rows:",int(train.sum()),int(test.sum()))
Observed rows: 19735 ; energy: Wh/10 min; derived interval-average power: W; source timezone unspecified
| timestamp | Appliances | appliance_average_power_W | T1 | RH_1 | |
|---|---|---|---|---|---|
| 0 | 2016-01-11 17:00:00 | 60 | 360.0 | 19.890000 | 47.596667 |
| 1 | 2016-01-11 17:10:00 | 60 | 360.0 | 19.890000 | 46.693333 |
| 2 | 2016-01-11 17:20:00 | 50 | 300.0 | 19.890000 | 46.300000 |
| 3 | 2016-01-11 17:30:00 | 50 | 300.0 | 19.890000 | 46.066667 |
| 4 | 2016-01-11 17:40:00 | 60 | 360.0 | 19.890000 | 46.333333 |
| 5 | 2016-01-11 17:50:00 | 50 | 300.0 | 19.890000 | 46.026667 |
| 6 | 2016-01-11 18:00:00 | 60 | 360.0 | 19.890000 | 45.766667 |
| 7 | 2016-01-11 18:10:00 | 60 | 360.0 | 19.856667 | 45.560000 |
Training/test rows: 13814 5921
2. Fit training-only scaling and compare a baseline
The random variables rv1/rv2 and other energy measurements are excluded. Ridge uses only training means/SDs. The mean baseline also uses the training target mean. Report held-out metrics even if the simple model performs poorly.
model=ridge_fit_predict(frame.loc[train,features].to_numpy(),frame.loc[train,"Appliances"].to_numpy(),frame.loc[test,features].to_numpy(),penalty=10)
truth=frame.loc[test,"Appliances"].to_numpy(float);prediction=model["predictions"]
baseline=np.full(len(truth),frame.loc[train,"Appliances"].mean())
metrics=pd.DataFrame({"ridge":regression_metrics(truth,prediction),"training-mean":regression_metrics(truth,baseline)}).T;display(metrics)
fig,ax=plt.subplots(figsize=(7,5));ax.scatter(truth,prediction,s=5,alpha=.25)
limit=[min(truth.min(),prediction.min()),max(truth.max(),prediction.max())];ax.plot(limit,limit,"k--")
ax.set(xlabel="Observed Wh/interval",ylabel="Predicted Wh/interval",title="Chronological held-out conditional predictions");plt.tight_layout();plt.show()
| mae | rmse | r_squared | |
|---|---|---|---|
| ridge | 49.573429 | 87.729910 | 0.082222 |
| training-mean | 53.398043 | 91.646165 | -0.001546 |
3. Inspect time-dependent errors
Plot a bounded first-week test interval; errors and target values share Wh/interval units. A limited feature set may miss occupancy and appliance events. The residual trace is not an independent-series confidence calculation.
timestamp=frame.loc[test,"timestamp"].to_numpy();residual=prediction-truth;count=min(7*144,len(truth))
fig,ax=plt.subplots(figsize=(10,4));ax.plot(iso_datetimes(timestamp[:count]),residual[:count],lw=.8)
ax.axhline(0,color="black",linestyle="--");ax.set(xlabel="Source-local test timestamp",ylabel="Prediction − observation (Wh/interval)",title="First test-week conditional errors");plt.tight_layout();plt.show()
4. Compare average errors by clock hour
Group test absolute errors by source-local hour on the same dates for model and baseline. Error patterns can motivate a new prospective design, but do not retrospectively tune this held-out result and still call it untouched.
hours=np.array([value.hour for value in iso_datetimes(frame.loc[test,"timestamp"])])
hourly=pd.DataFrame({"hour":hours,"ridge":abs(residual),"training-mean":abs(baseline-truth)}).groupby("hour").mean()
fig,ax=plt.subplots(figsize=(9,4));hourly.plot(ax=ax,marker="o")
ax.set(xlabel="Source-local clock hour",ylabel="Held-out MAE (Wh/interval)",title="Common-date error stratification");plt.tight_layout();plt.show()
5. Reconcile predictors and export
Check training-only statistics and direct RMSE. Do not hide negative predictions by clipping. Export the time split, original target and both predictions, keeping the single-building observational limitation.
assert np.allclose(model["mean"],frame.loc[train,features].mean())
assert np.isclose(metrics.loc["ridge","rmse"],np.sqrt(np.mean(residual**2)))
assert not {"Appliances","lights","rv1","rv2"}&set(features)
OUTPUT_DIR.mkdir(parents=True,exist_ok=True)
pd.DataFrame({"timestamp":timestamp,"observed_Wh":truth,"ridge_Wh":prediction,"training_mean_Wh":baseline}).to_csv(OUTPUT_DIR/"held-out-energy.csv",index=False)
metrics.to_csv(OUTPUT_DIR/"metrics-Wh-per-interval.csv");hourly.to_csv(OUTPUT_DIR/"hourly-test-MAE.csv")
print("Prediction-boundary checks passed; negative model predictions:",int(np.sum(prediction<0)))
Prediction-boundary checks passed; negative model predictions: 0
Exercises and reference explanation
- Why prefer chronological over random-row holdout here?
- Why distinguish conditional regression from forecasting?
Reference explanation. Nearby seasonal observations can leak information across random splits. Contemporaneous environmental values are known for reconstruction, not necessarily for future predictions.
Keep a copy before changing parameters. Compare old and new figures and record which inputs, calculations and conclusions changed.
Optional AI coding exercises
Prompt 1
Using NumPy/Pandas/Matplotlib, add a training-only clock-hour mean baseline and evaluate on the same test timestamps.
Prompt 2
With the same packages, report error separately for high and low observed energy while keeping the original fitted model unchanged.
Human acceptance. No test values enter scaling or fitting; all methods share dates and Wh/interval units.
Review the edited source, then manually run affected cells and subsequent checks. Ask the assistant to explain the purpose, packages and data provenance of its changes, and reconcile that explanation with actual output.
Optional online extension
RUN_ONLINE_UPDATE=False
if RUN_ONLINE_UPDATE:
import requests
try:
response=requests.get('https://archive.ics.uci.edu/dataset/374/appliances+energy+prediction',timeout=20)
response.raise_for_status()
print("Source:",response.url)
print(response.text[:700])
except requests.RequestException as error:
print("Online extension unavailable:",type(error).__name__,str(error)[:160])
else:
print("Online extension skipped; embedded core data unchanged")
Online extension skipped; embedded core data unchanged
Sources, snapshots and next steps
- UCI energy — CC BY 4.0; credit UCI and original dataset authors; retrieved 2026-10-09. SHA-256
2fccf354445d886e7917620b0195db1f3e3e34d5a067a93b844694a4c561255a. Extract energydata_complete.csv from original download; preserve all rows and declared units; no imputation. Candanedo, L. (2017). Appliances Energy Prediction. UCI Machine Learning Repository. DOI: 10.24432/C5VC8G.
Next steps. Transfer these methods to your own public or authorized data while retaining input checks, recorded parameters and result validation. Document the new experimental design and method limits; this example does not validate a new experiment. Full data licenses and generation instructions accompany the cookbook.