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. 手机端META详细解释

    一.天猫 <title>天猫触屏版</title> <meta content="text/html; charset=utf-8" http-equ ...

  2. HBase 数据坐标

  3. 【论文翻译】NIN层论文中英对照翻译--(Network In Network)

    [论文翻译]NIN层论文中英对照翻译--(Network In Network) [开始时间]2018.09.27 [完成时间]2018.10.03 [论文翻译]NIN层论文中英对照翻译--(Netw ...

  4. C++对C的扩展、增强

    C++对C的扩展 1. 双冒号::作用域运算符 代码中对同一个变量多次声明,在代码块中使用时,局部变量会将全局变量隐藏.若在代码块使用变量前添加::,表示为全局变量. ::表示作用域运算符,如常见的s ...

  5. Java虚拟机系列(一)---Java内存划分

    Java和C++之间有一堵由内存管理和垃圾收集技术所围成的“高墙”,墙外的人想进去,墙内的人却想出来.  ------摘自<深入理解Java虚拟机> 作为一个Java程序员,因为虚拟机的好 ...

  6. Python爬虫笔记【一】模拟用户访问之Tesseract-ocr验证码训练(5)

    验证码处理之后就需要对处理的验证码进行识别训练,这里用Tesseract-ocr工具进行识别,用jTessBoxeditor进行训练生成模板. 一,对图片进行处理 利用上一篇代码对图片进行降噪处理,得 ...

  7. fork 与 vfork

    fork 函数复制父进程(包括父进程的地址空间)产生子进程 在父进程返回子进程ID,在子进程本身返回0. fork一般有两个用处: 1.网络服务进程等待请求,新请求到来,fork一个子进程处理,父进程 ...

  8. Tarjan求LCA(离线)

    基本思想 把要求的点对保存下来,在dfs时顺带求出来. 方法 将每个已经遍历的点指向它回溯的最高节点(遍历它的子树时指向自己),每遍历到一个点就处理它存在的询问如果另一个点已经遍历,则lca就是另一个 ...

  9. 通过游戏学python 3.6 第一季 第五章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆 可复制直接使用 娱乐 可封装 函数

    #猜数字--核心代码--猜测次数--随机函数和屏蔽错误代码---优化代码及注释--账号密码登陆 #!usr/bin/env python #-*-coding:utf-8-*- #QQ12411129 ...

  10. HTML,CSS,JS优化

    HTML部分 语义化HTML:好处在于可以使代码简洁清晰,支持不同设备,利于搜索引擎,便于团队开发: 减少DOM节点:加速页面渲染: 给图片加上正确的宽高值:这可以减少页面重绘,同时防止图片缩放: 防 ...