Hyperparameter optimization for ECOC: how can I choose the variables to be minimized?

1 view (last 30 days)
1) When training an ECOC classifier for multiclass classification, with knn as base learner, I would like to optimize only the NumNeighbors parameter.
How can it be done?
I'm now using this code:
% variable labels contains the labels of training data
tknn = templateKNN('Distance', @distKNN); % I WOULD LIKE TO USE THIS DISTANCE
N = size(XKnn,1);
c = cvpartition(N,'LeaveOut');
% Use leave one out
mdlknnCecoc = fitcecoc(XKnn,labelsRed, ...
'OptimizeHyperparameters','auto', ...
'HyperparameterOptimizationOptions',struct( 'UseParallel',...
true,'CVPartition',c), 'Learners',tknn);
resPreds = predict(mdlknnCecoc, XKnn); % I don't know why kfoldPredict function does not work
myLoss = double(sum(abs(resPreds - labels)))/double(N); % this is the loss function I wish to minimize
  1 Comment
Elena Casiraghi
Elena Casiraghi on 22 Sep 2019
Edited: Elena Casiraghi on 22 Sep 2019
I'm now trying to use the following code to choose the parameter to optimize (and to use my own ditance function for the knns and my own loss function). It's working, at least it seems so. Could anyone confirm me that I'm doing it right? How can I use optimizableVariable function to optimize the value argument Coding?
% nTrain = number of elements in my training set
% distKnn is my distance function
% myLoss is myLoss function
% maxK is the maximum value for the number of neighbors
num = optimizableVariable('n',[1,maxK],'Type','integer');
c = cvpartition(nTrain,'LeaveOut');
fun = @(x)kfoldLoss(fitcecoc(XKnn,labelsRed,'CVPartition',c,...
'Learners',templateKNN('Distance', @distKNN,'NumNeighbors',x.n)),'LossFun',@myLoss);
results = bayesopt(fun,num,'Verbose',0,'UseParallel',true);
% train a model with the best parameter
bestMdl = fitcecoc(XKnn,labelsRed,'CVPartition',c,...
'Learners',templateKNN('Distance', @distKNN,'NumNeighbors',results.n));

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!