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. eclipse安装m2e

    Installation You can install last M2Eclipse release by using the following update site from within E ...

  2. 经典分类CNN模型系列其五:Inception v2与Inception v3

    经典分类CNN模型系列其五:Inception v2与Inception v3 介绍 Inception v2与Inception v3被作者放在了一篇paper里面,因此我们也作为一篇blog来对其 ...

  3. python 连接mssql数据库

    1.目标数据sql2008 R2 ComPrject=>TestModel 2.安装python 连接mssql 模块 运行 pip install pymssql-2.2.0.dev0-cp3 ...

  4. 一条sql获取每个类别最新的一条记录

    1.初始化数据 create table Products ( id ,), name ), categroy int, addtime datetime , ) insert into Produc ...

  5. 常见的5个runtime exception

    NullPointException(空指针异常),ArrIndexOutOfBoundsException(数组越界异常),ClassCastException(类型转换异常),ClassNotFo ...

  6. ssh小知识

    1.查看系统在线用户 [root@testdb ~]# w 14:30:26 up 38 days, 21:22, 3 users, load average: 0.00, 0.01, 0.05USE ...

  7. 技术 | TypeScript

    技术 | TypeScript   第一次接触TypeScript还是和一帮兄弟在居民楼里撸每日优鲜App的时候,没有想过那么多,只想可以快速实现和快速落地,于是我们选择ionic这个Hybrid框架 ...

  8. 修改maven本地库路径

    1.打开maven的安装路径:${M2_HOME}/conf/setings.xml文件 2.找到<localRepository>项,将它的值修改就可以了,我修改的是:${M2_HOME ...

  9. python-web-下载所有xkcd漫画

    下载所有xkcd漫画 # downloads every single xkcd comic import requests,os,bs4 url='http://xkcd.com' # start ...

  10. 【python之路35】网络编程之socket相关

    Socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. sock ...