unrecognized table variable name minLS in Bayesian optimization of TreeBagger

1 Ansicht (letzte 30 Tage)
Hello,
I tried to do the hyperparameter tuning using bayesian optimization for the randomforest model I made using Treebagger. This is the code I used. I didnot use ensemble bagged trees in regression learner directly because eachtime i check the optimization the minimum mse output is like 498 learners and 1 minimum leaf size for 9 number of predictors.
264 inputTable=readtable('dataall_trainingregression.csv');
265 predictorNames = {'temp_diff', 'temp_median', 'NDVI', 'Clay', 'elevation', 'slope', 'TWI', 'sand', 'DOY'};
266 predictors = inputTable(:, predictorNames);
267 response = inputTable.daily_meanSM;
268 n=length(inputTable.daily_meanSM);
269 cvp = cvpartition(n,'KFold',5);
271 maxMinLS = 20;
272 minLS = optimizableVariable('minLS',[1,maxMinLS],'Type','integer');
273 numPTS = optimizableVariable('numPTS',[1,size(predictors,2)],'Type','integer');
274 hyperparametersRF = [minLS; numPTS];
275 fun = @(params)crossval('mse',predictors,response,'Predfun',@myfunction,'Partition',cvp);
277 results = bayesopt(fun,hyperparametersRF,...
'AcquisitionFunctionName','expected-improvement-plus','Verbose',0);
280 function yfit = myfunction(params,predictors,response,test)
281 Mdl1 = TreeBagger(30,predictors,response,...
282 'Method',"regression",'Surrogate',"on",...
283 'PredictorSelection',"curvature",...
284 'OOBPredictorImportance',"on",'MinLeafSize',params.minLS,...
285 'NumPredictorsToSample',params.numPTS);
286 yfit = predict(Mdl1,test);
288 end
for this code i receive an error message of :
Error using crossval>evalFun
The function 'myfunction' generated the following error:
Unrecognized table variable name 'minLS'.
Error in crossval>getLossVal (line 529)
funResult = evalFun(funorStr,arg(1:end-1));
Error in crossval (line 428)
[funResult,outarg] = getLossVal(i, nData, cvp, data, predfun);
Error in model_hyperparameter_tuning>@(params)crossval('mse',predictors,response,'Predfun',@myfunction,'Partition',cvp) (line 275)
fun = @(params)crossval('mse',predictors,response,'Predfun',@myfunction,'Partition',cvp);
Error in BayesianOptimization/callObjNormally (line 13)
Objective = this.ObjectiveFcn(conditionalizeX(this, X));
Error in BayesianOptimization/callObjFcn (line 25)
= callObjNormally(this, X);
Error in BayesianOptimization/runSerial (line 24)
ObjectiveFcnObjectiveEvaluationTime, ObjectiveNargout] = callObjFcn(this, this.XNext);
Error in BayesianOptimization/run (line 9)
this = runSerial(this);
Error in BayesianOptimization (line 184)
this = run(this);
Error in bayesopt (line 323)
Results = BayesianOptimization(Options);
Error in model_hyperparameter_tuning (line 277)
results = bayesopt(fun,hyperparametersRF,...
can anyone please help me to solve this problem. Thanks

Antworten (1)

Jack
Jack am 9 Mär. 2025
Hey Kalani,
The error "Unrecognized table variable name 'minLS'" happens because MATLAB is trying to access params.minLS as if params were a table, but in your function myfunction, params is actually a struct, not a table.
Fix: Access params as a struct
Change this line in myfunction:
Mdl1 = TreeBagger(30,predictors,response,...
'Method',"regression",'Surrogate',"on",...
'PredictorSelection',"curvature",...
'OOBPredictorImportance',"on",'MinLeafSize',params.minLS,...
'NumPredictorsToSample',params.numPTS);
to
Mdl1 = TreeBagger(30, predictors, response,...
'Method', "regression", 'Surrogate', "on",...
'PredictorSelection', "curvature",...
'OOBPredictorImportance', "on", 'MinLeafSize', params.minLS,...
'NumPredictorsToSample', params.numPTS);
Alternative Fix:
If the error persists, another way to reference params safely is to convert it to a structure explicitly:
params = table2struct(params);
This ensures MATLAB treats params as a struct with fields minLS and numPTS.
This should resolve the issue and allow your Bayesian optimization to run properly.
Follow me so you can message me anytime with future MATLAB questions. If this helps, please accept the answer as well.
  2 Kommentare
Walter Roberson
Walter Roberson am 9 Mär. 2025
?? The only difference I can find between those two code blocks is in the spacing after commas ? Spaces after commas are good for human readability but make no difference to MATLAB's interpretation.
Jack
Jack am 9 Mär. 2025
You're right.
Fix
Inside myfunction, add:
params = table2struct(params);
So your function becomes:
function yfit = myfunction(params, predictors, response, test)
params = table2struct(params); % Convert params to struct
Mdl1 = TreeBagger(30, predictors, response, ...
'Method', "regression", 'Surrogate', "on", ...
'PredictorSelection', "curvature", ...
'OOBPredictorImportance', "on", 'MinLeafSize', params.minLS, ...
'NumPredictorsToSample', params.numPTS);
yfit = predict(Mdl1, test);
end
This makes sure MATLAB recognizes params.minLS and params.numPTS properly.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by