Cost in fitcecoc input
11 views (last 30 days)
Elena Casiraghi on 24 Aug 2019
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
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, ...
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?
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!