function [tpr,fpr,thresholds] = roc(targets,outputs)
%ROC Receiver operating characteristic.
%
% The receiver operating characteristic is a metric used to check
% the quality of classifiers. For each class of a classifier,
% threshold values across the interval [0,1] are applied to
% outputs. For each threshold, two values are calculated, the
% True Positive Ratio (the number of outputs greater or equal
% to the threshold, divided by the number of one targets),
% and the False Positive Ratio (the number of outputs greater
% then the threshold, divided by the number of zero targets).
%
% For single class problems, [TPR,FPR,TH] = <a href="matlab:doc roc">roc</a>(T,Y) takes
% a 1xQ target matrix T, where each element is either 1 or 0 indicating
% class membership or non-menbership respectively, and 1xQ outputs Y of
% values in the range [0,1].
%
% It returns three 1xQ vectors: the true-positive/positive ratios TPR,
% the false-positive/negative ratios FPR, and the thresholds associated
% with each of those values TH.
%
% For multi-class problems [TPR,FPR,TH] = <a href="matlab:doc roc">roc</a>(T,Y) takes
% an SxQ target matrix T, where each column contains a single 1 value,
% with all other elements 0. The row index of each 1 indicates which of S
% categories that vector represents. It also takes an SxQ output matrix Y,
% with values in the range [0,1]. The row indices of the largest elements in
% each column of Y indicate the most likely class.
%
% In the multi-class case, all three values returned are 1xS cell arrays,
% so that TPR{i}, FPR{i} and TH{i} are the ratios and thresholds for the
% ith class.
%
% <a href="matlab:doc roc">roc</a>(T,Y) can also take a boolean row vector T, and row vector Y, in
% which case two categories are represented by targets 1 and 0.
%
% Here a network is trained to recognize iris flowers the ROC is
% calculated and plotted.
%
% [x,t] = <a href="matlab:doc iris_dataset">iris_dataset</a>;
% net = <a href="matlab:doc patternnet">patternnet</a>(10);
% net = <a href="matlab:doc train">train</a>(net,x,t);
% y = net(x);
% [tpr,fpr,th] = <a href="matlab:doc roc">roc</a>(t,y)
% <a href="matlab:doc plotroc">plotroc</a>(t,y)
%
% See also PLOTROC, CONFUSION % Copyright 2007-2011 The MathWorks, Inc. nnassert.minargs(nargin,2);
targets = nntype.data('format',targets,'Targets');
outputs = nntype.data('format',outputs,'Outputs');
% TOTO - nnassert_samesize({targets,outputs},{'Targets','Outputs'});
if size(targets,1) > 1
warning(message('nnet:roc:Arguments'));
end
targets = [targets{1,:}];
outputs = [outputs{1,:}];
numClasses = size(targets,1); known = find(~isnan(sum(targets,1)));
targets = targets(:,known);
outputs = outputs(:,known); if (numClasses == 1)
targets = [targets; 1-targets];
outputs = [outputs; 1-outputs-eps*(outputs==0.5)];
[tpr,fpr,thresholds] = roc(targets,outputs);
tpr = tpr{1};
fpr = fpr{1};
thresholds = thresholds{1};
return;
end fpr = cell(1,numClasses);
tpr = cell(1,numClasses);
thresholds = cell(1,numClasses); for i=1:numClasses
[tpr{i},fpr{i},thresholds{i}] = roc_one(targets(i,:),outputs(i,:));
end %%
function [tpr,fpr,thresholds] = roc_one(targets,outputs) numSamples = length(targets);
numPositiveTargets = sum(targets);
numNegativeTargets = numSamples-numPositiveTargets; thresholds = unique([0 outputs 1]);
numThresholds = length(thresholds); sortedPosTargetOutputs = sort(outputs(targets == 1));
numPosTargetOutputs = length(sortedPosTargetOutputs);
sortedNegTargetOutputs = sort(outputs(targets == 0));
numNegTargetOutputs = length(sortedNegTargetOutputs); fpcount = zeros(1,numThresholds);
tpcount = zeros(1,numThresholds); posInd = 1;
negInd = 1;
for i=1:numThresholds
threshold = thresholds(i);
while (posInd <= numPosTargetOutputs) && (sortedPosTargetOutputs(posInd) <= threshold)
posInd = posInd + 1;
end
tpcount(i) = numPosTargetOutputs + 1 - posInd;
while (negInd <= numNegTargetOutputs) && (sortedNegTargetOutputs(negInd) <= threshold)
negInd = negInd + 1;
end
fpcount(i) = numNegTargetOutputs + 1 - negInd;
end tpr = fliplr(tpcount) ./ max(1,numPositiveTargets);
fpr = fliplr(fpcount) ./ max(1,numNegativeTargets);
thresholds = fliplr(thresholds);

  

roc.m的更多相关文章

  1. ROC曲线、PR曲线

    在论文的结果分析中,ROC和PR曲线是经常用到的两个有力的展示图. 1.ROC曲线 ROC曲线(receiver operating characteristic)是一种对于灵敏度进行描述的功能图像. ...

  2. ROC & AUC笔记

    易懂:http://alexkong.net/2013/06/introduction-to-auc-and-roc/ 分析全面但难懂:http://mlwiki.org/index.php/ROC_ ...

  3. 精确率与召回率,RoC曲线与PR曲线

    在机器学习的算法评估中,尤其是分类算法评估中,我们经常听到精确率(precision)与召回率(recall),RoC曲线与PR曲线这些概念,那这些概念到底有什么用处呢? 首先,我们需要搞清楚几个拗口 ...

  4. 【数据挖掘】朴素贝叶斯算法计算ROC曲线的面积

    题记:          近来关于数据挖掘学习过程中,学习到朴素贝叶斯运算ROC曲线.也是本节实验课题,roc曲线的计算原理以及如果统计TP.FP.TN.FN.TPR.FPR.ROC面积等等.往往运用 ...

  5. PR曲线,ROC曲线,AUC指标等,Accuracy vs Precision

    作为机器学习重要的评价指标,标题中的三个内容,在下面读书笔记里面都有讲: http://www.cnblogs.com/charlesblc/p/6188562.html 但是讲的不细,不太懂.今天又 ...

  6. 如何利用Matlab进行ROC分析

    ROC曲线基本知识: 判断分类器的工作效率需要使用召回率和准确率两个变量. 召回率:Recall,又称"查全率", 准确率:Precision,又称"精度".& ...

  7. 机器学习之分类器性能指标之ROC曲线、AUC值

    分类器性能指标之ROC曲线.AUC值 一 roc曲线 1.roc曲线:接收者操作特征(receiveroperating characteristic),roc曲线上每个点反映着对同一信号刺激的感受性 ...

  8. [zz] ROC曲线

    wiki https://zh.wikipedia.org/wiki/ROC%E6%9B%B2%E7%BA%BF 在信号检测理论中,接收者操作特征曲线(receiver operating chara ...

  9. ROC曲线、AUC、Precision、Recall、F-measure理解及Python实现

    本文首先从整体上介绍ROC曲线.AUC.Precision.Recall以及F-measure,然后介绍上述这些评价指标的有趣特性,最后给出ROC曲线的一个Python实现示例. 一.ROC曲线.AU ...

  10. ROC曲线与AUC值

    本文根据以下文章整理而成,链接: (1)http://blog.csdn.net/ice110956/article/details/20288239 (2)http://blog.csdn.net/ ...

随机推荐

  1. mybatis配合pagehelper分页助手查询

    Maven: 参考: springBoot2.x整合pagehelper5.1.2:https://blog.csdn.net/Carlson_Chis/article/details/8563748 ...

  2. PKUWC&SC 2018 刷题记录

    PKUWC&SC 2018 刷题记录 minimax 线段树合并的题,似乎并不依赖于二叉树. 之前写的草率的题解在这里:PKUWC2018 minimax Slay the Spire 注意到 ...

  3. SpringMVC,3种不同的URL路由配置方法 [转]

    SpringMVC中配置URL拦截,非常简单.网上找个示例,就能通过.但是,在我做了好几个Web项目,又参与了别人主导的Web项目时,发现URL配置也非常有学问. 1. 先说说一种比较常见的: < ...

  4. gulp入门之常见处理方式(三)

    整合 streams 来处理错误 默认情况下,在 stream 中发生一个错误的话,它会被直接抛出,除非已经有一个时间监听器监听着 error时间. 这在处理一个比较长的管道操作的时候会显得比较棘手. ...

  5. css3之文本和颜色功能之text-overflow,word-wrap

    语法 text-overflow: clip|ellipsis|string; clip修剪文本.ellipsis显示省略符号来代表被修剪的文本.string使用给定的字符串来代表被修剪的文本. 效果 ...

  6. spring MVC4 配置详解(个人记录)

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. python 当文件目录不存在时,如何自动创建

    import os if not os.path.exists('foldername'): os.mkdir('foldername')

  8. String and Times

    String and Times 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Now you have a string consists of uppercase letters, ...

  9. stringstream的使用 UVA 10815

    水题题目描述就不写了 主要是发现stringstream真的是好用,可以把string绑定到stringstream中,然后就能以空格为分隔符分割出每个单词,听说每次重新创建stringstream开 ...

  10. Iterm2 快捷键介绍

    Mac 原来自带的终端工具 Terminal 不好用是出了名的,虽然最近几个版本苹果稍微做了些优化,功能上,可用性方面增强不少,无奈有个更好用的 Iterm2 摆在那,基本上也就没有多少出场机会了 I ...