MATLAB画ROC曲线,及计算AUC值
根据决策值和真实标签画ROC曲线,同时计算AUC的值
步骤:
- 根据决策值和真实标签画ROC曲线,同时计算AUC的值:
- 计算算法的决策函数值deci
- 根据决策函数值deci对真实标签y进行降序排序,得到新的排序$roc_y$
- 根据$roc_y$分别对正负类样本进行累积分布$stack_x$,$stack_y$
- 根据$stack_x$,$stack_y$计算RUC的值
- \[AUC = \sum_{i=2}^{n}(stack_x(i)-stack_x(i-1))*stack_y(i) \]
- 分别以$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值的更多相关文章
- scikit-learn机器学习(二)逻辑回归进行二分类(垃圾邮件分类),二分类性能指标,画ROC曲线,计算acc,recall,presicion,f1
数据来自UCI机器学习仓库中的垃圾信息数据集 数据可从http://archive.ics.uci.edu/ml/datasets/sms+spam+collection下载 转成csv载入数据 im ...
- 使用Python画ROC曲线以及AUC值
from:http://kubicode.me/2016/09/19/Machine%20Learning/AUC-Calculation-by-Python/ AUC介绍 AUC(Area Unde ...
- ROC 曲线,以及AUC计算方式
ROC曲线: roc曲线:接收者操作特征(receiveroperating characteristic),roc曲线上每个点反映着对同一信号刺激的感受性. ROC曲线的横轴: 负正类率(false ...
- ROC曲线的计算
1.ROC曲线简介 在评价分类模型时,会用到ROC(receiver operating characteristic)曲线.ROC曲线可用来评价二元分类器( binary classifier)的优 ...
- PR曲线 ROC曲线的 计算及绘制
在linear model中,我们对各个特征线性组合,得到linear score,然后确定一个threshold,linear score < threshold 判为负类,linear sc ...
- 一个画ROC曲线的封装包
Draw_ROC_Curves This is a python file which is used for drawing ROC curves -f : assign file name -t ...
- 机器学习常见的几种评价指标:精确率(Precision)、召回率(Recall)、F值(F-measure)、ROC曲线、AUC、准确率(Accuracy)
原文链接:https://blog.csdn.net/weixin_42518879/article/details/83959319 主要内容:机器学习中常见的几种评价指标,它们各自的含义和计算(注 ...
- 【分类模型评判指标 二】ROC曲线与AUC面积
转自:https://blog.csdn.net/Orange_Spotty_Cat/article/details/80499031 略有改动,仅供个人学习使用 简介 ROC曲线与AUC面积均是用来 ...
- [机器学习]-分类问题常用评价指标、混淆矩阵及ROC曲线绘制方法
分类问题 分类问题是人工智能领域中最常见的一类问题之一,掌握合适的评价指标,对模型进行恰当的评价,是至关重要的. 同样地,分割问题是像素级别的分类,除了mAcc.mIoU之外,也可以采用分类问题的一些 ...
随机推荐
- MS16-016 提权EXP
测试环境 win7 32 位未打任何补丁 1.使用net user 指令 测试得到结果没有权限新建用户 2.查看系统用户 3.复制EXP到win7 下 使用命令打开 添加新用户再查看用户列表
- HIP-HOP 漫画家 Skottie Young
http://blog.sina.com.cn/s/blog_67e59b160100v9ib.html 以上是部分预览图! 下载地址: 29 MB= 127 张 yy语 ...
- CoreLocation
导入框架(Xcode5.0之后可以省略)
- Linux命令工具 top详解
Linux命令工具 top详解 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.top是一个动态显示过程,即可以通过用户按键来不 ...
- 2016.3.22考试(HNOI难度)
T1 盾盾的打字机 盾盾有一个非常有意思的打字机,现在盾哥要用这台打字机来打出一段文章. 由于有了上次的经验,盾盾预先准备好了一段模板A存在了内存中,并以此为基础来打出文章B.盾盾每次操作可以将内存中 ...
- Android ActionBar以及menu的代码设置样式
menu部分xml代码 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android= ...
- iOS - Swift Subscript 下标脚本
1.Subscript 下标脚本允许你通过在实例后面的方括号中传入一个或者多个的索引值来对实例进行访问和赋值.语法类似于实例方法和计算型属性的混合.与定义实例方法类似,定义下标脚本使用 subscri ...
- iOS - Notification 通知
1.Notification 通知中心实际上是在程序内部提供了消息广播的一种机制,它允许我们在低程度耦合的情况下,满足控制器与一个任意的对象进行通信的目的.每一个 iOS 程序(即每一个进程)都有一个 ...
- Getuserpassword
将[新注册的用户的用户名和密码]保存到服务端本地 /*将注册成功的用户名和密码保存到本地*/ /*定位*/ File f = new File("D:/lab_2/用户名和密码.qq&quo ...
- python paramiko模块SSH自动登录linux系统进行操作
1). Linux系统首先要开启SSH服务:service ssh status 如果没安装的话,则要:apt-get install openssh-server service ssh resta ...