L2R 二:常用评价指标之AUC
零零散散写了一些,主要是占个坑:
AUC作为一个常用的评价指标,无论是作为最后模型效果评价还是前期的特征选择,都发挥着不可替代的作用,下面我们详细介绍下这个指标。
1.定义
2.实现
# coding=utf-8
# auc值的大小可以理解为: 随机抽一个正样本和一个负样本,正样本预测值比负样本大的概率
# 根据这个定义,我们可以自己实现计算auc from sklearn.metrics import roc_curve, auc, roc_auc_score
import random
import time
import sys
import codecs
import numpy as np def timeit(func):
"""
装饰器,计算函数执行时间
""" def wrapper(*args, **kwargs):
time_start = time.time()
result = func(*args, **kwargs)
time_end = time.time()
exec_time = time_end - time_start
print("{function} exec time: {time}s".format(function=func.__name__, time=exec_time))
return result return wrapper def gen_label_pred(n_sample):
"""
随机生成n个样本的标签和预测值
"""
labels = [random.randint(0, 1) for _ in range(n_sample)]
preds = [random.random() for _ in range(n_sample)]
return labels, preds def load_label_pred(label_file): with codecs.open(label_file, "r", "utf-8") as f:
labels = np.array([float(l.strip().split("\t")[0]) for l in f.readlines()]) with codecs.open(label_file, "r", "utf-8") as f:
preds = np.array([float(l.strip().split("\t")[1]) for l in f.readlines()]) return labels, preds @timeit
def sklearn_auc_api(labels, preds):
"""
直接调用sklearn包中的结果
"""
auc = roc_auc_score(labels, preds)
return auc
#print("auc:"+str(auc)) @timeit
def naive_auc(labels, preds):
"""
最简单粗暴的方法
先排序,然后统计有多少正负样本对满足:正样本预测值>负样本预测值, 再除以总的正负样本对个数
复杂度 O(NlogN), N为样本数
"""
n_pos = sum(labels)
n_neg = len(labels) - n_pos
total_pair = n_pos * n_neg labels_preds = zip(labels, preds)
labels_preds = sorted(labels_preds, key=lambda x: x[1])
accumulated_neg = 0
satisfied_pair = 0
for i in range(len(labels_preds)):
if labels_preds[i][0] == 1:
satisfied_pair += accumulated_neg
else:
accumulated_neg += 1 return satisfied_pair / float(total_pair) @timeit
def approximate_auc(labels, preds, n_bins=100):
"""
近似方法,将预测值分桶(n_bins),对正负样本分别构建直方图,再统计满足条件的正负样本对
复杂度 O(N)
这种方法有什么缺点?怎么分桶? """
n_pos = sum(labels)
n_neg = len(labels) - n_pos
total_pair = n_pos * n_neg pos_histogram = [0 for _ in range(n_bins)]
neg_histogram = [0 for _ in range(n_bins)]
bin_width = 1.0 / n_bins
for i in range(len(labels)):
nth_bin = int(preds[i] / bin_width)
if labels[i] == 1:
pos_histogram[nth_bin] += 1
else:
neg_histogram[nth_bin] += 1 accumulated_neg = 0
satisfied_pair = 0
for i in range(n_bins):
satisfied_pair += (pos_histogram[i] * accumulated_neg + pos_histogram[i] * neg_histogram[i] * 0.5)
accumulated_neg += neg_histogram[i] return satisfied_pair / float(total_pair) if __name__ == "__main__":
#labels, preds = gen_label_pred(10000000)
labels, preds = load_label_pred(sys.argv[1])
naive_auc_rst = naive_auc(labels, preds)
#approximate_auc_rst = approximate_auc(labels, preds)
approximate_auc_rst = 0
sklearn_rst = sklearn_auc_api(labels, preds)
print("naive auc result:{},approximate auc result:{},sklearn auc result:{}".format(naive_auc_rst, approximate_auc_rst, sklearn_rst)) """
naive_auc exec time: 31.7306630611s
approximate_auc exec time: 2.32403683662s
naive auc result:0.500267265728,approximate auc result:0.50026516844
"""
3.应用
L2R 二:常用评价指标之AUC的更多相关文章
- [机器学习]-分类问题常用评价指标、混淆矩阵及ROC曲线绘制方法
分类问题 分类问题是人工智能领域中最常见的一类问题之一,掌握合适的评价指标,对模型进行恰当的评价,是至关重要的. 同样地,分割问题是像素级别的分类,除了mAcc.mIoU之外,也可以采用分类问题的一些 ...
- css入门二-常用样式
css入门二-常用样式总结 基本标签样式 背景色background-color 高度height; 宽度width; 边框对齐以及详细设定举例 width/*宽度*/: 80%; height/*高 ...
- Django笔记&教程 1-2 二 常用配置
Django 自学笔记兼学习教程第1章第2节--二 常用配置 点击查看教程总目录 新手建议简单浏览本文,不理解的建议跳过,不要强行理解. Django的设置涉及多个模块,需要了解Django的一些相关 ...
- 模型评价指标:AUC
参考链接:https://www.iteye.com/blog/lps-683-2387643 问题: AUC是什么 AUC能拿来干什么 AUC如何求解(深入理解AUC) AUC是什么 混淆矩阵(Co ...
- 分类器的评价指标-ROC&AUC
ROC 曲线:接收者操作特征曲线(receiver operating characteristic curve),是反映敏感性和特异性连续变量的综合指标,roc 曲线上每个点反映着对同一信号刺激的感 ...
- 初识PHP(二)常用函数
在此记录一些常用库函数和常用语法以便查阅 一.PHP手册 php手册中文地址 http://php.net/manual/zh 二.一些常用操作 2.1字符串操作 2.1.1 strpos — 查找字 ...
- Git(二):常用 Git 命令清单
转: http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html 我每天使用 Git ,但是很多命令记不住. 一般来说,日常使用只要记住下图 ...
- LINUX笔记之二常用命令(文件处理命令)
一.概述 1. “.”开头的文件是隐藏文件,大小写敏感是因为用C语言编写 2. DOS中 cd..可回到父目录 在LINUX中要用cd ..(用空格) 3. 4.LINUX命令有两种:仅root可执行 ...
- echart图表控件配置入门(二)常用图表数据动态绑定
上一节 <echart图表控件配置入门(一)>介绍了echarts图表控件的入门配置,使开发人员可以快速搭建出一个静态的图表.但是在实际开发过程这还是不够的,不可能所有的图表控件都是静态数 ...
随机推荐
- WinDbg常用命令系列---源代码操作相关命令
lsf, lsf- (Load or Unload Source File) lsf和lsf-命令加载或卸载源文件. lsf Filename lsf- Filename 参数: Filename指定 ...
- NVIDIA vGPU License服务器搭建详解
当配置有vGPU虚拟机发起License授权请求,授权服务器会根据License中所包含的GRID License版本,加载不同的vGPU驱动(普通驱动和专业Quodra卡驱动).目前vPC和vApp ...
- 洛谷 P1281 书的复制 题解
P1281 书的复制 题目背景 大多数人的错误原因:尽可能让前面的人少抄写,如果前几个人可以不写则不写,对应的人输出0 0. 不过,已经修改数据,保证每个人都有活可干. 题目描述 现在要把m本有顺序的 ...
- Flume 测试 Kafka 案例
Flume Kafka 测试案例,Flume 的配置. a1.sources = s1 a1.channels = c1 a1.sinks = k1 a1.sources.s1.type = netc ...
- mysql 选择所有同学名字
mysql> select * from test; +----+----------+-------+-----------+ | id | name | score | subject | ...
- 超级简单的tab点击
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Shell脚本实现对文件编辑
常见Linux文件的编辑命令 vi/vim,有时候我们想写一个脚本实现对文件编辑,这个时候,可能就不够用了,下面介绍一些办法 1.echo命令 Shell的echo命令常用于字符串的输出 例如: [r ...
- SDN第七次上机作业
1.补充并运行basic代码 任务是实现基础的交换机转发数据包功能 补充后代码如下: /* -*- P4_16 -*- */ #include <core.p4> #include < ...
- hdoj - 1864 最大报销额
Problem Description 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过1000元,每张发票上,单项物品 ...
- Maven中依赖的scope的依赖范围
在Maven中依赖的域有这几个:import.provided.runtime.compile.system.test 1compile 的范围 当依赖的scope为compile的时候,那么当前这个 ...