MATLAB: How to create a regression object using the exported function from the Regression Learner App that has the same size as the exported network from the Regression Learner App when using the option “Export Compact Model”

Statistics and Machine Learning Toolbox

The Regression Learner App has the option to export the trained model as compact using the option "Export Compact Model", which results in obtaining a regression object that is lighter than the complete regression object. Using the "Export Compact Model" option then, the exported regression object does not contain any training data, thus being lighter.
When exporting a function using the option "Generate Function" and then using the generated function in order to create the same regression object, then the object still has the training data and it is larger than the regression object exported from the Regression Learner App using the "Export Compact Model" option.
How can I create a regression object using the exported function from the Regression Learner App that has the same size as the exported network from the Regression Learner App when using the option "Export Compact Model"?

Best Answer

In order to achieve this workflow, the generated function that is exported from the Regression Learner App using the option "Generate Function" should be customized.
To do so, a call to function 'compact' needs to be added after creating the 'regressionTree' object in the exported function, namely,
regressionTree = fitrtree(...
predictors, ...
response, ...
'MinLeafSize', 4, ...
'Surrogate', 'off');
regressionTree = compact(regressionTree);
 
For more information on how to construct a compact regression tree, please visit the following documentation page,
Then, the following lines in the exported function should be comment out,
 
% Perform cross-validation
% partitionedModel = crossval(trainedModel.RegressionTree, 'KFold', 5);
% Compute validation predictions
% validationPredictions = kfoldPredict(partitionedModel);
% Compute validation RMSE
% validationRMSE = sqrt(kfoldLoss(partitionedModel, 'LossFun', 'mse'));
 
because the RMSE cannot be computed anymore since the regression object no longer has training data. Lastly, the function signature of the exported function needs to be changed, such that it no longer returns variable 'validationRMSE', namely,
function trainedModel = trainRegressionModel(trainingData, responseData)
Then, the resulting regression object should have the same size as the one exported by the Regression Learner App when using option "Export Compact Model".
Related Question