Plot the ROC curve in libsvm

Source: Internet
Author: User
Convert from labels (true_labels, predict_labels, classnumber)

A friend asked me how to use MATLAB to plot the ROC curve of lisvm results, so I stayed up late and got a little bit.
The subject is implemented using the plotroc provided by Matlab. Some preprocessing is added.

You can plot the ROC of the data of any number of categories, and the category tag of the data can be any. (Not necessarily + 1 and-1)

LSSVM has functions that can draw ROC curves, but the class labels can only be-1 and + 1

The program is simple. However, the previous preprocessing is quite interesting. In addition, I spent some time thinking about the combination of plotroc and later (and finally implemented with Eval ). O (partition _ partition) O

==== The friend posted a wrong post
How to draw ROC curves using LIBSVM-MAT?
Http://www.ilovematlab.cn/thread-62962-1-1.html

====

What is the ROC curve? I don't know ...!??
Understanding is naturally clear. I don't understand Baidu .. I will not talk about it. Go to bed first. You have to study tomorrow
End-time review ing ....

Test diagram

Test 1 two category labels of hearscale data-1 + 1


1. jpg


2. jpg

====================

Test 2. Three types of wine data labels 1 2 3


3. jpg


4. jpg

Code:
================== Plotsvmroc (true_labels, predict_labels, classnumber) ======

Code:

function plotSVMroc(true_labels,predict_labels,classnumber)
% plotSVMroc
% by faruto
% Email:farutoliyang@gmail.com
% 2010.01.11
%%
len = length(true_labels);
label = zeros(1,classnumber);
label(1) = true_labels(1);
currentindex = 1;
for run = 2:len
    flag = 0;
    for class = 1:currentindex
        if true_labels(run) == label(class)  
            flag = 1;
        end
    end
    if flag == 1
        continue;
    else        
        label(currentindex+1) = true_labels(run);
        currentindex = currentindex + 1;
    end
end
%%
targets_true = zeros(classnumber,len);
outputs_predict = zeros(classnumber,len);
for class = 1:classnumber
    for run = 1:len
        if true_labels(run) == label(class)
            targets_true(class,run) = 1;
        end
        if predict_labels(run) == label(class)
            outputs_predict(class,run) = 1;
        end
    end
end
%% plot ROC curve
plotroc(targets_true,outputs_predict);
grid on;
%% plot ROC curve subplot
str = ['plotroc('];
for class = 1:classnumber
    
    str_temp = ['targets_true(',num2str(class),...
        ',:),','outputs_predict(',num2str(class),',:),',...
        '''class ','',num2str(class),'(label:',...
        num2str(label(class)),')''',','];
    
    str = [str str_temp];
    
end
str = str(1:end-1);
str = [str ')'];
eval(str);
=========== Test code

Code:

%% plotSVMroc_test
%%
clear;
clc;
%%
load wine_test
%%
[train_data,test_data] = scaleForSVM(train_data,test_data,0,1); 
model = svmtrain(train_data_labels,train_data);
[pre,acc] = svmpredict(test_data_labels,test_data,model);
%% plotSVMroc
plotSVMroc(test_data_labels,pre,3)

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.