Let’s Get Certified
Module 06 / 12 · Data analysis and presentation

Train, evaluate and use a model without leaking the outcome

A utility wants tomorrow’s load forecast and a summary of incoming customer complaints.

Train, evaluate and use a model without leaking the outcome

A utility wants tomorrow’s load forecast and a summary of incoming customer complaints. One task has a numerical label; the other requires generation. Choose different baselines and evaluation criteria for those two outputs.

The instructor describes applying a remote model to a prompt column in BigQuery. Earlier examples set aside an evaluation split before using a trained model for predictions.

Work through the decision

A training table includes final invoice adjustments recorded after the day being forecast. Why might the backtest look extraordinary?

Check your reasoning

The adjustments reveal information that could not be known at prediction time. Rebuild point-in-time features, split by time for this forecasting task and compare with a simple baseline on the untouched period.

Associate Data Practitioner recording · 57:12

Decisions and tradeoffs

BigQuery ML regression model
Numerical targets in a warehouse can be modeled with a supported regression algorithm in SQL. Fit only on information available at prediction time.
ML.EVALUATE on held-out labels
Evaluate a trained model against unseen labeled examples and a simple baseline with a metric suited to the target.
ML.PREDICT
Use the trained model with a compatible set of feature columns to generate predictions for new rows.
Remote model with an authorized connection
A supported remote model can use a managed model endpoint from BigQuery with the appropriate connection identity and invocation permissions.
Model Registry version tracking
Register and identify model versions and their evaluation artifacts to support discovery, controlled release and rollback.

Repeat the train–evaluate–predict workflow in BigQuery ML

This SQL example uses a writable scratch dataset named study in your current Google Cloud project. Model creation and queries can incur charges. Use an unused model name and run the statements in order.

CREATE MODEL study.meter_demo
OPTIONS (model_type='LINEAR_REG', input_label_cols=['units'],
         data_split_method='NO_SPLIT') AS
SELECT x, 2 * x + 1 AS units
FROM UNNEST(GENERATE_ARRAY(1, 200)) AS x;
SELECT mean_absolute_error
FROM ML.EVALUATE(MODEL study.meter_demo,
  (SELECT x, 2 * x + 1 AS units FROM UNNEST([201, 202, 203]) AS x));

SELECT x, predicted_units
FROM ML.PREDICT(MODEL study.meter_demo,
  (SELECT 204 AS x));

The held-out labels are 403, 405 and 407. The continuation of the generating rule at 204 is 409. Inspect the actual error, compare with the training-mean baseline of 202, and keep the held-out rows out of training. The offline exercise below uses a smaller fixture with a noisy held-out label.

Practice each guide skill

Work through these exercises in the order of the published guide. The checklist numbers are local references.

  1. Choose BigQuery ML for suitable SQL-native warehouse data or evaluate managed AutoML when it fits the data modality, task and operational constraints.
  2. Create a supported remote connection with a least-privileged identity; test a small approved prompt table before invoking a hosted model at scale.
  3. Define the label, feature availability, chronological split for forecasting, baseline and decision metric before training.
  4. Use CREATE MODEL on training rows and ML.EVALUATE on distinct labeled data. Compare the metric’s units with a reference forecast.
  5. Call ML.PREDICT with features available at prediction time. Verify returned row counts, keys and predicted values.
  6. Track versions, intended use and evaluation results in Model Registry. Identify which version should be retained for rollback.

Official objectives for this lesson

Train a baseline and score held-out meter readings

Fit a one-feature least-squares model on three historical pairs. Predict the next two readings and compare mean absolute error with a baseline that always predicts the training mean. This exercise runs offline. Run with Python 3 and its standard library.

train = [(1, 3), (2, 5), (3, 7)]
holdout = [(4, 10), (5, 11)]
x_mean = sum(x for x, _ in train) / len(train)
y_mean = sum(y for _, y in train) / len(train)
slope = sum((x-x_mean)*(y-y_mean) for x, y in train) / sum((x-x_mean)**2 for x, _ in train)
intercept = y_mean - slope*x_mean
predictions = [intercept + slope*x for x, _ in holdout]
mae = sum(abs(y-p) for (_, y), p in zip(holdout, predictions)) / len(holdout)
baseline = sum(abs(y-y_mean) for _, y in holdout) / len(holdout)
print("predictions:", predictions)
print("model MAE:", mae, "baseline MAE:", baseline)
Check the result
predictions: [9.0, 11.0]
model MAE: 0.5 baseline MAE: 5.5

Holdout labels never enter the fit. MAE has the target’s unit, so inspect whether the reduction matters to the forecasting team. The linked BigQuery ML tutorial runs the corresponding train, evaluate and predict workflow on Google Cloud.

Related lesson · Keep bad records from quietly reaching consumers

Knowledge check

Train, evaluate and use a model without leaking the outcome quiz

Answer 3 questions. Check each answer for an explanation.

Worked example · Train, evaluate and use a model without leaking the outcome