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. gatekeeper学习概述

    1.概述 该产品部署在网络隔离装置两端,以代理程序的身份,完成两侧设备连接维护,数据转发的功能.场景简化如图所示: 软件核心是一个基于Netty的网络应用程序,考虑到系统的可维可测性,集成了web化的 ...

  2. ie浏览器下载附件中文乱码

    String llq = request.getHeader( "USER-AGENT" ).toLowerCase();Boolean isIE = false;if (llq. ...

  3. JVM系列(四)— 原子性、可见性与有序性

    上一篇讲了Java内存模型的相关知识,模型设计正是围绕着并发过程中如何处理原子性,可见性和有序性这3个特征来建立的 一.原子性(Atomicity) 原子性的概念无需多说,熟悉事物的4个特性的应该比较 ...

  4. C# 把十六进制表示的ASCII码转换为对应的字符组成的字符串

    0x30表示字符‘0’的ASCII码.

  5. 05-4-style的代替操作

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 05.Hibernate常用的接口和类---Configuration类和作用

    Configuration作用: 加载Hibernate配置文件,可以获取SessionFactory对象 加载方式: 1.加载配置文件 Configuration configuration = n ...

  7. 机器学习入门:K-近邻算法

    机器学习入门:K-近邻算法 先来一个简单的例子,我们如何来区分动作类电影与爱情类电影呢?动作片中存在很多的打斗镜头,爱情片中可能更多的是亲吻镜头,所以我们姑且通过这两种镜头的数量来预测这部电影的主题. ...

  8. R语言画图教程之盒形图

    R语言画图教程之盒形图 我们之前有分享过一系列的R语言画图代码(PCA图.Pathway图.火山图.RDA图.热图),今天再来补充一个盒形图(箱形图)的代码. 以下代码只是示例,不能直接搬来用哦,注意 ...

  9. [转载] DSP6000图像位移与变形典型算法

    原文地址:转载:DSP6000图像位移与变形典型算法作者:Jane 李现路:DSP6000图像位移与变形典型算法 一.图像的平移算法 图像平移的数学表达式原理: 初始坐标为(x0,y0)的点经过平移( ...

  10. pytest框架-介绍、Mark(打标签)、命令运行用例、用例执行顺序、

    1.pytest介绍:基于unittest 之上的单元测试框架 1.1.自动发现测试模块和测试用例: unitest 需要添加用例,(泰斯特楼贷)加载器加载测试用例 pytest 只需要一条代码就可以 ...