tf.math.confusion_matrix
Stay organized with collections
Save and categorize content based on your preferences.
Computes the confusion matrix from predictions and labels.
tf.math.confusion_matrix(
labels,
predictions,
num_classes=None,
weights=None,
dtype=tf.dtypes.int32
,
name=None
)
Used in the notebooks
The matrix columns represent the prediction labels and the rows represent the
real labels. The confusion matrix is always a 2-D array of shape [n, n]
,
where n
is the number of valid labels for a given classification task. Both
prediction and labels must be 1-D arrays of the same shape in order for this
function to work.
If num_classes
is None
, then num_classes
will be set to one plus the
maximum value in either predictions or labels. Class labels are expected to
start at 0. For example, if num_classes
is 3, then the possible labels
would be [0, 1, 2]
.
If weights
is not None
, then each prediction contributes its
corresponding weight to the total value of the confusion matrix cell.
For example:
tf.math.confusion_matrix([1, 2, 4], [2, 2, 4]) ==>
[[0 0 0 0 0]
[0 0 1 0 0]
[0 0 1 0 0]
[0 0 0 0 0]
[0 0 0 0 1]]
Note that the possible labels are assumed to be [0, 1, 2, 3, 4]
,
resulting in a 5x5 confusion matrix.
Args |
labels
|
1-D Tensor of real labels for the classification task.
|
predictions
|
1-D Tensor of predictions for a given classification.
|
num_classes
|
The possible number of labels the classification task can
have. If this value is not provided, it will be calculated
using both predictions and labels array.
|
weights
|
An optional Tensor whose shape matches predictions .
|
dtype
|
Data type of the confusion matrix.
|
name
|
Scope name.
|
Returns |
A Tensor of type dtype with shape [n, n] representing the confusion
matrix, where n is the number of possible labels in the classification
task.
|
Raises |
ValueError
|
If both predictions and labels are not 1-D vectors and have
mismatched shapes, or if weights is not None and its shape doesn't
match predictions .
|
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. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.math.confusion_matrix\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/confusion_matrix.py#L92-L196) |\n\nComputes the confusion matrix from predictions and labels. \n\n tf.math.confusion_matrix(\n labels,\n predictions,\n num_classes=None,\n weights=None,\n dtype=../../tf/dtypes#int32,\n name=None\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Video classification with a 3D convolutional neural network](https://www.tensorflow.org/tutorials/video/video_classification) - [Simple audio recognition: Recognizing keywords](https://www.tensorflow.org/tutorials/audio/simple_audio) - [Transfer learning for video classification with MoViNet](https://www.tensorflow.org/tutorials/video/transfer_learning_with_movinet) - [Fine tuning models for plant disease detection](https://www.tensorflow.org/hub/tutorials/cropnet_on_device) - [How to solve a problem on Kaggle with TF-Hub](https://www.tensorflow.org/hub/tutorials/text_classification_with_tf_hub_on_kaggle) |\n\nThe matrix columns represent the prediction labels and the rows represent the\nreal labels. The confusion matrix is always a 2-D array of shape `[n, n]`,\nwhere `n` is the number of valid labels for a given classification task. Both\nprediction and labels must be 1-D arrays of the same shape in order for this\nfunction to work.\n\nIf `num_classes` is `None`, then `num_classes` will be set to one plus the\nmaximum value in either predictions or labels. Class labels are expected to\nstart at 0. For example, if `num_classes` is 3, then the possible labels\nwould be `[0, 1, 2]`.\n\nIf `weights` is not `None`, then each prediction contributes its\ncorresponding weight to the total value of the confusion matrix cell.\n\n#### For example:\n\n tf.math.confusion_matrix([1, 2, 4], [2, 2, 4]) ==\u003e\n [[0 0 0 0 0]\n [0 0 1 0 0]\n [0 0 1 0 0]\n [0 0 0 0 0]\n [0 0 0 0 1]]\n\nNote that the possible labels are assumed to be `[0, 1, 2, 3, 4]`,\nresulting in a 5x5 confusion matrix.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `labels` | 1-D `Tensor` of real labels for the classification task. |\n| `predictions` | 1-D `Tensor` of predictions for a given classification. |\n| `num_classes` | The possible number of labels the classification task can have. If this value is not provided, it will be calculated using both predictions and labels array. |\n| `weights` | An optional `Tensor` whose shape matches `predictions`. |\n| `dtype` | Data type of the confusion matrix. |\n| `name` | Scope name. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor` of type `dtype` with shape `[n, n]` representing the confusion matrix, where `n` is the number of possible labels in the classification task. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|---------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `ValueError` | If both predictions and labels are not 1-D vectors and have mismatched shapes, or if `weights` is not `None` and its shape doesn't match `predictions`. |\n\n\u003cbr /\u003e"]]