MATLAB Draw Roc Curve, and calculate AUC value

Source: Internet
Author: User

Draw ROC curves based on decision values and real labels while calculating AUC values

function AUC = Roc_curve (deci,label_y)%%deci=wx+b, label_y, true label[val,ind] = sort (deci, ' descend '); roc_y = Label_y (i nd); 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:lengt H (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);p lot (stack_x,stack_y); Xlabel (' False Positive rate '), Ylabel (' True Positive rate '), title ([' ROC curve of (AUC = ' num2str (AUC) ') '); end

  

Code from Linzhiren website: https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/#roc_curve_for_binary_svm

function AUC = Plotroc (y,x,params)%plotroc draws the Recevier operating characteristic (ROC) Curve.%%auc = Plotroc (trainin G_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);% PLO Troc (Y,x,model); rand (' state ', 0); % reset Random seedif nargin < 2help Plotrocreturnelseif isempty (y) | IsEmpty (x) error (' Input data is empty '), ElseIf sum (y = = 1) + sum (y = =-1) ~= Length (y) error (' ROC is only applicable to Bina RY classes with labels 1,-1 '); % Check the Trainig_file is Binaryelseif exist (' params ') && ~ischar (params) model = params; [Predict_label,mse,deci] = Svmpredict (Y,x,model);% the procedure for PREDICTINGAUC = Roc_curve (Deci*model. Label (1), y), ElseIf ~exist (' params ') params = [];end[param,fold] = PROC_ARGV (params); % specify each Parameterif fold <= 1error (' The number of folds must is greater than 1 '); else[deci,label_y] = Get_cv_dec I (Y,x,param,fold); % get the value of decision and label after CROSS-CALIDATIONAUC = Roc_curve (deci,label_y); % plot ROC curveendendendfunction [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 is specified by "-V cv_fold" '); End Endendfunction [deci,label_y] = Get_cv_deci (prob_y,prob_x,param,nr_fold) l=length (prob_y);d eci = ones (l,1); label_ y = ones (l,1); Rand_ind = Randperm (l); For i=1:nr_fold percent cross Training:folDingtest_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);d ECI (test_ind) = subdeci.* Model. Label (1); label_y (Test_ind) = Prob_y (test_ind); Endendfunction 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]=per Fcurve (label_y,deci,1);p lot (stack_x,stack_y), Xlabel (' False Positive rate '), Ylabel (' True Positive rate '); title ([' ROC curve of (AUC = ' num2str (AUC) ') '); end

Call:

[Y,x] = Libsvmread (' heart_scale.txt ');
Model = Svmtrain (y,x);
Plotroc (Y,x,model);

MATLAB Draw Roc Curve, and calculate AUC value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.