Architecting low-code AI solutions · Topic 1 of 4

BigQuery ML Models, Features and Predictions

Choose BigQuery ML model types for classification, regression, forecasting and clustering, engineer and select features in SQL, and generate predictions.

  • 5 min
In a nutshell
  • Labels decide the family: a category is classification, a number is regression, no label is clustering, time ordered values are forecasting.
  • TRANSFORM stores preprocessing with the model and prevents training-serving skew.
  • ML.FEATURE_IMPORTANCE, ML.GLOBAL_EXPLAIN and L1_REG help select features.

What it is

BigQuery ML lets you create, evaluate and use machine learning models with SQL, inside BigQuery. The data never leaves the warehouse, and analysts who know SQL can build models without Python or a separate training environment.

The exam tests whether you can map a business question to the right model type, prepare features in SQL, and pick the right prediction function.

Business questionModel typeCREATE MODELFeaturesTRANSFORM clauseEvaluateML.EVALUATEPredictML.PREDICT or ML.FORECAST

How it works

Matching the model type to the business problem

The model_type option in CREATE MODEL sets the algorithm. Start from what the business wants to know.

Business questionProblem typeExample model_type
Will this customer churn? Is this transaction fraud?ClassificationLOGISTIC_REG, BOOSTED_TREE_CLASSIFIER, RANDOM_FOREST_CLASSIFIER, DNN_CLASSIFIER
How much will this customer spend?RegressionLINEAR_REG, BOOSTED_TREE_REGRESSOR, DNN_REGRESSOR
How many units will each store sell next month?Forecasting (time series)ARIMA_PLUS, ARIMA_PLUS_XREG (with extra regressors)
Which natural customer segments exist?Clustering (no labels)KMEANS
Which products should we recommend?RecommendationMATRIX_FACTORIZATION
Which readings look abnormal?Anomaly detectionML.DETECT_ANOMALIES on KMEANS, AUTOENCODER or ARIMA_PLUS models

Classification and regression need a label column (input_label_cols). Clustering needs none. Forecasting needs a timestamp column and a value column, plus an ID column when you forecast many series at once.

Feature engineering in SQL

BigQuery ML already does some preprocessing for you: for many model types it standardizes numeric columns and one-hot encodes string columns. For anything more, use the TRANSFORM clause. Preprocessing written there is saved with the model and applied again automatically at prediction time, so training and serving cannot drift apart.

CREATE OR REPLACE MODEL `retail.churn_model`
TRANSFORM (
  ML.STANDARD_SCALER(tenure_months) OVER () AS tenure_scaled,
  ML.QUANTILE_BUCKETIZE(monthly_spend, 4) OVER () AS spend_bucket,
  ML.FEATURE_CROSS(STRUCT(plan_type, region)) AS plan_region,
  churned
)
OPTIONS (
  model_type = 'BOOSTED_TREE_CLASSIFIER',
  input_label_cols = ['churned'],
  enable_global_explain = TRUE
) AS
SELECT tenure_months, monthly_spend, plan_type, region, churned
FROM retail.customer_features;
FunctionWhat it does
ML.STANDARD_SCALER, ML.MIN_MAX_SCALERScale numeric features
ML.BUCKETIZE, ML.QUANTILE_BUCKETIZETurn a number into ranges
ML.ONE_HOT_ENCODER, ML.LABEL_ENCODEREncode categories
ML.FEATURE_CROSSCombine categories into one feature
ML.IMPUTERFill missing values

Feature selection

Fewer, better features train faster and generalize better. BigQuery ML gives several ways to find weak or harmful features:

ToolUse
ML.FEATURE_IMPORTANCEImportance scores for boosted tree and random forest models
ML.GLOBAL_EXPLAINOverall feature attributions (train with enable_global_explain = TRUE)
L1_REG optionPushes weights of unhelpful features in linear and logistic models toward zero
SQL checks (CORR, COUNTIF(x IS NULL))Spot duplicated, empty or leaky columns before training

Remove any feature that would not be known at prediction time, such as a "cancellation date" in a churn model. That is data leakage: evaluation looks excellent and production fails.

Evaluating and generating predictions

FunctionReturns
ML.EVALUATEMetrics on held-out data (precision, recall, ROC AUC, RMSE and so on)
ML.PREDICTPredictions for new rows, as a batch query
ML.FORECASTFuture values with prediction intervals for time series models
ML.EXPLAIN_PREDICTPredictions plus per-row feature attributions
ML.DETECT_ANOMALIESAn anomaly flag per row, with a probability or distance score depending on the model type
SELECT customer_id, predicted_churned, predicted_churned_probs
FROM ML.PREDICT(MODEL `retail.churn_model`,
  (SELECT * FROM retail.customers_today));

Batch scoring is usually a scheduled query that writes ML.PREDICT output to a table. For low-latency online predictions, register the model in Model Registry and deploy it to an endpoint, or export it with EXPORT MODEL.

A worked example

A retailer wants a 30-day sales forecast for each of 400 stores. The data is a daily sales table in BigQuery and the team knows SQL. One ARIMA_PLUS model with time_series_id_col trains a separate series per store in a single statement.

CREATE OR REPLACE MODEL `retail.store_forecast`
OPTIONS (
  model_type = 'ARIMA_PLUS',
  time_series_timestamp_col = 'sale_date',
  time_series_data_col = 'units',
  time_series_id_col = 'store_id'
) AS
SELECT sale_date, store_id, units FROM retail.daily_sales;

SELECT * FROM ML.FORECAST(MODEL `retail.store_forecast`,
  STRUCT(30 AS horizon, 0.9 AS confidence_level));
Daily sales tableARIMA_PLUSone series per storeML.FORECAST30 day horizonForecast tableDashboard

How the exam asks about this

  • "Analysts want to segment customers by behaviour; there is no label column." Train a KMEANS model in BigQuery ML.
  • "Predictions differ because the app scales features differently from training." Move the preprocessing into the TRANSFORM clause so ML.PREDICT applies it.
  • "Forecast demand for thousands of products from a BigQuery table with minimal effort." ARIMA_PLUS with time_series_id_col, then ML.FORECAST.

What to remember for the exam

  • Labels decide the family: a category is classification, a number is regression, no label is clustering, time ordered values are forecasting.
  • TRANSFORM stores preprocessing with the model and prevents training-serving skew.
  • ML.FEATURE_IMPORTANCE, ML.GLOBAL_EXPLAIN and L1_REG help select features.
  • ML.PREDICT is batch scoring in SQL; online serving needs an endpoint.
  • BigQuery ML keeps data in place: no export, no extra infrastructure.
Watch out

A random split on time-series data leaks the future into training. Forecasting models must be evaluated on a later period than they were trained on.

Common mistakes

Exporting data to train elsewhere. If the data is in BigQuery and SQL skills are what the team has, moving data to a notebook adds cost and work for no gain.

Using regression for a yes or no outcome. Churn, fraud and approval are classification problems, even when the label is stored as 0 and 1.

Repeating preprocessing in application code. Two copies of the logic drift apart; keep it in TRANSFORM.

Check yourself

  • Which BigQuery ML model type groups unlabeled customers into segments?

    KMEANS, a clustering model that needs no label column.

  • Why use the TRANSFORM clause instead of preprocessing in a view?

    TRANSFORM saves the preprocessing with the model, so ML.PREDICT applies exactly the same steps to new data and avoids training-serving skew.

  • How do you get a forecast for each of many stores in one model?

    Train ARIMA_PLUS with time_series_id_col set to the store column, then call ML.FORECAST with the horizon you need.

Topic 1 of 4 in Architecting low-code AI solutions

Test yourself on Architecting low-code AI solutions

The full Google Cloud Professional Machine Learning Engineer question bank has 749 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Google Cloud. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.