Multiclass Logistic Regression ROC Curves in MATLAB

Viewed 132

I have 7 classes within my training examples (labeled 1-7). I'm running logistic regression and I want to create my ROC curve for each of my classes.

To train my model and make a prediction, I have the following code:

Theta = zeros(k, n+1); %initialize theta 
[Theta, costs] = gradientDescent(Theta, @(t)(CostFunc(t, X, Y, lambda)),...
    @(t)(DerivOfCostFunc(t, X, Y, lambda)), alpha, iter_num);

%Make prediction with trained model
[scores,prediction] = predict(Theta, X_test); %X_test is the design matrix (ones on the first col)

Within the predict script, I have

scores = g(X*all_theta'); %this is the sigmoid function
[p_max, IndexOfMax]=max(scores, [], 2);
prediction = IndexOfMax;

Note that scores is a m by k matrix, where m is the number of training examples and k is the number of classes. Prediction is a m by 1 vector with numbers going from 1-7, based on the predicted class. To create the ROC curve, for class 3 for example,

classNum=3;
for i=1:size(scores,1)
      temp=scores(i,:); 
      diffscore(i,:)=temp(classNum)-max([temp(:,1:classNum-1),temp(:,classNum+1:end)]); 
end

This last part I did because I read that I had to establish my class 3 as positive and the others as negative.

At last, I made my curve with the following code:

[xROC,yROC,~,auc] = perfcurve(y_test,diffscore,classNum);
%y_test contains my true labels, m by 1 column vector

However, when running the ROC curve for each of my classes, I get the same plot for all. They all have an AUC of 1. Based on some analysis, I know this is not correct but can't figure out in which part of the code I went wrong! Is there additional code I should add or should I need to modify any of my existing code?

0 Answers
Related