7.2 Creating, Training & Evaluating Models with BigQuery ML
Key Takeaways
BigQuery ML democratizes machine learning by executing model training, evaluation, and batch prediction directly inside BigQuery using standard SQL.
In-warehouse ML eliminates latency, governance risks, and compute overhead associated with exporting massive datasets to external Python or Spark environments.
BQML supports diverse model types including Linear Regression (numeric), Logistic Regression (classification), Boosted Trees (non-linear tabular data), K-Means (unsupervised clustering), and ARIMA_PLUS (time-series forecasting).
The standard BQML workflow progresses methodically through CREATE MODEL, ML.EVALUATE to inspect performance metrics, and ML.PREDICT to generate inference results.
BigQuery ML automatically performs feature preprocessing—such as one-hot encoding categorical variables and standardizing numerical distributions—during model creation.
Creating, Training & Evaluating Models with BigQuery ML
Core Focus: BigQuery ML (BQML) enables data analysts and practitioners to build, evaluate, and operationalize machine learning models directly inside Google BigQuery using SQL. By eliminating data movement, BQML accelerates time-to-value while preserving enterprise data governance and security.
In traditional enterprise machine learning workflows, training a model requires multiple disparate steps: exporting data from the warehouse to Cloud Storage, provisioning dedicated compute clusters (such as Vertex AI Workbench, Dataproc, or local Python environments), writing feature engineering scripts with libraries like scikit-learn or pandas, training the model, and setting up dedicated microservice endpoints for prediction. This workflow incurs substantial network egress overhead, creates governance blind spots, and demands specialized data science skills. BigQuery ML changes this paradigm by bringing machine learning algorithms directly to the data warehouse engine.
The BigQuery ML Value Proposition
BigQuery ML leverages BigQuery's massively parallel Dremel execution slots to train and score models at scale without data movement.
Core Advantages of BQML
- Zero Data Movement: Training and inference occur where the data already resides, eliminating network transfer latency and Cloud Storage egress costs.
- Democratized Tooling: SQL analysts can build robust predictive pipelines without learning Python, R, TensorFlow, or PyTorch.
- Built-in Governance and Lineage: Models are first-class BigQuery schema objects stored inside datasets, inheriting existing Google Cloud IAM policies, dataset access controls, and Cloud Audit Logging.
- Automated Feature Preprocessing: BQML automatically performs one-hot encoding on categorical string columns, handles missing value imputation, and normalizes numerical features during training without requiring manual preprocessing code.
The Core BigQuery ML Workflow
The BQML operational lifecycle consists of four primary stages:
[1. Prepare Data] ---> [2. CREATE MODEL] ---> [3. ML.EVALUATE] ---> [4. ML.PREDICT]
(Clean features via (Train in-warehouse (Inspect precision, (Generate batch
standard BigQuery SQL) using Dremel slots) recall, ROC AUC, RMSE) predictions)
- Data Preparation: Use standard SQL views or tables to aggregate, clean, and select input features.
- Model Training (
CREATE MODEL): Define the model architecture, hyperparameters, and label column using theOPTIONS()clause, providing the training dataset via anAS SELECTstatement. - Model Evaluation (
ML.EVALUATE): Evaluate model performance on a held-out test dataset or let BigQuery evaluate on an automated data split to inspect metrics like accuracy, ROC AUC, or Root Mean Squared Error (RMSE). - Batch Prediction (
ML.PREDICT): Pass new unlabelled feature records intoML.PREDICT()to generate predictions and probability scores in tabular format.
Supported Model Types for Data Practitioners
BigQuery ML supports a wide spectrum of machine learning tasks across supervised, unsupervised, and time-series domains:
1. Supervised Learning: Regression
LINEAR_REG(Linear Regression): Models a continuous numeric target variable (e.g., predicting customer lifetime value, home sale price, or transaction dollar volume).BOOSTED_TREE_REGRESSOR/RANDOM_FOREST_REGRESSOR: Uses ensembles of decision trees (XGBoost engine under the hood) to capture complex non-linear relationships among tabular features for numeric predictions.
2. Supervised Learning: Classification
LOGISTIC_REG(Logistic Regression): Predicts qualitative categorical outcomes. Supports binary classification (e.g., churn: TRUE/FALSE) and multiclass classification (e.g., support ticket priority: Low, Medium, High).BOOSTED_TREE_CLASSIFIER: Highly effective for tabular classification problems where tree-based feature interactions outperform linear boundaries.
3. Unsupervised Learning: Clustering
KMEANS(K-Means Clustering): Partitions unlabeled data into k distinct clusters based on Euclidean distance among features. Common applications include customer segmentation, behavioral cohorting, and anomaly detection (identifying records located far from cluster centroids).
4. Time-Series Forecasting: ARIMA_PLUS
ARIMA_PLUS: A fully managed, automated time-series forecasting model based on the statistical ARIMA algorithm. It automatically decomposes time-series data into trend and seasonal components, adjusts for holiday effects, detects and removes anomalies/spikes, and imputes missing time points.
SQL Syntax Breakdown: Creating a Supervised Classification Model
The following SQL example trains a binary logistic regression model to predict customer churn:
CREATE OR REPLACE MODEL `customer_analytics.churn_prediction_model`
OPTIONS(
-- Specify model architecture
model_type = 'logistic_reg',
-- Specify target label column
input_label_cols = ['has_churned'],
-- AUTO_SPLIT sizes the evaluation split from the row count (see below)
data_split_method = 'AUTO_SPLIT',
-- Automatically balance class weights if churned users are a small minority
auto_class_weights = TRUE
) AS
SELECT
tenure_months,
monthly_spend,
contract_type,
payment_method,
support_tickets_count,
has_churned
FROM `customer_analytics.customer_features`
WHERE account_status != 'SUSPENDED';
Key Options Parameters
model_type: Required string specifying the algorithm ('logistic_reg','linear_reg','kmeans','arima_plus','boosted_tree_classifier').input_label_cols: Array containing the name of the target column being predicted. If omitted, BQML assumes the label column is namedlabel.auto_class_weights: When set toTRUE, automatically balances class weights inversely proportional to class frequencies, addressing severe class imbalances (common in fraud detection or churn).data_split_method: Controls how BigQuery splits input data into training and evaluation sets ('AUTO_SPLIT','RANDOM','CUSTOM','SEQ', or'NO_SPLIT'). With the defaultAUTO_SPLIT, fewer than 500 rows are not split, 500 to 50,000 rows hold out 20% for evaluation, and larger inputs hold out 10,000 rows.
Model Evaluation: Understanding Performance Metrics
Once training completes, data practitioners must evaluate the model's predictive validity using ML.EVALUATE():
SELECT *
FROM ML.EVALUATE(
MODEL `customer_analytics.churn_prediction_model`,
(SELECT * FROM `customer_analytics.customer_test_features`)
);
If the second argument (the evaluation dataset) is omitted, BigQuery evaluates the model against the validation split automatically held out during training.
Classification Metrics
- Accuracy: The proportion of total correct predictions (positive and negative) over total instances. Can be deceptive in imbalanced datasets (e.g., 99% accuracy on fraud when only 1% of transactions are fraudulent).
- Precision: True Positives / (True Positives + False Positives). Measures exactness: of all customers predicted to churn, what percentage actually churned? High precision avoids wasting retention incentives on non-churners.
- Recall (Sensitivity): True Positives / (True Positives + False Negatives). Measures completeness: of all customers who actually churned, what percentage did the model catch? High recall is critical when missing a positive case is costly (e.g., fraud or terminal disease detection).
- ROC AUC (Area Under the Receiver Operating Characteristic Curve): Measures the model's ability to discriminate between positive and negative classes across all possible decision thresholds. A score of 0.5 represents random guessing, while 1.0 represents perfect classification.
- Log Loss: Penalizes confident incorrect predictions; lower values indicate better calibration.
Regression Metrics
- Mean Absolute Error (MAE): The average absolute difference between predicted and actual values in the original target units.
- Mean Squared Error (MSE): The average of the squared errors, penalizing larger deviations more severely.
- Root Mean Squared Error (RMSE): The square root of MSE, restoring the metric to the scale of the original target variable.
- R-Squared (R²): The proportion of variance in the dependent variable explained by the model features (values closer to 1.0 indicate strong explanatory power).
Generating Batch Predictions: ML.PREDICT
To apply a trained model to new records, invoke ML.PREDICT():
-- has_churned is a BOOL label, so each probs array holds labels TRUE and FALSE
SELECT
customer_id,
predicted_has_churned,
(SELECT prob FROM UNNEST(predicted_has_churned_probs) WHERE label = TRUE) AS churn_probability
FROM ML.PREDICT(
MODEL `customer_analytics.churn_prediction_model`,
(SELECT * FROM `customer_analytics.active_customers`)
)
ORDER BY churn_probability DESC;
Prediction Output Schema
predicted_<label_column_name>: Contains the predicted class label (for classification) or predicted numeric value (for regression).predicted_<label_column_name>_probs: For classification models, anARRAY<STRUCT<label STRING, prob FLOAT64>>containing the predicted probability for every possible class, enabling custom thresholding.
More Evaluation and Inspection Functions
ML.EVALUATE returns summary metrics, but the exam also expects you to recognize the companion functions:
| Function | What it returns | Typical use |
|---|---|---|
ML.CONFUSION_MATRIX | Counts of true and false positives and negatives at a threshold | Explain precision and recall trade-offs to stakeholders |
ML.ROC_CURVE | True positive rate and false positive rate across thresholds | Pick a decision threshold for a binary classifier |
ML.TRAINING_INFO | Loss per training iteration | Spot overfitting or a model that stopped improving |
ML.FEATURE_INFO | Statistics for each input column | Check data quality before trusting a model |
ML.GLOBAL_EXPLAIN / ML.EXPLAIN_PREDICT | Feature attributions overall or per prediction | Explain which inputs drive the prediction |
-- Confusion matrix at a 0.6 threshold on held-out data
SELECT *
FROM ML.CONFUSION_MATRIX(
MODEL `customer_analytics.churn_prediction_model`,
(SELECT * FROM `customer_analytics.customer_test_features`),
STRUCT(0.6 AS threshold)
);
Choosing the Right Metric
- Imbalanced classes (fraud, churn, rare defects): accuracy misleads; compare precision, recall, and ROC AUC.
- False negatives are costly (missing fraud or a sick patient): favor recall.
- False positives are costly (sending expensive offers or blocking good customers): favor precision.
- Regression in business units (dollars, days): MAE and RMSE speak the stakeholder's language; RMSE punishes large misses more.
Common Exam Traps & Practitioner Scenarios
Exam Tip: Whenever an exam scenario describes structured tabular data residing in BigQuery that requires standard classification, regression, or forecasting, BigQuery ML is almost always the correct answer. Avoid selecting external pipelines that export data to Cloud Storage and provision Compute Engine VMs or Dataproc clusters.
Trap 1: Forgetting input_label_cols for Supervised Models
- The Trap: An engineer attempts to train a
logistic_regmodel without specifyinginput_label_colsinOPTIONS(), while the target column in theSELECTquery is namedchurn_flag. - The Reality: By default, BQML looks for a column named exactly
label. If your target column is named anything else, omittinginput_label_cols = ['churn_flag']causes a compilation error.
Trap 2: Exporting BigQuery Data for Standard Tabular Models
- The Trap: An architecture question asks for the fastest, most cost-effective way to train a linear regression model on 500 GB of sales data in BigQuery, proposing an ETL pipeline to export data to Cloud Storage and run scikit-learn on a Compute Engine VM.
- The Reality: This introduces needless network egress, infrastructure provisioning, and maintenance. BigQuery ML trains linear regression directly in SQL in minutes without data movement.
Trap 3: Passing the Label Column into ML.PREDICT
- The Trap: Assuming that
ML.PREDICTrequires the true label column in its input table. - The Reality:
ML.PREDICTis used for inference on new, unlabelled records. Passing the target label column is not required (and will simply be ignored or overwritten by the predicted output column).
A data team needs to create a predictive model in BigQuery to classify whether an online transaction is fraudulent. The dataset is stored in finance.transactions where the target column is is_fraud (BOOLEAN). Because fraud represents less than 0.5% of all transactions, the model must account for class imbalance without manual resampling. Which SQL statement correctly configures this model?
CREATE OR REPLACE MODEL finance.fraud_model OPTIONS(model_type='linear_reg', input_label_cols=['is_fraud'], auto_class_weights=TRUE) AS SELECT * FROM finance.transactions
CREATE OR REPLACE MODEL finance.fraud_model OPTIONS(model_type='arima_plus', time_series_timestamp_col='tx_timestamp', input_label_cols=['is_fraud']) AS SELECT * FROM finance.transactions
CREATE OR REPLACE MODEL finance.fraud_model OPTIONS(model_type='logistic_reg', input_label_cols=['is_fraud'], auto_class_weights=TRUE) AS SELECT * FROM finance.transactions
CREATE OR REPLACE MODEL finance.fraud_model OPTIONS(model_type='kmeans', num_clusters=2, input_label_cols=['is_fraud']) AS SELECT * FROM finance.transactions
An analytics team trains a binary classification model in BigQuery ML to predict customer subscription cancellation. The business priority is to catch as many cancelling customers as possible, even if that means offering retention discounts to some customers who would not have cancelled. Which evaluation metric should the team optimize when reviewing ML.EVALUATE results?
Precision
R-squared
Recall
Mean Absolute Error (MAE)
A team trains a BigQuery ML linear regression model that predicts next month's spend in dollars. Finance leaders want an error measure that is reported in dollars and penalizes a few very large misses more heavily than many small ones. Which metric from ML.EVALUATE should the team highlight?
Precision at a 0.5 decision threshold
Area under the ROC curve (ROC AUC)
Log loss (cross-entropy)
Root mean squared error (RMSE)
Sections you finish are checked off in the contents.