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));