Main Content

Optimize a Boosted Regression Ensemble

This example shows how to optimize hyperparameters of a boosted regression ensemble. The optimization minimizes the cross-validation loss of the model.

The problem is to model the efficiency in miles per gallon of an automobile, based on its acceleration, engine displacement, horsepower, and weight. Load the carsmall data, which contains these and other predictors.

load carsmall
X = [Acceleration Displacement Horsepower Weight];
Y = MPG;

Fit a regression ensemble to the data using the LSBoost algorithm, and using surrogate splits. Optimize the resulting model by varying the number of learning cycles, the maximum number of surrogate splits, and the learn rate. Furthermore, allow the optimization to repartition the cross-validation between every iteration.

For reproducibility, set the random seed and use the 'expected-improvement-plus' acquisition function.

rng('default')
Mdl = fitrensemble(X,Y, ...
    'Method','LSBoost', ...
    'Learner',templateTree('Surrogate','on'), ...
    'OptimizeHyperparameters',{'NumLearningCycles','MaxNumSplits','LearnRate'}, ...
    'HyperparameterOptimizationOptions',struct('Repartition',true, ...
    'AcquisitionFunctionName','expected-improvement-plus'))
|====================================================================================================================|
| Iter | Eval   | Objective:  | Objective   | BestSoFar   | BestSoFar   | NumLearningC-|    LearnRate | MaxNumSplits |
|      | result | log(1+loss) | runtime     | (observed)  | (estim.)    | ycles        |              |              |
|====================================================================================================================|
|    1 | Best   |      3.5219 |      16.403 |      3.5219 |      3.5219 |          383 |      0.51519 |            4 |
|    2 | Best   |      3.4752 |     0.76793 |      3.4752 |      3.4777 |           16 |      0.66503 |            7 |
|    3 | Best   |      3.1575 |      1.1122 |      3.1575 |      3.1575 |           33 |       0.2556 |           92 |
|    4 | Accept |      6.3076 |      0.5165 |      3.1575 |      3.1579 |           13 |    0.0053227 |            5 |
|    5 | Accept |      3.4449 |       8.914 |      3.1575 |      3.1579 |          277 |      0.45891 |           99 |
|    6 | Accept |      3.9806 |        2.12 |      3.1575 |      3.1584 |           10 |      0.13017 |           33 |
|    7 | Best   |       3.059 |     0.55029 |       3.059 |        3.06 |           10 |      0.30126 |            3 |
|    8 | Accept |      3.1707 |      1.7519 |       3.059 |      3.1144 |           10 |      0.28991 |           15 |
|    9 | Accept |      3.0937 |     0.49477 |       3.059 |      3.1046 |           10 |      0.31488 |           13 |
|   10 | Accept |       3.196 |     0.37508 |       3.059 |      3.1233 |           10 |      0.32005 |           11 |
|   11 | Best   |      3.0495 |     0.43122 |      3.0495 |      3.1083 |           10 |      0.27882 |           85 |
|   12 | Best   |       2.946 |     0.62152 |       2.946 |      3.0774 |           10 |      0.27157 |            7 |
|   13 | Accept |      3.2026 |     0.38444 |       2.946 |      3.0995 |           10 |      0.25734 |           20 |
|   14 | Accept |       5.595 |      12.321 |       2.946 |      3.0996 |          440 |    0.0010008 |           36 |
|   15 | Accept |      3.1976 |      14.292 |       2.946 |      3.0935 |          496 |     0.027133 |           18 |
|   16 | Accept |      3.9809 |      0.9909 |       2.946 |      3.0927 |           34 |     0.041016 |           18 |
|   17 | Accept |      3.0512 |      10.804 |       2.946 |      3.0939 |          428 |     0.019766 |            3 |
|   18 | Accept |      3.4832 |       5.304 |       2.946 |      3.0946 |          205 |      0.99989 |            8 |
|   19 | Accept |      3.3389 |      2.3905 |       2.946 |      3.0956 |           95 |     0.021453 |            2 |
|   20 | Accept |      3.2818 |      14.142 |       2.946 |      3.0979 |          494 |     0.020773 |           12 |
|====================================================================================================================|
| Iter | Eval   | Objective:  | Objective   | BestSoFar   | BestSoFar   | NumLearningC-|    LearnRate | MaxNumSplits |
|      | result | log(1+loss) | runtime     | (observed)  | (estim.)    | ycles        |              |              |
|====================================================================================================================|
|   21 | Accept |      3.4367 |      13.085 |       2.946 |      3.0962 |          480 |      0.27412 |            7 |
|   22 | Accept |      6.2247 |     0.46585 |       2.946 |      3.0995 |           10 |     0.010965 |           15 |
|   23 | Accept |      3.2847 |       5.195 |       2.946 |      3.0991 |          181 |     0.057422 |           22 |
|   24 | Accept |       3.142 |      6.1645 |       2.946 |      3.0997 |          222 |     0.025594 |           25 |
|   25 | Accept |      3.2174 |     0.60308 |       2.946 |       3.106 |           18 |      0.32203 |           37 |
|   26 | Accept |       3.064 |      2.7115 |       2.946 |      3.1057 |          108 |      0.18554 |            1 |
|   27 | Accept |      3.4532 |      3.1502 |       2.946 |      3.1038 |           93 |      0.22441 |            3 |
|   28 | Accept |      3.1992 |      6.6084 |       2.946 |      3.1038 |          252 |     0.020628 |            3 |
|   29 | Best   |      2.9432 |     0.47337 |      2.9432 |      3.0766 |           10 |      0.36141 |           86 |
|   30 | Best   |       2.891 |     0.38316 |       2.891 |           3 |           10 |      0.38339 |            2 |

Figure contains an axes. The axes with title Min objective vs. Number of function evaluations contains 2 objects of type line. These objects represent Min observed objective, Estimated min objective.

__________________________________________________________
Optimization completed.
MaxObjectiveEvaluations of 30 reached.
Total function evaluations: 30
Total elapsed time: 195.1551 seconds
Total objective function evaluation time: 133.527

Best observed feasible point:
    NumLearningCycles    LearnRate    MaxNumSplits
    _________________    _________    ____________

           10             0.38339          2      

Observed objective function value = 2.891
Estimated objective function value = 2.9674
Function evaluation time = 0.38316

Best estimated feasible point (according to models):
    NumLearningCycles    LearnRate    MaxNumSplits
    _________________    _________    ____________

           10             0.30126          3      

Estimated objective function value = 3
Estimated function evaluation time = 0.58616
Mdl = 
  RegressionEnsemble
                         ResponseName: 'Y'
                CategoricalPredictors: []
                    ResponseTransform: 'none'
                      NumObservations: 94
    HyperparameterOptimizationResults: [1x1 BayesianOptimization]
                           NumTrained: 10
                               Method: 'LSBoost'
                         LearnerNames: {'Tree'}
                 ReasonForTermination: 'Terminated normally after completing the requested number of training cycles.'
                              FitInfo: [10x1 double]
                   FitInfoDescription: {2x1 cell}
                       Regularization: []


  Properties, Methods

Compare the loss to that of a boosted, unoptimized model, and to that of the default ensemble.

loss = kfoldLoss(crossval(Mdl,'kfold',10))
loss = 20.6082
Mdl2 = fitrensemble(X,Y, ...
    'Method','LSBoost', ...
    'Learner',templateTree('Surrogate','on'));
loss2 = kfoldLoss(crossval(Mdl2,'kfold',10))
loss2 = 36.4539
Mdl3 = fitrensemble(X,Y);
loss3 = kfoldLoss(crossval(Mdl3,'kfold',10))
loss3 = 36.6756

For a different way of optimizing this ensemble, see Optimize Regression Ensemble Using Cross-Validation.