Cost in fitcecoc input

조회 수: 3 (최근 30일)
Elena Casiraghi
Elena Casiraghi 2019년 8월 24일
댓글: Elena Casiraghi 2019년 8월 30일
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

채택된 답변

Shashank Gupta
Shashank Gupta 2019년 8월 29일
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
Shashank Gupta
Shashank Gupta 2019년 8월 30일
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 2019년 8월 30일
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!!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by