[ Web Proxy ]
URL:
Viewing: https://docs.cloud.google.com/bigquery/docs/reference/standard-sql/bigqueryml-syntax-confusion [Back]  [Original]

The ML.CONFUSION_MATRIX function  |  BigQuery  |  Google Cloud Documentation Skip to main content
Google Cloud Documentation [Google Cloud Documentation]
Send feedback Stay organized with collections Save and categorize content based on your preferences.

The ML.CONFUSION_MATRIX function

This document describes the ML.CONFUSION_MATRIX function, which you can use to 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:

Note: ML.CONFUSION_MATRIX requires input data with some models, and returns an error if it is absent. If this occurs, provide input data when using ML.CONFUSION_MATRIX with these models.

Output

The output columns of the ML.CONFUSION_MATRIX function depend on the model. The first output column is always expected_label. There are N additional columns, one for each class in the trained model. The names of the additional columns depend on the class labels used to train the model.

If the training class labels all conform to BigQuery column naming rules, the labels are used as the column names. Columns that don't conform to naming rules are altered to conform to the column naming rules and to be unique. For example, if the labels are 0 and 1, the output column names are _0 and _1.

The columns are ordered based on the class labels in ascending order. If the labels in the evaluation data match those in the training data, the True Positives are shown on the diagonal from top left to bottom right. The expected (or actual) labels are listed one per row, and the predicted labels are listed one per column.

The values in the expected_label column are the exact values and type passed into ML.CONFUSION_MATRIX in the label column of the evaluation data. This is true even if they don't exactly match the values or type used during training.

Limitations

ML.CONFUSION_MATRIX doesn't support imported TensorFlow models.

Examples

The following examples demonstrate the use of the ML.CONFUSION_MATRIX function.

ML.CONFUSION_MATRIX with a query statement

The following example returns the confusion matrix for a logistic regression model named mydataset.mymodel in your default project:

SELECT
  *
FROM
  ML.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 logistic regression model named mydataset.mymodel in your default project:

SELECT
  *
FROM
  ML.CONFUSION_MATRIX(MODEL `mydataset.mymodel`,
    (
    SELECT
      *
    FROM
      `mydataset.mytable`),
    STRUCT(0.6 AS threshold))

What's next

Send feedback

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-08-11 UTC.

Need to tell us more? [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-08-11 UTC."],[],[]]

Web Proxy Viewer  |  New URL  |  Original Page