Python - tensorflow.math.confusion_matrix() - GeeksforGeeks

08 Dec, 2021
TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks.  confusion_matrix() is used to find the confusion matrix from predictions and labels.
Syntax: tensorflow.math.confusion_matrix( labels, predictions, num_classes, weights, dtype,name) 
Parameters:
labels: It’s a 1-D Tensor which contains real labels for the classification task.
predictions: It’s also a 1-D Tensor of same shape as labels. It’s value represents the predicted class.
num_classes(optional): It is the possible number of labels/class classification task might have. If it’s not provided then num_classes will be one more than the maximum value in either predictions or labels.
weight(optional): It’s a Tensor of same shape as predictions whose values define the corresponding weight for each prediction.
dtype(optional): It defines the dtype of returned confusion matrix. Default if tensorflow.dtypes.int32.
name(optional): Defines the name for the operation. 
Returns:It returns a confusion matrix of shape [n,n] where n is the possible number of labels. 
Example 1:
Python3
# importing the libraryimport tensorflow as tf # Initializing the input tensorlabels = tf.constant([1,3,4],dtype = tf.int32)predictions = tf.constant([1,2,3],dtype = tf.int32) # Printing the input tensorprint('labels: ',labels)print('Predictions: ',predictions) # Evaluating confusion matrixres = tf.math.confusion_matrix(labels,predictions) # Printing the resultprint('Confusion_matrix: ',res)
Output:
labels:  tf.Tensor([1 3 4], shape=(3,), dtype=int32)
Predictions:  tf.Tensor([1 2 3], shape=(3,), dtype=int32)
Confusion_matrix:  tf.Tensor(
[[0 0 0 0 0]
 [0 1 0 0 0]
 [0 0 0 0 0]
 [0 0 1 0 0]
 [0 0 0 1 0]], shape=(5, 5), dtype=int32)
Example2: This example provide the weights to all predictions.
Python3
# importing the libraryimport tensorflow as tf # Initializing the input tensorlabels = tf.constant([1,3,4],dtype = tf.int32)predictions = tf.constant([1,2,4],dtype = tf.int32)weights = tf.constant([1,2,2], dtype = tf.int32) # Printing the input tensorprint('labels: ',labels)print('Predictions: ',predictions)print('Weights: ',weights) # Evaluating confusion matrixres = tf.math.confusion_matrix(labels, predictions, weights=weights) # Printing the resultprint('Confusion_matrix: ',res)
Output:
labels:  tf.Tensor([1 3 4], shape=(3,), dtype=int32)
Predictions:  tf.Tensor([1 2 4], shape=(3,), dtype=int32)
Weights:  tf.Tensor([1 2 2], shape=(3,), dtype=int32)
Confusion_matrix:  tf.Tensor(
[[0 0 0 0 0]
 [0 1 0 0 0]
 [0 0 0 0 0]
 [0 0 2 0 0]
 [0 0 0 0 2]], shape=(5, 5), dtype=int32)
surindertarika1234
sumitgumber28
sagartomar9927
nnr223442
Python Tensorflow-math-functions
Python-Tensorflow
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get unique values from a list
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python