评估指标

==用于衡量模型的最终效果==

一、背景

在学习tensorflow的初级阶段,会常常搞不懂,metrics的具体意义和实际用途,接下来的文章一方面是对接自己的解答,也是一种学习路径的记录。

二、基础

混淆矩阵是理解众多评价指标的基础。下面是混淆矩阵的表格

image
  • TP:True Positive,预测为正例,实际也为正例。
  • FP:False Positive,预测为正例,实际却为负例。
  • TN:True Negative,预测为负例,实际也为负例。
  • FN:False Negative,预测为负例,实际却为正例。

统计正确预测的次数在总的数据集中的占比,==最常用==,tensorflow中是通过记录每次测试的批次的正确数量和总体数量确定的。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Time : 2020/4/4 9:18 下午
# Author : Dale Chen
# Description: 
# File : accuracy.py
# Copyright: (c) 2020 year, 4399 Network CO.ltd. All Rights Reserved.

import numpy as np

labels = np.array([[1, 1, 1, 0],
                   [1, 1, 1, 0],
                   [1, 1, 1, 0],
                   [1, 1, 1, 0]], dtype=np.float)

predictions = np.array([[1, 0, 0, 0],
                        [1, 1, 0, 0],
                        [1, 1, 1, 0],
                        [0, 1, 1, 1]], dtype=np.float)

N_CORRECT = 0
N_ITEM_SEEN = 0


def reset_running_variables():
    """reset variables"""
    global N_CORRECT, N_ITEM_SEEN
    N_CORRECT = 0
    N_ITEM_SEEN = 0


def update_running_variables(labs, preds):
    """update variables"""
    global N_CORRECT, N_ITEM_SEEN
    N_CORRECT += (labs == preds).sum()
    N_ITEM_SEEN += labs.size


def calculate_accuracy():
    """calculate accuracy"""
    global N_CORRECT, N_ITEM_SEEN
    return float(N_CORRECT) / N_ITEM_SEEN


reset_running_variables()
for i in range(len(labels)):
    update_running_variables(labels[i], predictions[I])
    accuracy = calculate_accuracy()
    print("accuracy:", accuracy)

三、内置评估指标算子

  • tf.metrics.accuracy()
  • tf.metrics.precision()
  • tf.metrics.recall()
  • tf.metrics.mean_iou()

1. accuracy [ˈækjərəsi] (准确率)

==所有预测正确的样本(不论正例还是负例,只看对错)占总体数量的比例==

注意一下输入的数据需要是true或者false, 实际上就是比对正确的数量占总体的百分比

import tensorflow as tf
import numpy as np

l = np.array([[0, 1, 0, 0],
              [0, 0, 0, 0]], dtype=np.float)
p = np.array([[0, 1, 0, 0],
              [0, 0, 0, 1]], dtype=np.float)

labels = tf.reshape(l, [2, 4])
predictions = tf.reshape(p, [2, 4])

op = tf.keras.metrics.BinaryAccuracy()
op.update_state(labels, predictions)

print("accuracy:", op.result().numpy())

//accuracy: 1.0

2.precision (精确度)

==精确率就是指 当前划分到正样本类别中,被正确分类的比例,即真正的正样本所占所有预测为正样本的比例。==

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Time : 2020/4/4 11:46 下午
# Author : Dale Chen
# Description: 
# File : precision.py
# Copyright: (c) 2020 year, 4399 Network CO.ltd. All Rights Reserved.

import numpy as np
import tensorflow as tf

l = np.array([[0, 1, 1, 0],
              [0, 0, 0, 0]])
p = np.array([[0, 1, 0, 0],
              [0, 0, 0, 0]])

labels = tf.reshape(l, [1, 8])
predictions = tf.reshape(p, [1, 8])


op = tf.keras.metrics.Precision()
op.update_state(labels, predictions)

print("precision:", (op.result()).numpy())


//precision: 1

我们有一个样本数量为50的数据集,其中正样本的数量为20。但是,在我们所有的预测结果中,只预测出了一个正样本,并且这个样本也确实是正样本,那么 TP=1,FP=0,Precision = TP/(TP+FP) = 1.0,那么我们的模型是不是就很好了呢?当然不是,我们还有19个正样本都没有预测成功. 这时候要使用回招率。

3.recall(回召率)

==召回率即指 当前被分到正样本类别中,真实的正样本占所有正样本的比例,即召回了多少正样本的比例。Recall = TP/(TP+FN)==

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Time : 2020/4/5 12:19 上午
# Author : Dale Chen
# Description: 
# File : recall.py
# Copyright: (c) 2020 year, 4399 Network CO.ltd. All Rights Reserved.

import numpy as np
import tensorflow as tf

l = np.array([[0, 1, 1, 0],
              [0, 0, 0, 0]])
p = np.array([[0, 1, 0, 0],
              [0, 0, 0, 0]])

labels = tf.reshape(l, [1, 8])
predictions = tf.reshape(p, [1, 8])

op = tf.keras.metrics.Recall()
op.update_state(labels, predictions)

print("recall:", (op.result()).numpy())

//recall: 0.5

3.mean_iou

用于处理分类问题的,需要输入种类的数量。标签的格式一定要是[0, 0 , 1, 0]的结构,只能有一种结果, 但是预测的值可以是有多种结果。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Time : 2020/4/4 10:10 下午
# Author : Dale Chen
# Description: 
# File : mean_iou.py
# Copyright: (c) 2020 year, 4399 Network CO.ltd. All Rights Reserved.


import numpy as np
import tensorflow as tf

l = np.array([[0, 1, 0, 0],
              [0, 0, 0, 1]])
p = np.array([[0, 1, 0, 0],
              [0, 0, 1, 0]])

labels = tf.reshape(l, [2, 4])
predictions = tf.reshape(p, [2, 4])

op = tf.keras.metrics.MeanIoU(num_classes=4)
op.update_state(labels, predictions)

print("iou_op", op.result().numpy())

你可能感兴趣的:(评估指标)