根据决策值和真实标签画ROC曲线,同时计算AUC的值

步骤:

  1. 根据决策值和真实标签画ROC曲线,同时计算AUC的值:
  2. 计算算法的决策函数值deci
  3. 根据决策函数值deci对真实标签y进行降序排序,得到新的排序$roc_y$
  4. 根据$roc_y$分别对正负类样本进行累积分布$stack_x$,$stack_y$
  5. 根据$stack_x$,$stack_y$计算RUC的值
  6. \[AUC = \sum_{i=2}^{n}(stack_x(i)-stack_x(i-1))*stack_y(i) \]
  7. 分别以$stack_x$,$stack_y$作为横坐标和纵坐标,画出RUC图
function auc = roc_curve(deci,label_y) %%deci=wx+b, label_y, true label
[val,ind] = sort(deci,'descend');
roc_y = label_y(ind);
stack_x = cumsum(roc_y == -1)/sum(roc_y == -1);
stack_y = cumsum(roc_y == 1)/sum(roc_y == 1);
auc = sum((stack_x(2:length(roc_y),1)-stack_x(1:length(roc_y)-1,1)).*stack_y(2:length(roc_y),1)) %Comment the above lines if using perfcurve of statistics toolbox
%[stack_x,stack_y,thre,auc]=perfcurve(label_y,deci,1);
plot(stack_x,stack_y);
xlabel('False Positive Rate');
ylabel('True Positive Rate');
title(['ROC curve of (AUC = ' num2str(auc) ' )']);
end

  

代码来自林智仁网站:https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/#roc_curve_for_binary_svm

function auc = plotroc(y,x,params)
%plotroc draws the recevier operating characteristic(ROC) curve.
%
%auc = plotroc(training_label, training_instance [, libsvm_options -v cv_fold])
% Use cross-validation on training data to get decision values and plot ROC curve.
%
%auc = plotroc(testing_label, testing_instance, model)
% Use the given model to predict testing data and obtain decision values
% for ROC
%
% Example:
%
% load('heart_scale.mat');
% plotroc(heart_scale_label, heart_scale_inst,'-v 5');
%
% [y,x] = libsvmread('heart_scale');
% model = svmtrain(y,x);
% plotroc(y,x,model);
rand('state',0); % reset random seed
if nargin < 2
help plotroc
return
elseif isempty(y) | isempty(x)
error('Input data is empty');
elseif sum(y == 1) + sum(y == -1) ~= length(y)
error('ROC is only applicable to binary classes with labels 1, -1'); % check the trainig_file is binary
elseif exist('params') && ~ischar(params)
model = params;
[predict_label,mse,deci] = svmpredict(y,x,model) ;% the procedure for predicting
auc = roc_curve(deci*model.Label(1),y);
else
if ~exist('params')
params = [];
end
[param,fold] = proc_argv(params); % specify each parameter
if fold <= 1
error('The number of folds must be greater than 1');
else
[deci,label_y] = get_cv_deci(y,x,param,fold); % get the value of decision and label after cross-calidation
auc = roc_curve(deci,label_y); % plot ROC curve
end
end
end function [resu,fold] = proc_argv(params)
resu=params;
fold=5;
if ~isempty(params) && ~isempty(regexp(params,'-v'))
[fold_val,fold_start,fold_end] = regexp(params,'-v\s+\d+','match','start','end');
if ~isempty(fold_val)
[temp1,fold] = strread([fold_val{:}],'%s %u');
resu([fold_start:fold_end]) = [];
else
error('Number of CV folds must be specified by "-v cv_fold"');
end
end
end function [deci,label_y] = get_cv_deci(prob_y,prob_x,param,nr_fold)
l=length(prob_y);
deci = ones(l,1);
label_y = ones(l,1);
rand_ind = randperm(l);
for i=1:nr_fold % Cross training : folding
test_ind=rand_ind([floor((i-1)*l/nr_fold)+1:floor(i*l/nr_fold)]');
train_ind = [1:l]';
train_ind(test_ind) = [];
model = svmtrain(prob_y(train_ind),prob_x(train_ind,:),param);
[predict_label,mse,subdeci] = svmpredict(prob_y(test_ind),prob_x(test_ind,:),model);
deci(test_ind) = subdeci.*model.Label(1);
label_y(test_ind) = prob_y(test_ind);
end
end function auc = roc_curve(deci,label_y) %%deci=wx+b, label_y, true label
[val,ind] = sort(deci,'descend');
roc_y = label_y(ind);
stack_x = cumsum(roc_y == -1)/sum(roc_y == -1);
stack_y = cumsum(roc_y == 1)/sum(roc_y == 1);
auc = sum((stack_x(2:length(roc_y),1)-stack_x(1:length(roc_y)-1,1)).*stack_y(2:length(roc_y),1)) %Comment the above lines if using perfcurve of statistics toolbox
%[stack_x,stack_y,thre,auc]=perfcurve(label_y,deci,1);
plot(stack_x,stack_y);
xlabel('False Positive Rate');
ylabel('True Positive Rate');
title(['ROC curve of (AUC = ' num2str(auc) ' )']);
end

调用:

[y,x] = libsvmread('heart_scale.txt');
model = svmtrain(y,x);
plotroc(y,x,model);

  

MATLAB画ROC曲线,及计算AUC值的更多相关文章

  1. scikit-learn机器学习(二)逻辑回归进行二分类(垃圾邮件分类),二分类性能指标,画ROC曲线,计算acc,recall,presicion,f1

    数据来自UCI机器学习仓库中的垃圾信息数据集 数据可从http://archive.ics.uci.edu/ml/datasets/sms+spam+collection下载 转成csv载入数据 im ...

  2. 使用Python画ROC曲线以及AUC值

    from:http://kubicode.me/2016/09/19/Machine%20Learning/AUC-Calculation-by-Python/ AUC介绍 AUC(Area Unde ...

  3. ROC 曲线,以及AUC计算方式

    ROC曲线: roc曲线:接收者操作特征(receiveroperating characteristic),roc曲线上每个点反映着对同一信号刺激的感受性. ROC曲线的横轴: 负正类率(false ...

  4. ROC曲线的计算

    1.ROC曲线简介 在评价分类模型时,会用到ROC(receiver operating characteristic)曲线.ROC曲线可用来评价二元分类器( binary classifier)的优 ...

  5. PR曲线 ROC曲线的 计算及绘制

    在linear model中,我们对各个特征线性组合,得到linear score,然后确定一个threshold,linear score < threshold 判为负类,linear sc ...

  6. 一个画ROC曲线的封装包

    Draw_ROC_Curves This is a python file which is used for drawing ROC curves -f : assign file name -t ...

  7. 机器学习常见的几种评价指标:精确率(Precision)、召回率(Recall)、F值(F-measure)、ROC曲线、AUC、准确率(Accuracy)

    原文链接:https://blog.csdn.net/weixin_42518879/article/details/83959319 主要内容:机器学习中常见的几种评价指标,它们各自的含义和计算(注 ...

  8. 【分类模型评判指标 二】ROC曲线与AUC面积

    转自:https://blog.csdn.net/Orange_Spotty_Cat/article/details/80499031 略有改动,仅供个人学习使用 简介 ROC曲线与AUC面积均是用来 ...

  9. [机器学习]-分类问题常用评价指标、混淆矩阵及ROC曲线绘制方法

    分类问题 分类问题是人工智能领域中最常见的一类问题之一,掌握合适的评价指标,对模型进行恰当的评价,是至关重要的. 同样地,分割问题是像素级别的分类,除了mAcc.mIoU之外,也可以采用分类问题的一些 ...

随机推荐

  1. 小米盒子连接老式电脑显示器(VGA接口)

    家里闲置一台老式显示器,只有VGA接口,无HDMI高清接口; 小米盒子上有三个输出接口: 一个HDMI高清接口:HDMI接口输出的有音频信号和视频信号,现在买的电视机一般都有HDMI高清接口: 一个A ...

  2. 关于Android 构建

    在简书上面有系列关于Android 的文章,还不错,部分同学可以在开发过程中阅读和学习:www.jianshu.com/collection/3fde3b545a35 关于Android 构建,看到这 ...

  3. php 引用其他action中的方法

    例如要调用IndexAction里的isonline(); 可用下列代码 import('@.Action.IndexAction'); $Index=new IndexAction(); $Inde ...

  4. 强制性签出被人没有签入的文件(在.net开发vs中)

    灵感,是天才的女神.她并不步履蹒跚地走过,而是在空中像乌鸦那么警觉地飞过的,她没有什么剽带给诗人抓握,她的头是一团烈火,她溜得快,像那些白里带红的鹤,教猎人见了无可奈何.——巴尔扎克(上海网站建设) ...

  5. JavaScript基于对象(面向对象)<一>类和对象

    javascript中一切皆对象,比如:Array,Date.....这些都是对象.javascript中没有class的定义,function既是定义函数,也可以是定义类.function Obj( ...

  6. 自学EF一些小笔记

    一直在用DHhelper做MVC,感觉好山寨,也不怎么好用.决定开始学EF. 废话不多说开始记笔记..... EF就是把数据库表,存储过程,视图实例化,通过继承DbContext的一个类来操作数据实例 ...

  7. linux中脚本的一些小知识的积累

    对于变量的问题: 对变量赋值,a="hello world",现在打印变量a的内容:echo $a. 对于${}的使用:如$aall,我们想要$a,这是,就可以${a}all了. ...

  8. XML HTML

    XML和HTML常用转义字符 XML和HTML中都有一些特殊的字符,这些字符在XML和HTML中是不能直接使用的,如果必须使用这些字符,应该使用其对应的转义字符. XML常用转义字符: 字符 转义字符 ...

  9. maven Connection refused: connect

    现象: 本地可以访问错误提示中的地址.但使用maven无法下载jar包. 环境: 浏览器上网需要使用代理 解决方法: 设置成正常代理可以.具体方法可以下载一个代理工具.只要IE配置成能直接访问http ...

  10. HBase的shell命令行界面按退格键(Backspace)无法删除问题

    在HBase的shell命令行界面输入错误项按"退格键"删除,却怎么也删除不了: 解决办法: 第一步,修改SecureCRT的设置参数: 第二步,按"Ctrl+退格键(B ...