matlab计算prc曲线auc面积,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
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,'-vs+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计算prc曲线auc面积,MATLAB画ROC曲线,及计算AUC值相关推荐
- python画出roc曲线 auc计算逻辑_Python画ROC曲线和AUC值计算
前言 ROC(Receiver Operating Characteristic)曲线和AUC常被用来评价一个二值分类器(binary classifier)的优劣.这篇文章将先简单的介绍ROC和AU ...
- Sklearn机器学习——ROC曲线、ROC曲线的绘制和AUC面积、运用ROC曲线找到最佳阈值
目录 1 ROC曲线 2 ROC曲线的绘制 2.1 Sklearn中的ROC曲线和AUC面积 2.2 利用ROC曲线找到最佳阈值 1 ROC曲线 上篇博客介绍了ROC曲线的概率和阈值还有SVM实现概率 ...
- 数据挖掘 python roc曲线_利用scikitlearn画ROC曲线实例
一个完整的数据挖掘模型,最后都要进行模型评估,对于二分类来说,AUC,ROC这两个指标用到最多,所以 利用sklearn里面相应的函数进行模块搭建. 具体实现的代码可以参照下面博友的代码,评估svm的 ...
- python画roc曲线_使用Python画ROC曲线以及AUC值
AUC介绍 AUC (Area Under Curve)是机器学习二分类模型中非常常用的评估指标,相比于 F1-Score 对项目的不平衡有更大的容忍性,目前常见的机器学习库中(比如 scikit-l ...
- matlab roc曲线,MATLAB画ROC曲线,及计算AUC值
标签: 根据决策值和真实标签画ROC曲线,同时计算AUC的值 function auc = roc_curve(deci,label_y) %%deci=wx+b, label_y, true lab ...
- 如何画ROC曲线和FROC曲线
画ROC曲线代码 具体去看https://www.jianshu.com/p/5df19746daf9.,里面的代码讲的详细 例子 # coding=UTF-8 from sklearn import ...
- 高斯白噪声中CW,LFM脉冲检测,定量画ROC曲线
高斯白噪声中CW脉冲检测,matlab定量画ROC曲线?LFM又如何 理论背景 高斯白噪声: 高斯白噪声,幅度分布服从高斯分布,功率谱密度服从均匀分布 白噪声在功率谱上(若以频率为横轴,信号幅度的平方 ...
- r roc函数_画ROC曲线的R包总结
原标题:画ROC曲线的R包总结 作者: Joseph Rickert 原文链接: https://rviews.rstudio.com/2019/03/01/some-r-packages-for-r ...
- 使用Python画出ROC曲线后,如何在ROC曲线代码中增加95%CI?
使用Python画出ROC曲线后,如何在ROC曲线代码中增加95%CI? 计算AUC之后,大部文献都会给出95%CI,如何在代码中增加这一功能呢?希望有大神给出代码!!!! 代码如下: import ...
最新文章
- opengl中的Floatbuffer和IntBuffer与java中数据的存储方式不同的解决方法,编辑一个自己的BufferUtil工具类
- DHTMLX 前端框架 建立你的一个应用程序教程(二)--设置布局
- Condition总结-await和signal的总结
- nodejs操作mongodb增删改查
- 搭建分布式环境:Dubbo+Zookeeper
- 操作系统页面置换算法(opt,lru,fifo,clock)实现
- 服务器不稳定 如何让百度重新收录网站,教你如何让百度重新收录首页
- FreeSwitch通过远程接口验证用户登录
- 实现用于意图识别的文本分类神经网络
- 我的网站被黑了,关键词被劫持,总结一下是怎么解决的。
- android 进程(复习)
- python入口文件详解_Python基础系列讲解——那些py文件中容易忽略的细节
- CCS12.0 安装并设置中文
- 观影计划:漫威电影宇宙「无限战争」系列
- windbg挂代理下载pdb
- 我开着车 你带着我
- 一文总结十大经典排序算法(思维导图 + 动图演示 + 代码实现 C/C++/Python + 致命吐槽)
- Redis 整合spring ,做mysql的缓存
- java 递归搜索文件_Java递归搜索指定文件夹下的匹配文件
- Vector CANoe 更改帧的调度表的顺序