The ML.CONFUSION_MATRIX function
This document describes theML.CONFUSION_MATRIX function, which you can useto return a confusion matrix for the input classification model and input data.
Syntax
ML.CONFUSION_MATRIX( MODEL `PROJECT_ID.DATASET.MODEL_NAME`, [, { TABLE `PROJECT_ID.DATASET.TABLE` | (QUERY_STATEMENT) }] STRUCT( [THRESHOLD AS threshold] [,TRIAL_ID AS trial_id]))Arguments
ML.CONFUSION_MATRIX takes the following arguments:
PROJECT_ID: the project that contains theresource.DATASET: the dataset that contains theresource.MODEL: the name of the model.TABLE: the name of the input table that containsthe evaluation data.If
TABLEis specified, the input column names in the table must match thecolumn names in the model, and their types should be compatible according toBigQueryimplicit coercion rules.The input must have a column that matches thelabel column name provided during training. This value is provided using theinput_label_colsoption. Ifinput_label_colsis unspecified, the columnnamedlabelin the training data is used.If you don't specify either
TABLEorQUERY_STATEMENT,ML.CONFUSION_MATRIXcomputes the confusion matrix results as follows:- If the data is split during training, the split evaluation data is used tocompute the confusion matrix results.
- If the data is not split during training, the entire training input isused to compute the confusion matrix results.
QUERY_STATEMENT: a GoogleSQL query that isused to generate the evaluation data. For the supported SQL syntax of theQUERY_STATEMENTclause in GoogleSQL, seeQuery syntax.If
QUERY_STATEMENTis specified, the input column names from the querymust match the column names in the model, and their types should becompatible according to BigQueryimplicit coercion rules.The input must have a column that matches the label column name providedduring training. This value is provided using theinput_label_colsoption.Ifinput_label_colsis unspecified, the column namedlabelin thetraining data is used. The extra columns are ignored.If you used the
TRANSFORMclausein theCREATE MODELstatement that created the model, then only the inputcolumns present in theTRANSFORMclause must appear inQUERY_STATEMENT.If you don't specify either
TABLEorQUERY_STATEMENT,ML.CONFUSION_MATRIXcomputes the confusion matrix results as follows:- If the data is split during training, the split evaluation data is used tocompute the confusion matrix results.
- If the data is not split during training, the entire training input isused to compute the confusion matrix results.
THRESHOLD: aFLOAT64value that specifies a customthreshold for the binary-class classification model to use for evaluation. Thedefault value is0.5.A
0value for precision or recall means that the selected thresholdproduced no true positive labels. ANaNvalue for precision means that theselected threshold produced no positive labels, neither true positives norfalse positives.If both
TABLEandQUERY_STATEMENTare unspecified, you can't use athreshold.You can't use
THRESHOLDwith multiclass classification models.TRIAL_ID: anINT64value that identifies thehyperparameter tuning trial that you want the function to evaluate. Thefunction uses the optimal trial by default. Only specify this argument if youran hyperparameter tuning when creating the model.
ML.CONFUSION_MATRIX requires input data with some models, andreturns an error if it is absent. If this occurs, provide input data when usingML.CONFUSION_MATRIX with these models.Output
The output columns of theML.CONFUSION_MATRIX function depend on the model.The first output column is alwaysexpected_label. There areN additionalcolumns, one for each class in the trained model. The names of the additionalcolumns depend on the class labels used to train the model.
If the training class labels all conform to BigQuerycolumn naming rules, the labels are usedas the column names. Columns that don't conform to naming rules are altered toconform to the column naming rules and to be unique. For example, if the labelsare0 and1, the output column names are_0 and_1.
The columns are ordered based on the class labels in ascending order. If thelabels in the evaluation data match those in the training data, theTrue Positivesare shown on the diagonal from top left to bottom right. The expected (oractual) labels are listed one per row, and the predicted labels are listed oneper column.
The values in theexpected_label column are the exact values and type passedintoML.CONFUSION_MATRIX in the label column of the evaluation data. This istrue even if they don't exactly match the values or type used during training.
Limitations
ML.CONFUSION_MATRIX doesn't supportimported TensorFlow models.
Examples
The following examples demonstrate the use of theML.CONFUSION_MATRIX function.
ML.CONFUSION_MATRIX with a query statement
The following example returns the confusion matrix for a logisticregression model namedmydataset.mymodel in your default project:
SELECT*FROMML.CONFUSION_MATRIX(MODEL`mydataset.mymodel`,(SELECT*FROM`mydataset.mytable`))
ML.CONFUSION_MATRIX with a custom threshold
The following example returns the confusion matrix for a logisticregression model namedmydataset.mymodel in your default project:
SELECT*FROMML.CONFUSION_MATRIX(MODEL`mydataset.mymodel`,(SELECT*FROM`mydataset.mytable`),STRUCT(0.6ASthreshold))
What's next
- For more information about model evaluation, seeBigQuery ML model evaluation overview.
- For more information about supported SQL statements and functions for MLmodels, seeEnd-to-end user journeys for ML models.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.