Bayesian Optimization ionosphere example
3 views (last 30 days)
Show older comments
How to understand the program in the penultimate line?Thank you for any advice.
%% https://www.mathworks.com/help/stats/bayesopt.html
clear;clc;
load ionosphere
rng default
num = optimizableVariable('n',[1,30],'Type','integer');
dst = optimizableVariable('dst',{'chebychev','euclidean','minkowski'},'Type','categorical');
vars = [num,dst];
c = cvpartition(351,'Kfold',5);
fun = @(x)kfoldLoss(fitcknn(X,Y,'CVPartition',c,'NumNeighbors',x.n,'Distance',char(x.dst),'NSMethod','exhaustive'));%How to understand this line??
What is 'x' and 'x.n'? Does the 'fitcknn(...)' outputs validation loss? Thanks a lot.
results = bayesopt(fun,vars,'Verbose',1,'AcquisitionFunctionName','expected-improvement-plus')
0 Comments
Answers (2)
Alan Weiss
on 26 Jul 2020
The x.n argument takes the 'n' field of the table x, and the x.dst argument takes the dst field of the table x. You see that the earlier lines of code set up the optimizable variables n and tbl. The variable x is the name given in the @(x) line for the variable of optimization, which, as I stated, is a table variable. You can read about these things in the Hyperparameter Optimization section of Model Building and Assessment.
The objective function fun computes the cross-validation loss of the k-nearest-neighbor classifier for given values of the optimizable parameters.
Alan Weiss
MATLAB mathematical toolbox documentation
Alan Weiss
on 26 Jul 2020
I think that you need to do some reading in Model Building and Assessment. Please read about how to specify variables and objective functions. Your objective function is a function of ONE VARIABLE, typically called x. It is not a function of x.a, x.b, etc. Just x. If you need to pass extra parameters that are not optimizable variables, well, the examples show how to include them, too.
Alan Weiss
MATLAB mathematical toolbox documentation
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!