根据决策值和真实标签画ROC曲线,同时计算AUC的值
步骤:
- 根据决策值和真实标签画ROC曲线,同时计算AUC的值:
- 计算算法的决策函数值deci
- 根据决策函数值deci对真实标签y进行降序排序,得到新的排序
- 根据分别对正负类样本进行累积分布,
- 根据,$stack_y$计算RUC的值
- 分别以,作为横坐标和纵坐标,画出RUC图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 |
代码来自林智仁网站:https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/#roc_curve_for_binary_svm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | 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, '-v\s+\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 |
调用:
1 2 3 | [y,x] = libsvmread( 'heart_scale.txt' ); model = svmtrain(y,x); plotroc(y,x,model); |
标签:
ROC
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧