Cost in fitcecoc input

6 views (last 30 days)
Elena Casiraghi
Elena Casiraghi on 24 Aug 2019
Commented: Elena Casiraghi on 30 Aug 2019
Dear,
I'm trying to train an ecoc model using svm as base classifiers.
Since the data in X is unbalanced, I would like to use cost-sensitive svms, that is, I would like to use the misclassification cost matrix, where where cost(i,j) is the cost of misclassifying i into class j.
Using svms I could then use the following code:
numLabels = 5; % suppose I have 5 classes
cost = zeros(numLabels,numLabels);
for nL1 = 1: numLabels; for nL2 = 1: numLabels
if nL1 == nL2; cost(nL1,nL1)=0;
else; cost(nL1,nL2)= double(sum(uint8(labelsRed==nL1)))/double(sum(uint8(labelsRed==nL2))); end
end; end
X = randi(1000, 21); % my data has 1000 training points, each having 21 features
labels = randi(5,1000,1);
mdl = fitcsvm(X, labels, 'Cost', cost,'Standardize',true,'Leaveout','on','KernelFunction','rbf','OptimizeHyperparameter', 'auto');
% if I wanted to create the cecoc I'm doing
t = templateSVM('Standardize',true,'KernelFunction','rbf');
mdlsvmCecoc = fitcecoc(X,labels, ...
'Leaveout','on', 'Cost', cost, ...
'OptimizeHyperparameter', 'auto',...
'coding', 'ternarycomplete','Learners',t);
is it right?
Is the cost used here to weight the 5 classes in different ways?
I'm asking since cecoc produces many binary problems by somehow splitting the classes.
How is the cost used here?
THANKS

Accepted Answer

Shashank Gupta
Shashank Gupta on 29 Aug 2019
I am not sure what LabelsRed variable is in your code but let’s just assume you have defined misclassification cost matrix correctly.
MATLAB function “fitcecoc” trains or cross-validate an SVM only, Since SVM are binary learner models only and therefore this function treats multiple classes as a combined binary SVM model. By default, it uses a one-versus-one coding design, you can understand the model design by accessing the “mdlsvmCecoc” object, you can also look at each of the binary learner by accessing “BinaryLearner” property of the object. So probably that’s the reason of getting binary problems.
I hope it helps!
  3 Comments
Shashank Gupta
Shashank Gupta on 30 Aug 2019
Hi Elena,
I kind of understand what you are trying to say, sorry if I misinterpreted you question earlier.
Let’s say the “ecoc” first solving a binary problem between 1st and 3rd class, then SVM optimizer choose the corresponding submatrix defined in cost matrix (which is cost([1,3],[1,3]) in our case), like this for every binary problem, “ecoc” can always find a corresponding cost submatrix to optimize with.
I hope this make more sense.
Elena Casiraghi
Elena Casiraghi on 30 Aug 2019
Thanks! I didn't Think it was like that because I know ecoc can mix classes when building the binary classifiers. But anyway, this solves the problem!!!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!