📜  Python – tensorflow.math.confusion_matrix()

📅  最后修改于: 2022-05-13 01:54:49.701000             🧑  作者: Mango

Python – tensorflow.math.confusion_matrix()

TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。混淆矩阵()用于从预测和标签中找到混淆矩阵。

示例 1:

Python3
# importing the library
import tensorflow as tf
 
# Initializing the input tensor
labels = tf.constant([1,3,4],dtype = tf.int32)
predictions = tf.constant([1,2,3],dtype = tf.int32)
 
# Printing the input tensor
print('labels: ',labels)
print('Predictions: ',predictions)
 
# Evaluating confusion matrix
res = tf.math.confusion_matrix(labels,predictions)
 
# Printing the result
print('Confusion_matrix: ',res)


Python3
# importing the library
import tensorflow as tf
 
# Initializing the input tensor
labels = 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 tensor
print('labels: ',labels)
print('Predictions: ',predictions)
print('Weights: ',weights)
 
# Evaluating confusion matrix
res = tf.math.confusion_matrix(labels, predictions, weights=weights)
 
# Printing the result
print('Confusion_matrix: ',res)


输出:

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)

示例2:此示例为所有预测提供权重。

Python3

# importing the library
import tensorflow as tf
 
# Initializing the input tensor
labels = 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 tensor
print('labels: ',labels)
print('Predictions: ',predictions)
print('Weights: ',weights)
 
# Evaluating confusion matrix
res = tf.math.confusion_matrix(labels, predictions, weights=weights)
 
# Printing the result
print('Confusion_matrix: ',res)

输出:

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)