Skip to main content
Bird & Bear
Menu
← All articles

BigQuery Working Examples: ML.METRICS

bigquery ai analytics sql
BigQuery Working Examples: ML.METRICS

Last in our series of BigQuery AI function examples, alongside AI.KEY_DRIVERS, AI.FORECAST, AI.CAUSAL_EFFECT, and ML.CORRELATION. ML.METRICS scores any predicted-versus-actual pair of columns, classification or regression, without needing a stored BigQuery ML model behind it.

BigQuery’s ML.METRICS is also currently in Preview, same as ML.CORRELATION, worth checking the current support terms before relying on it for anything production-critical.

The dataset

Same synthetic e-commerce orders table as the rest of this series. Setup instructions are in our dataset setup guide; you’ll need it loaded as bq_ai_demo.ecommerce_orders before running the query below.

The query

This scores the simplest possible prediction you could make for an order’s revenue: just use the average revenue for that product category. It’s a genuine baseline, the kind of thing worth checking before reaching for a proper forecasting model, so you have a number to say whether the model actually earned its complexity.

WITH baseline AS (
  SELECT
    revenue,
    AVG(revenue) OVER (PARTITION BY product_category) AS predicted_revenue
  FROM `bq_ai_demo.ecommerce_orders`
)
SELECT *
FROM ML.METRICS(
  (SELECT * FROM baseline),
  predicted_col => 'predicted_revenue',
  actual_col => 'revenue',
  task_type => 'regression'
);

predicted_col and actual_col just need to be two numeric columns of the same type, actual and predicted. There’s no model object involved, which is the point: you can score any prediction, including a deliberately simple one like this, without setting anything up first. task_type set to regression returns error metrics; set it to classification instead and it returns precision, recall, accuracy, and F1 score for a predicted-versus-actual label pair.

What the result shows

The output is a single row: mean_absolute_error, mean_squared_error, mean_squared_log_error, median_absolute_error, r2_score, and explained_variance. For this baseline, expect a low r2_score, close to zero or even slightly negative. That’s expected. Predicting every order in a category as the category average ignores all the genuine order-to-order variation in the data, which is exactly what r2_score is measuring the absence of.

Using BigQuery's ML.METRICS

Why scoring a bad prediction on purpose is useful

The point of this example isn’t that the baseline is good. It’s that you now have a number for how not-good it is, before building anything more sophisticated. If someone on a team later builds a proper forecasting model, or uses AI.FORECAST from earlier in this series, this is the number that model needs to beat to justify the extra complexity. Without it, “the model seems better” is a feeling. With it, it’s a comparison.

That’s the pattern across this whole series. Each function answers a specific, checkable question: which segment moved a metric, where a series is headed, whether a change caused an effect, whether two metrics correlate, how good a prediction is. None of them replace judgement. All of them turn a question that used to take a person an afternoon of manual work into a query that comes back in seconds, provided the data underneath is clean enough to trust the answer.

All articles in this series

Newsletter

Occasional notes on data, tracking & AI-readiness

No spam, no growth-hacking. Just an email when there's something worth reading.