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

1 次查看(过去 30 天)
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 个评论
Elena Casiraghi
Elena Casiraghi 2019-9-22
编辑:Elena Casiraghi 2019-9-22
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));

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by