Python - tensorflow.math.confusion_matrix ()

Опубликовано: 29 Марта, 2022

TensorFlow - это библиотека Python с открытым исходным кодом, разработанная Google для разработки моделей машинного обучения и нейронных сетей глубокого обучения. confusion_matrix () используется для нахождения матрицы путаницы из прогнозов и меток.

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 mroe 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. Defaut 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.
 

Пример 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("Predictins: ",predictions)
  
# Evaluating confusion matric
res = tf.math.confusion_matrix(labels,predictions)
  
# Printing the result
print("Confusion_matrix: ",res)

Выход:

 метки: tf.Tensor ([1 3 4], shape = (3,), dtype = int32)
Предсказания: 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 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("Predictins: ",predictions)
print("Weights: ",weights)
  
# Evaluating confusion matric
res = tf.math.confusion_matrix(labels, predictions, weights=weights)
  
# Printing the result
print("Confusion_matrix: ",res)

Выход:

 метки: tf.Tensor ([1 3 4], shape = (3,), dtype = int32)
Предсказания: tf.Tensor ([1 2 4], shape = (3,), dtype = int32)
Веса: 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)

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.