您的位置:首页 > 其它

机器学习: 性能度量

2017-10-28 19:56 363 查看

介绍

在机器学习中,性能度量主要体现在三个指标: 查准率(P)、查全率(R)、F1 。

代码模板

# coding=utf-8

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

... ...

def performance(tp, fn, fp, tn):
# 查准率
P = tp / float(tp + fp)
# 查全率
R = tp / float(tp + fn)
# F1
F1 = (2 * P * R) / (P + R)
# print P, R, F1
return P, R, F1

... ...

P, R, F1 = performance(white_pixels, red_pixels, green_pixels, black_pixels)
print '查准率 P = {:>.3f},  查全率 R = {:>.3f},   F1 = {:>.3f}'.format(P, R, F1).encode('gb18030')

... ...


运用示例

# coding=utf-8

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

diff_path = './data/diff.jpg'

black_pixels, white_pixels, green_pixels, red_pixels = 29158, 530899, 75994, 3949
sum_pixels = 640000

def performance(tp, fn, fp, tn):
# 查准率
P = tp / float(tp + fp)
# 查全率
R = tp / float(tp + fn)
# F1
F1 = (2 * P * R) / (P + R)
# print P, R, F1
return P, R, F1

def occupancy_cal(a, b):
result = (a + b) / float(sum_pixels)
return result

def loss_cal(a, b):
result = (a - b) / float(a)
return result

print '以 二值化结果 作为 预测值, 标注框结果 作为 ground_truth 时:'.encode('gb18030')
truth, predict = occupancy_cal(w
4000
hite_pixels, red_pixels), occupancy_cal(white_pixels, green_pixels)
loss = loss_cal(truth, predict)
print '实际 横截面区域 占图像 {:>.3f}, 预测值为 {:>.3f}, 损失比例为 {:>.3f}'.format(truth, predict, loss).encode('gb18030')
P, R, F1 = performance(white_pixels, red_pixels, green_pixels, black_pixels)
print '查准率 P = {:>.3f},  查全率 R = {:>.3f},   F1 = {:>.3f}'.format(P, R, F1).encode('gb18030')
print
print '以 标注框结果 作为 预测值, 二值化结果 作为 ground_truth 时:'.encode('gb18030')
truth, predict = occupancy_cal(white_pixels, green_pixels), occupancy_cal(white_pixels, red_pixels)
loss = loss_cal(truth, predict)
print '实际 横截面区域 占图像 {:>.3f}, 预测值为 {:>.3f}, 损失比例为 {:>.3f}'.format(truth, predict, loss).encode('gb18030')
P, R, F1 = performance(white_pixels, green_pixels, red_pixels, black_pixels)
print '查准率 P = {:>.3f},  查全率 R = {:>.3f},   F1 = {:>.3f}'.format(P, R, F1).encode('gb18030')


打印结果:

以 二值化结果 作为 预测值, 标注框结果 作为 ground_truth 时:
实际 横截面区域 占图像 0.836, 预测值为 0.948, 损失比例为 -0.135
查准率 P = 0.875,  查全率 R = 0.993,   F1 = 0.930

以 标注框结果 作为 预测值, 二值化结果 作为 ground_truth 时:
实际 横截面区域 占图像 0.948, 预测值为 0.836, 损失比例为 0.119
查准率 P = 0.993,  查全率 R = 0.875,   F1 = 0.930
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: