How to optimize the variables of a regression model

8 views (last 30 days)
Hello,
I have created a regression model using Statistics and Machine Learning Toolbox that is dependent on 6 variables. After exporting the model to the workspace, I now want to determine the global minimum of the function using "surrogateopt".
With my code, the solver varies the attributes in the specified interval, but the result of the output "fval" does not change.
Can anyone help me with this problem?
objconstr = @(x)trainedModel.predictFcn(data);
lb = [-10,-10,-10,-10,-10,-10];
ub = [10,10,10,10,10,10];
[x,fval] = surrogateopt(objconstr,lb,ub);
trainedModel.predictFcn is the name of the function
data is a table with the 6 attributes
Best
Simon

Accepted Answer

Alan Weiss
Alan Weiss on 9 Aug 2021
I think that you need to connect your data argument to your x argument. Something like this:
function f = objconstr(x,trainedModel)
% Convert x to a table of the correct type.
% For example,
mytable = array2table(x,'VariableNames',...
["name1" "name2" "name3" "name4" "name5" "name6"); % Put in the appropriate names
y = trainedModel.predictFcn(mytable);
end
Then call surrogateopt on the objective function @(x)objconstr(x,trainedModel).
Alan Weiss
MATLAB mathematical toolbox documentation
  2 Comments
Simon Koch
Simon Koch on 16 Aug 2021
Thank you very much for your answer.
I have written the function in a separate script (objconstr.m).
function f = objconstr(x,trainedModel)
data = array2table(x,'VariableNames',["L1","L2","L3","R1","R2","R3"]);
f = trainedModel.predictFcn(data);
end
Now when I call surrogatopt in my original script (optimisation.m), an error message appears.
lb = [-1,-1,-1,-1,-1];
ub = [1,1,1,1,1];
[x,fval] = surrogateopt(@(x)objconstr(x,trainedModel),lb,ub);
Unable to find function @(x)trainedModel.predictFcn(TestData_new) within C:\Users\PCUser\Documents\MATLAB\optimisation.m.
Simon Koch
Alan Weiss
Alan Weiss on 16 Aug 2021
Please perform the following experiment. Get trainedModel into your workspace and make sure that objconstr is on your MATLAB path. Then define fun = @(x)objconstr(x,trainedModel). Then take
x = zeros(1,6); % or any other feasible point
val = fun(x)
See whether this throws the same error. If so, then you have to figure out why before trying to optimize fun. If not, then I think that you are good to go, using fun inside surrogateopt.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!