fitrauto
Automatically select regression model with optimized hyperparameters
Since R2020b
Syntax
Description
Given predictor and response data, fitrauto
automatically
tries a selection of regression model types with different hyperparameter values. By default,
the function uses Bayesian optimization to select models and their hyperparameter values, and
computes the following for each model: log(1 + valLoss), where valLoss is the crossvalidation mean squared error
(MSE). After the optimization is complete, fitrauto
returns the model,
trained on the entire data set, that is expected to best predict the responses for new data.
You can use the predict
and loss
object functions of
the returned model to predict on new data and compute the test set MSE,
respectively.
Use fitrauto
when you are uncertain which model types best suit your
data. For information on alternative methods for tuning hyperparameters of regression models,
see Alternative Functionality.
If your data contains over 10,000 observations, consider using an asynchronous successive
halving algorithm (ASHA) instead of Bayesian optimization when you run
fitrauto
. ASHA optimization often finds good solutions faster than
Bayesian optimization for data sets with many observations.
returns a regression model Mdl
= fitrauto(Tbl
,ResponseVarName
)Mdl
with tuned hyperparameters. The table
Tbl
contains the predictor variables and the response variable,
where ResponseVarName
is the name of the response variable.
specifies options using one or more namevalue arguments in addition to any of the input
argument combinations in previous syntaxes. For example, use the
Mdl
= fitrauto(___,Name,Value
)HyperparameterOptimizationOptions
namevalue argument to specify
whether to use Bayesian optimization (default) or an asynchronous successive halving
algorithm (ASHA). To use ASHA optimization, specify
"HyperparameterOptimizationOptions",struct("Optimizer","asha")
. You
can include additional fields in the structure to control other aspects of the
optimization.
[
also returns Mdl
,OptimizationResults
] = fitrauto(___)OptimizationResults
, which contains the results of the
model selection and hyperparameter tuning process. This output is a
BayesianOptimization
object when you use Bayesian optimization, and a
table when you use ASHA optimization.
Examples
Automatically Select Regression Model Using Table Data
Use fitrauto
to automatically select a regression model with optimized hyperparameters, given predictor and response data stored in a table.
Load Data
Load the carbig
data set, which contains measurements of cars made in the 1970s and early 1980s.
load carbig
Create a table containing the predictor variables Acceleration
, Displacement
, and so on, as well as the response variable MPG
.
cars = table(Acceleration,Displacement,Horsepower, ...
Model_Year,Origin,Weight,MPG);
Remove rows of cars
where the table has missing values.
cars = rmmissing(cars);
Categorize the cars based on whether they were made in the USA.
cars.Origin = categorical(cellstr(cars.Origin)); cars.Origin = mergecats(cars.Origin,["France","Japan",... "Germany","Sweden","Italy","England"],"NotUSA");
Partition Data
Partition the data into training and test sets. Use approximately 80% of the observations for the model selection and hyperparameter tuning process, and 20% of the observations to test the performance of the final model returned by fitrauto
. Use cvpartition
to partition the data.
rng("default") % For reproducibility of the data partition c = cvpartition(height(cars),"Holdout",0.2); trainingIdx = training(c); % Training set indices carsTrain = cars(trainingIdx,:); testIdx = test(c); % Test set indices carsTest = cars(testIdx,:);
Run fitrauto
Pass the training data to fitrauto
. By default, fitrauto
determines appropriate model types to try, uses Bayesian optimization to find good hyperparameter values, and returns a trained model Mdl
with the best expected performance. Additionally, fitrauto
provides a plot of the optimization and an iterative display of the optimization results. For more information on how to interpret these results, see Verbose Display.
Expect this process to take some time. To speed up the optimization process, consider running the optimization in parallel, if you have a Parallel Computing Toolbox™ license. To do so, pass "HyperparameterOptimizationOptions",struct("UseParallel",true)
to fitrauto
as a namevalue argument.
Mdl = fitrauto(carsTrain,"MPG");
Learner types to explore: ensemble, svm, tree Total iterations (MaxObjectiveEvaluations): 90 Total time (MaxTime): Inf ================================================================================================================================================  Iter  Eval  log(1+valLoss) Time for training  Observed min  Estimated min  Learner  Hyperparameter: Value    result   & validation (sec) validation loss  validation loss    ================================================================================================================================================  1  Best  3.3416  0.1617  3.3416  3.3416  tree  MinLeafSize: 118   2  Accept  4.1303  0.10689  3.3416  3.3416  svm  BoxConstraint: 16.579          KernelScale: 0.0045538          Epsilon: 657.79   3  Best  2.5197  0.077313  2.5197  2.6121  tree  MinLeafSize: 2   4  Best  2.3335  4.6922  2.3335  2.3335  ensemble  Method: Bag          NumLearningCycles: 291          MinLeafSize: 9   5  Accept  2.3398  3.1524  2.3335  2.3366  ensemble  Method: Bag          NumLearningCycles: 206          MinLeafSize: 13   6  Best  2.204  4.9207  2.204  2.2049  ensemble  Method: LSBoost          NumLearningCycles: 256          MinLeafSize: 12   7  Accept  4.1303  0.055823  2.204  2.2049  svm  BoxConstraint: 0.0048178          KernelScale: 0.011576          Epsilon: 441.39   8  Accept  2.4787  0.060431  2.204  2.2049  tree  MinLeafSize: 9   9  Accept  4.1303  0.03731  2.204  2.2049  svm  BoxConstraint: 8.581          KernelScale: 61.095          Epsilon: 296.69   10  Accept  4.1303  0.035203  2.204  2.2049  svm  BoxConstraint: 140.96          KernelScale: 0.012197          Epsilon: 69.002   11  Accept  2.9157  0.035004  2.204  2.2049  tree  MinLeafSize: 32   12  Accept  3.2199  0.033218  2.204  2.2049  tree  MinLeafSize: 64   13  Accept  2.4157  0.038466  2.204  2.2049  tree  MinLeafSize: 4   14  Accept  4.1303  0.039282  2.204  2.2049  svm  BoxConstraint: 1.3859          KernelScale: 71.061          Epsilon: 181.44   15  Accept  3.4156  0.032522  2.204  2.2049  tree  MinLeafSize: 102   16  Accept  2.5197  0.042514  2.204  2.2049  tree  MinLeafSize: 2   17  Accept  5.4306  23.255  2.204  2.2049  svm  BoxConstraint: 0.0018102          KernelScale: 0.016815          Epsilon: 8.1687   18  Accept  3.1121  4.0538  2.204  2.2042  ensemble  Method: Bag          NumLearningCycles: 288          MinLeafSize: 106   19  Best  2.1971  4.4769  2.1971  2.1972  ensemble  Method: LSBoost          NumLearningCycles: 227          MinLeafSize: 2   20  Best  2.1971  4.1893  2.1971  2.1972  ensemble  Method: LSBoost          NumLearningCycles: 223          MinLeafSize: 2  ================================================================================================================================================  Iter  Eval  log(1+valLoss) Time for training  Observed min  Estimated min  Learner  Hyperparameter: Value    result   & validation (sec) validation loss  validation loss    ================================================================================================================================================  21  Accept  2.2314  5.0182  2.1971  2.1972  ensemble  Method: LSBoost          NumLearningCycles: 262          MinLeafSize: 5   22  Accept  2.5925  4.2399  2.1971  2.1972  ensemble  Method: Bag          NumLearningCycles: 283          MinLeafSize: 39   23  Accept  2.1971  5.466  2.1971  2.1972  ensemble  Method: LSBoost          NumLearningCycles: 285          MinLeafSize: 2   24  Accept  2.3352  1.6689  2.1971  2.1972  svm  BoxConstraint: 2.2648          KernelScale: 0.92531          Epsilon: 0.51865   25  Accept  2.9882  3.4342  2.1971  2.1971  ensemble  Method: Bag          NumLearningCycles: 247          MinLeafSize: 73   26  Accept  2.3583  3.7169  2.1971  2.1971  ensemble  Method: Bag          NumLearningCycles: 255          MinLeafSize: 12   27  Accept  2.6476  0.052651  2.1971  2.1971  tree  MinLeafSize: 28   28  Accept  2.4016  0.045653  2.1971  2.1971  tree  MinLeafSize: 6   29  Accept  3.7573  0.054446  2.1971  2.1971  svm  BoxConstraint: 9.4057          KernelScale: 100.66          Epsilon: 0.24447   30  Accept  2.6046  0.038702  2.1971  2.1971  tree  MinLeafSize: 24   31  Accept  2.4157  0.038974  2.1971  2.1971  tree  MinLeafSize: 4   32  Accept  4.1303  0.034693  2.1971  2.1971  svm  BoxConstraint: 303.85          KernelScale: 0.0083624          Epsilon: 39.54   33  Accept  4.146  0.039632  2.1971  2.1971  svm  BoxConstraint: 0.16546          KernelScale: 248.79          Epsilon: 1.1182   34  Accept  3.0466  0.035622  2.1971  2.1971  tree  MinLeafSize: 46   35  Accept  2.3417  3.6854  2.1971  2.1971  ensemble  Method: Bag          NumLearningCycles: 246          MinLeafSize: 12   36  Accept  2.7264  5.237  2.1971  2.1972  ensemble  Method: LSBoost          NumLearningCycles: 274          MinLeafSize: 105   37  Accept  2.5457  3.608  2.1971  2.1972  ensemble  Method: Bag          NumLearningCycles: 257          MinLeafSize: 33   38  Accept  2.6603  0.057475  2.1971  2.1972  tree  MinLeafSize: 1   39  Accept  2.3589  2.9631  2.1971  2.1972  svm  BoxConstraint: 56.509          KernelScale: 1.509          Epsilon: 0.5604   40  Accept  4.1303  0.037567  2.1971  2.1972  svm  BoxConstraint: 0.001484          KernelScale: 0.0032176          Epsilon: 22.445  ================================================================================================================================================  Iter  Eval  log(1+valLoss) Time for training  Observed min  Estimated min  Learner  Hyperparameter: Value    result   & validation (sec) validation loss  validation loss    ================================================================================================================================================  41  Accept  2.5507  0.045858  2.1971  2.1972  tree  MinLeafSize: 15   42  Best  2.1945  5.5104  2.1945  2.1966  ensemble  Method: LSBoost          NumLearningCycles: 289          MinLeafSize: 6   43  Accept  3.9788  0.050732  2.1945  2.1966  svm  BoxConstraint: 31.423          KernelScale: 0.16609          Epsilon: 14.619   44  Accept  4.0639  0.040411  2.1945  2.1966  svm  BoxConstraint: 42.958          KernelScale: 459.03          Epsilon: 0.98679   45  Accept  2.5392  0.037327  2.1945  2.1966  svm  BoxConstraint: 32.844          KernelScale: 24.244          Epsilon: 2.8938   46  Accept  4.1222  0.039989  2.1945  2.1966  svm  BoxConstraint: 0.001348          KernelScale: 5.1158          Epsilon: 2.4534   47  Best  2.1523  0.054429  2.1523  2.1525  svm  BoxConstraint: 145.8          KernelScale: 6.9661          Epsilon: 1.692   48  Best  2.0991  0.043964  2.0991  2.0991  svm  BoxConstraint: 11.118          KernelScale: 4.5614          Epsilon: 0.6909   49  Accept  2.1321  0.048682  2.0991  2.0991  svm  BoxConstraint: 12.625          KernelScale: 3.7951          Epsilon: 1.9243   50  Accept  2.1155  0.065734  2.0991  2.0988  svm  BoxConstraint: 2.9811          KernelScale: 2.2304          Epsilon: 0.11742   51  Accept  2.1127  0.061163  2.0991  2.0991  svm  BoxConstraint: 4.6122          KernelScale: 2.5002          Epsilon: 0.24122   52  Accept  2.3111  2.183  2.0991  2.099  svm  BoxConstraint: 13.002          KernelScale: 1.2194          Epsilon: 0.0097793   53  Accept  2.1329  0.060156  2.0991  2.0987  svm  BoxConstraint: 81.429          KernelScale: 5.9963          Epsilon: 0.040814   54  Accept  2.1395  0.046864  2.0991  2.0991  svm  BoxConstraint: 43.058          KernelScale: 8.8124          Epsilon: 0.31232   55  Accept  2.1268  0.070321  2.0991  2.0993  svm  BoxConstraint: 18.909          KernelScale: 3.2821          Epsilon: 0.049352   56  Accept  2.2052  5.7654  2.0991  2.0993  ensemble  Method: LSBoost          NumLearningCycles: 299          MinLeafSize: 1   57  Accept  2.1528  0.15163  2.0991  2.0994  svm  BoxConstraint: 930.08          KernelScale: 7.7684          Epsilon: 0.15953   58  Accept  2.1402  0.075441  2.0991  2.0998  svm  BoxConstraint: 90.458          KernelScale: 4.8465          Epsilon: 0.16486   59  Accept  2.169  0.33792  2.0991  2.0998  svm  BoxConstraint: 838.56          KernelScale: 4.9126          Epsilon: 0.010297   60  Accept  2.1441  0.1108  2.0991  2.1  svm  BoxConstraint: 977.44          KernelScale: 9.5442          Epsilon: 0.0096251  ================================================================================================================================================  Iter  Eval  log(1+valLoss) Time for training  Observed min  Estimated min  Learner  Hyperparameter: Value    result   & validation (sec) validation loss  validation loss    ================================================================================================================================================  61  Accept  2.3548  3.0057  2.0991  2.1  ensemble  Method: Bag          NumLearningCycles: 201          MinLeafSize: 1   62  Accept  2.134  0.051302  2.0991  2.1  svm  BoxConstraint: 7.3349          KernelScale: 4.5654          Epsilon: 0.010146   63  Accept  2.1198  0.042744  2.0991  2.1015  svm  BoxConstraint: 9.5959          KernelScale: 4.8771          Epsilon: 0.19667   64  Accept  2.1389  0.058507  2.0991  2.1014  svm  BoxConstraint: 948.83          KernelScale: 16.462          Epsilon: 0.47562   65  Accept  2.1122  0.062182  2.0991  2.1017  svm  BoxConstraint: 1.8061          KernelScale: 2.0447          Epsilon: 0.010408   66  Accept  2.1239  0.053398  2.0991  2.102  svm  BoxConstraint: 3.1375          KernelScale: 2.5109          Epsilon: 0.012724   67  Accept  2.1168  0.051692  2.0991  2.104  svm  BoxConstraint: 15.336          KernelScale: 3.8639          Epsilon: 0.35151   68  Accept  2.1262  0.060441  2.0991  2.1042  svm  BoxConstraint: 619.86          KernelScale: 12.746          Epsilon: 0.12944   69  Accept  2.1288  0.041367  2.0991  2.1087  svm  BoxConstraint: 14.346          KernelScale: 5.6377          Epsilon: 1.1115   70  Accept  2.1075  0.046906  2.0991  2.1073  svm  BoxConstraint: 6.3065          KernelScale: 3.3652          Epsilon: 0.37711   71  Accept  2.1122  0.058036  2.0991  2.1071  svm  BoxConstraint: 0.21669          KernelScale: 1.3771          Epsilon: 0.010668   72  Accept  2.1086  0.057236  2.0991  2.1072  svm  BoxConstraint: 0.6199          KernelScale: 1.6673          Epsilon: 0.02609   73  Accept  2.2008  0.20609  2.0991  2.107  svm  BoxConstraint: 0.74478          KernelScale: 1.1364          Epsilon: 0.0097464   74  Accept  2.1274  0.045525  2.0991  2.1075  svm  BoxConstraint: 0.24664          KernelScale: 1.9581          Epsilon: 0.0095196   75  Accept  2.1194  0.062398  2.0991  2.1076  svm  BoxConstraint: 0.84673          KernelScale: 1.6712          Epsilon: 0.38722   76  Accept  2.339  3.0245  2.0991  2.1076  ensemble  Method: Bag          NumLearningCycles: 204          MinLeafSize: 3   77  Accept  4.1303  0.045681  2.0991  2.1046  svm  BoxConstraint: 3.6938          KernelScale: 1.3489          Epsilon: 883.83   78  Accept  2.1418  0.063846  2.0991  2.105  svm  BoxConstraint: 53.966          KernelScale: 4.7398          Epsilon: 0.0098142   79  Accept  2.1226  0.068867  2.0991  2.1052  svm  BoxConstraint: 0.21684          KernelScale: 1.2724          Epsilon: 0.067508   80  Accept  2.1215  0.071022  2.0991  2.1054  svm  BoxConstraint: 0.89132          KernelScale: 1.6131          Epsilon: 0.080329  ================================================================================================================================================  Iter  Eval  log(1+valLoss) Time for training  Observed min  Estimated min  Learner  Hyperparameter: Value    result   & validation (sec) validation loss  validation loss    ================================================================================================================================================  81  Accept  2.1109  0.049506  2.0991  2.1055  svm  BoxConstraint: 1.8778          KernelScale: 2.3249          Epsilon: 0.40345   82  Accept  2.1894  0.047319  2.0991  2.1052  svm  BoxConstraint: 0.0080677          KernelScale: 0.98345          Epsilon: 0.0098795   83  Accept  2.1159  0.055132  2.0991  2.1053  svm  BoxConstraint: 0.097667          KernelScale: 1.4607          Epsilon: 0.029531   84  Accept  2.1106  0.047832  2.0991  2.1064  svm  BoxConstraint: 9.7081          KernelScale: 3.5404          Epsilon: 0.77633   85  Accept  2.1338  0.052769  2.0991  2.1065  svm  BoxConstraint: 328.02          KernelScale: 11.996          Epsilon: 0.63786   86  Accept  2.1017  0.046959  2.0991  2.1033  svm  BoxConstraint: 15.397          KernelScale: 4.4256          Epsilon: 0.7265   87  Accept  4.1303  0.036522  2.0991  2.1033  svm  BoxConstraint: 434.58          KernelScale: 0.0010827          Epsilon: 0.011393   88  Accept  2.0996  0.046978  2.0991  2.1017  svm  BoxConstraint: 24.978          KernelScale: 4.7899          Epsilon: 0.72324   89  Accept  2.1053  0.046511  2.0991  2.1025  svm  BoxConstraint: 18.777          KernelScale: 4.5251          Epsilon: 0.76878   90  Accept  2.1034  0.047849  2.0991  2.1026  svm  BoxConstraint: 19.098          KernelScale: 4.5336          Epsilon: 0.75174  __________________________________________________________ Optimization completed. Total iterations: 90 Total elapsed time: 136.2822 seconds Total time for training and validation: 111.4242 seconds Best observed learner is an svm model with: Learner: svm BoxConstraint: 11.118 KernelScale: 4.5614 Epsilon: 0.6909 Observed log(1 + valLoss): 2.0991 Time for training and validation: 0.043964 seconds Best estimated learner (returned model) is an svm model with: Learner: svm BoxConstraint: 15.397 KernelScale: 4.4256 Epsilon: 0.7265 Estimated log(1 + valLoss): 2.1026 Estimated time for training and validation: 0.046399 seconds Documentation for fitrauto display
The final model returned by fitrauto
corresponds to the best estimated learner. Before returning the model, the function retrains it using the entire training data (carsTrain
), the listed Learner
(or model) type, and the displayed hyperparameter values.
Evaluate Test Set Performance
Evaluate the performance of the model on the test set. testError
is based on the test set mean squared error (MSE). Smaller MSE values indicate better performance.
testMSE = loss(Mdl,carsTest,"MPG");
testError = log(1 + testMSE)
testError = 2.2000
Automatically Select Regression Model Using Matrix Data
Use fitrauto
to automatically select a regression model with optimized hyperparameters, given predictor and response data stored in separate variables.
Load Data
Load the carbig
data set, which contains measurements of cars made in the 1970s and early 1980s.
load carbig
Create a matrix X
containing the predictor variables Acceleration
, Cylinders
, and so on. Store the response variable MPG
in the variable Y
.
X = [Acceleration Cylinders Displacement Weight]; Y = MPG;
Delete rows of X
and Y
where either array has missing values.
R = rmmissing([X Y]); X = R(:,1:end1); Y = R(:,end);
Create a variable indicating which predictors are categorical. Cylinders
is the only categorical variable in X
.
categoricalVars = [false true false false];
Partition Data
Partition the data into training and test sets. Use approximately 80% of the observations for the model selection and hyperparameter tuning process, and 20% of the observations to test the performance of the final model returned by fitrauto
. Use cvpartition
to partition the data.
rng("default") % For reproducibility of the partition c = cvpartition(length(Y),"Holdout",0.20); trainingIdx = training(c); % Indices for the training set XTrain = X(trainingIdx,:); YTrain = Y(trainingIdx); testIdx = test(c); % Indices for the test set XTest = X(testIdx,:); YTest = Y(testIdx);
Run fitrauto
Pass the training data to fitrauto
. By default, fitrauto
determines appropriate model (or learner) types to try, uses Bayesian optimization to find good hyperparameter values for those models, and returns a trained model Mdl
with the best expected performance. Specify the categorical predictors, and run the optimization in parallel (requires Parallel Computing Toolbox™). Return a second output OptimizationResults
that contains the details of the Bayesian optimization.
Expect this process to take some time. By default, fitrauto
provides a plot of the optimization and an iterative display of the optimization results. For more information on how to interpret these results, see Verbose Display.
options = struct("UseParallel",true); [Mdl,OptimizationResults] = fitrauto(XTrain,YTrain, ... "CategoricalPredictors",categoricalVars, ... "HyperparameterOptimizationOptions",options);
Starting parallel pool (parpool) using the 'Processes' profile ... Connected to parallel pool with 8 workers. Copying objective function to workers... Done copying objective function to workers. Learner types to explore: ensemble, svm, tree Total iterations (MaxObjectiveEvaluations): 90 Total time (MaxTime): Inf ==========================================================================================================================================================  Iter  Active  Eval  log(1+valLoss) Time for training  Observed min  Estimated min  Learner  Hyperparameter: Value    workers  result   & validation (sec) validation loss  validation loss    ==========================================================================================================================================================  1  6  Best  3.1329  1.9788  3.1329  3.1329  tree  MinLeafSize: 5   2  6  Accept  3.1329  1.985  3.1329  3.1329  tree  MinLeafSize: 5   3  6  Accept  3.1539  1.9764  3.1329  3.1329  tree  MinLeafSize: 9   4  6  Accept  4.1701  2.1518  3.1329  3.1329  svm  BoxConstraint: 0.033502           KernelScale: 153.38           Epsilon: 0.095234   5  8  Accept  3.1684  0.72622  3.1329  3.147  tree  MinLeafSize: 4   6  8  Best  3.0322  0.20501  3.0322  3.0528  svm  BoxConstraint: 0.010812           KernelScale: 1.2015           Epsilon: 0.034779   7  8  Accept  3.2871  0.23632  3.0322  3.0528  tree  MinLeafSize: 2   8  8  Accept  4.1645  7.601  3.0322  3.0528  ensemble  Method: Bag           NumLearningCycles: 257           MinLeafSize: 154   9  8  Accept  3.2871  0.50364  3.0322  3.0528  tree  MinLeafSize: 2   10  8  Best  2.9469  10.828  2.9469  2.9628  ensemble  Method: LSBoost           NumLearningCycles: 287           MinLeafSize: 1   11  8  Best  2.9388  10.949  2.9388  2.9413  ensemble  Method: LSBoost           NumLearningCycles: 288           MinLeafSize: 3   12  8  Accept  2.9581  10.32  2.9388  2.9404  ensemble  Method: LSBoost           NumLearningCycles: 287           MinLeafSize: 62   13  7  Accept  2.9581  10.584  2.9388  2.9403  ensemble  Method: LSBoost           NumLearningCycles: 287           MinLeafSize: 62   14  7  Accept  2.9581  10.626  2.9388  2.9403  ensemble  Method: LSBoost           NumLearningCycles: 287           MinLeafSize: 62   15  8  Accept  3.6004  0.36596  2.9388  2.9403  tree  MinLeafSize: 120   16  8  Accept  3.6004  0.41888  2.9388  2.9403  tree  MinLeafSize: 120   17  8  Accept  3.1539  0.14494  2.9388  2.9403  tree  MinLeafSize: 9   18  7  Best  2.9287  11.356  2.9287  2.9403  ensemble  Method: LSBoost           NumLearningCycles: 283           MinLeafSize: 16   19  7  Accept  4.1645  1.9545  2.9287  2.9403  svm  BoxConstraint: 159.44           KernelScale: 34.732           Epsilon: 412.2   20  7  Accept  4.1862  0.1901  2.9287  2.9403  svm  BoxConstraint: 7.487           KernelScale: 81.753           Epsilon: 12.782  ==========================================================================================================================================================  Iter  Active  Eval  log(1+valLoss) Time for training  Observed min  Estimated min  Learner  Hyperparameter: Value    workers  result   & validation (sec) validation loss  validation loss    ==========================================================================================================================================================  21  7  Accept  4.1645  0.19246  2.9287  2.9403  svm  BoxConstraint: 2.1296           KernelScale: 3.2623           Epsilon: 609.61   22  6  Accept  3.9011  1.5706  2.9287  2.9403  svm  BoxConstraint: 9.4057           KernelScale: 100.66           Epsilon: 0.2386   23  6  Accept  3.1593  0.18031  2.9287  2.9403  tree  MinLeafSize: 8   24  7  Accept  2.9494  0.3265  2.9287  2.9403  svm  BoxConstraint: 746.54           KernelScale: 5.6788           Epsilon: 1.0177   25  7  Accept  2.9494  0.26551  2.9287  2.9403  svm  BoxConstraint: 746.54           KernelScale: 5.6788           Epsilon: 1.0177   26  7  Accept  9.5074  20.334  2.9287  2.9403  svm  BoxConstraint: 336.91           KernelScale: 0.0018275           Epsilon: 0.10919   27  7  Accept  2.9494  0.79583  2.9287  2.9403  svm  BoxConstraint: 746.54           KernelScale: 5.6788           Epsilon: 1.0177   28  6  Best  2.9287  11.352  2.9287  2.9403  ensemble  Method: LSBoost           NumLearningCycles: 262           MinLeafSize: 5   29  6  Accept  3.6021  0.45991  2.9287  2.9403  tree  MinLeafSize: 1   30  5  Accept  3.1697  8.5532  2.9287  2.9403  ensemble  Method: Bag           NumLearningCycles: 247           MinLeafSize: 74   31  5  Accept  3.1014  1.3678  2.9287  2.9403  tree  MinLeafSize: 13   32  8  Accept  2.9806  0.26062  2.9287  2.9403  svm  BoxConstraint: 5.2568           KernelScale: 10.811           Epsilon: 0.074584   33  8  Accept  4.165  0.13934  2.9287  2.9403  svm  BoxConstraint: 0.18243           KernelScale: 128.12           Epsilon: 5.0924   34  8  Accept  2.9351  11.473  2.9287  2.9288  ensemble  Method: LSBoost           NumLearningCycles: 282           MinLeafSize: 2   35  7  Accept  2.9423  5.5429  2.9287  2.9288  ensemble  Method: LSBoost           NumLearningCycles: 202           MinLeafSize: 50   36  7  Accept  3.4421  0.51292  2.9287  2.9288  tree  MinLeafSize: 76   37  8  Accept  2.9423  6.4536  2.9287  2.927  ensemble  Method: LSBoost           NumLearningCycles: 239           MinLeafSize: 50   38  4  Accept  3.1708  10.551  2.8874  2.8928  ensemble  Method: LSBoost           NumLearningCycles: 274           MinLeafSize: 106   39  4  Accept  2.9338  9.5391  2.8874  2.8928  ensemble  Method: Bag           NumLearningCycles: 280           MinLeafSize: 45   40  4  Accept  2.9351  6.3776  2.8874  2.8928  ensemble  Method: LSBoost           NumLearningCycles: 233           MinLeafSize: 2  ==========================================================================================================================================================  Iter  Active  Eval  log(1+valLoss) Time for training  Observed min  Estimated min  Learner  Hyperparameter: Value    workers  result   & validation (sec) validation loss  validation loss    ==========================================================================================================================================================  41  4  Best  2.8874  6.3082  2.8874  2.8928  ensemble  Method: Bag           NumLearningCycles: 271           MinLeafSize: 1   42  4  Accept  2.8981  7.7345  2.8874  2.8928  ensemble  Method: Bag           NumLearningCycles: 271           MinLeafSize: 1   43  8  Accept  3.2678  0.19997  2.8874  2.8928  tree  MinLeafSize: 65   44  5  Best  2.8796  4.3397  2.8796  2.8796  ensemble  Method: Bag           NumLearningCycles: 209           MinLeafSize: 4   45  5  Accept  2.8803  5.559  2.8796  2.8796  ensemble  Method: Bag           NumLearningCycles: 250           MinLeafSize: 10   46  5  Accept  3.2871  0.40833  2.8796  2.8796  tree  MinLeafSize: 2   47  5  Accept  2.9099  5.7828  2.8796  2.8796  ensemble  Method: LSBoost           NumLearningCycles: 213           MinLeafSize: 10   48  8  Accept  2.9351  7.025  2.8796  2.8796  ensemble  Method: LSBoost           NumLearningCycles: 265           MinLeafSize: 2   49  6  Accept  5.4316  20.005  2.8796  2.8796  svm  BoxConstraint: 28.808           KernelScale: 0.3219           Epsilon: 0.13762   50  6  Accept  3.1177  0.68844  2.8796  2.8796  svm  BoxConstraint: 127.48           KernelScale: 105           Epsilon: 0.58941   51  6  Accept  7.8524  0.68066  2.8796  2.8796  svm  BoxConstraint: 3.2128           KernelScale: 0.0015524           Epsilon: 0.11513   52  7  Accept  3.1329  0.068468  2.8796  2.8796  tree  MinLeafSize: 5   53  7  Accept  2.9597  0.088717  2.8796  2.8796  svm  BoxConstraint: 990.41           KernelScale: 29.533           Epsilon: 0.015011   54  8  Accept  2.9076  5.4054  2.8796  2.8796  ensemble  Method: LSBoost           NumLearningCycles: 248           MinLeafSize: 13   55  8  Accept  4.1645  0.07993  2.8796  2.8796  svm  BoxConstraint: 1.246           KernelScale: 0.048145           Epsilon: 337.42   56  4  Accept  4.8948  27.255  2.8796  2.8796  svm  BoxConstraint: 0.093799           KernelScale: 0.0053728           Epsilon: 17.621   57  4  Accept  2.9227  8.5858  2.8796  2.8796  ensemble  Method: LSBoost           NumLearningCycles: 360           MinLeafSize: 9   58  4  Accept  2.8934  5.1523  2.8796  2.8796  ensemble  Method: Bag           NumLearningCycles: 298           MinLeafSize: 2   59  4  Accept  2.8877  5.7141  2.8796  2.8796  ensemble  Method: Bag           NumLearningCycles: 299           MinLeafSize: 2   60  4  Accept  2.8982  5.5545  2.8796  2.8796  ensemble  Method: Bag           NumLearningCycles: 320           MinLeafSize: 1  ==========================================================================================================================================================  Iter  Active  Eval  log(1+valLoss) Time for training  Observed min  Estimated min  Learner  Hyperparameter: Value    workers  result   & validation (sec) validation loss  validation loss    ==========================================================================================================================================================  61  8  Accept  3.0945  0.065636  2.8796  2.8796  tree  MinLeafSize: 11   62  6  Accept  2.8818  5.1985  2.8796  2.8796  ensemble  Method: Bag           NumLearningCycles: 298           MinLeafSize: 2   63  6  Accept  4.1645  0.17235  2.8796  2.8796  svm  BoxConstraint: 0.0010273           KernelScale: 0.24074           Epsilon: 280.24   64  6  Accept  4.1645  0.069883  2.8796  2.8796  svm  BoxConstraint: 2.4541           KernelScale: 5.0059           Epsilon: 29.021   65  6  Best  2.8788  3.2047  2.8788  2.8785  ensemble  Method: Bag           NumLearningCycles: 223           MinLeafSize: 5   66  6  Accept  3.1966  4.5298  2.8788  2.8785  ensemble  Method: LSBoost           NumLearningCycles: 243           MinLeafSize: 109   67  6  Accept  4.2278  0.075829  2.8788  2.8785  svm  BoxConstraint: 0.49855           KernelScale: 822.57           Epsilon: 13.604   68  7  Accept  3.4183  0.040748  2.8788  2.8785  tree  MinLeafSize: 93   69  7  Accept  2.9682  0.089978  2.8788  2.8785  svm  BoxConstraint: 990.82           KernelScale: 32.258           Epsilon: 0.65013   70  7  Accept  17.606  16.61  2.8788  2.8785  svm  BoxConstraint: 244.81           KernelScale: 0.029911           Epsilon: 1.3663   71  7  Accept  3.2871  0.12707  2.8788  2.8785  tree  MinLeafSize: 2   72  6  Accept  2.8957  2.8051  2.8788  2.8785  ensemble  Method: Bag           NumLearningCycles: 203           MinLeafSize: 21   73  6  Accept  4.1645  0.049095  2.8788  2.8785  svm  BoxConstraint: 0.0023954           KernelScale: 0.0017894           Epsilon: 779.82   74  7  Accept  4.1645  0.073002  2.8788  2.8785  svm  BoxConstraint: 0.0022454           KernelScale: 1.2218           Epsilon: 1223.3   75  6  Accept  3.0324  7.4797  2.8788  2.878  ensemble  Method: LSBoost           NumLearningCycles: 386           MinLeafSize: 74   76  6  Accept  2.8964  3.1304  2.8788  2.878  ensemble  Method: Bag           NumLearningCycles: 222           MinLeafSize: 21   77  6  Accept  3.1637  0.054723  2.8788  2.878  tree  MinLeafSize: 6   78  5  Accept  2.8986  2.8386  2.8788  2.878  ensemble  Method: Bag           NumLearningCycles: 204           MinLeafSize: 21   79  5  Accept  4.1645  0.04896  2.8788  2.878  svm  BoxConstraint: 0.0012664           KernelScale: 0.0010838           Epsilon: 54.909   80  7  Accept  16.812  11.246  2.8788  2.8778  svm  BoxConstraint: 0.071974           KernelScale: 0.030156           Epsilon: 1.0328  ==========================================================================================================================================================  Iter  Active  Eval  log(1+valLoss) Time for training  Observed min  Estimated min  Learner  Hyperparameter: Value    workers  result   & validation (sec) validation loss  validation loss    ==========================================================================================================================================================  81  7  Accept  2.9388  7.0653  2.8788  2.8778  ensemble  Method: LSBoost           NumLearningCycles: 375           MinLeafSize: 3   82  4  Accept  2.9194  0.056852  2.8788  2.8778  svm  BoxConstraint: 0.49279           KernelScale: 1.837           Epsilon: 0.028024   83  4  Accept  3.6021  0.073288  2.8788  2.8778  tree  MinLeafSize: 1   84  4  Accept  4.1645  0.043986  2.8788  2.8778  svm  BoxConstraint: 0.0010569           KernelScale: 0.05901           Epsilon: 154.61   85  4  Accept  4.0662  0.051015  2.8788  2.8778  svm  BoxConstraint: 0.65745           KernelScale: 42.989           Epsilon: 0.018275   86  8  Accept  2.8973  2.9559  2.8788  2.8778  ensemble  Method: Bag           NumLearningCycles: 200           MinLeafSize: 21   87  6  Accept  3.1593  0.030722  2.8788  2.8778  tree  MinLeafSize: 8   88  6  Accept  4.1645  0.1004  2.8788  2.8778  svm  BoxConstraint: 0.026752           KernelScale: 0.0090205           Epsilon: 234.51   89  6  Accept  4.1645  0.085556  2.8788  2.8778  svm  BoxConstraint: 701.89           KernelScale: 0.014432           Epsilon: 823.78   90  7  Accept  3.0786  0.058939  2.8788  2.8778  tree  MinLeafSize: 12 
__________________________________________________________ Optimization completed. Total iterations: 90 Total elapsed time: 70.4919 seconds Total time for training and validation: 366.4178 seconds Best observed learner is an ensemble model with: Learner: ensemble Method: Bag NumLearningCycles: 223 MinLeafSize: 5 Observed log(1 + valLoss): 2.8788 Time for training and validation: 3.2047 seconds Best estimated learner (returned model) is an ensemble model with: Learner: ensemble Method: Bag NumLearningCycles: 223 MinLeafSize: 5 Estimated log(1 + valLoss): 2.8778 Estimated time for training and validation: 3.6185 seconds Documentation for fitrauto display
The final model returned by fitrauto
corresponds to the best estimated learner. Before returning the model, the function retrains it using the entire training data (XTrain
and YTrain
), the listed Learner
(or model) type, and the displayed hyperparameter values.
Evaluate Test Set Performance
Evaluate the performance of the model on the test set. testError
is based on the test set mean squared error (MSE). Smaller MSE values indicate better performance.
testMSE = loss(Mdl,XTest,YTest); testError = log(1 + testMSE)
testError = 2.6223
Compare Optimized and Simple Linear Regression Model
Use fitrauto
to automatically select a regression model with optimized hyperparameters, given predictor and response data stored in a table. Compare the performance of the resulting regression model to the performance of a simple linear regression model created with fitlm
.
Load and Partition Data
Load the carbig
data set, which contains measurements of cars made in the 1970s and early 1980s. Convert the Cylinders
variable to a categorical
variable. Create a table containing the predictor variables Acceleration
, Cylinders
, Displacement
, and so on, as well as the response variable MPG
.
load carbig Cylinders = categorical(Cylinders); cars = table(Acceleration,Cylinders,Displacement, ... Horsepower,Model_Year,Origin,Weight,MPG);
Delete rows of cars
where the table has missing values.
cars = rmmissing(cars);
Categorize the cars based on whether they were made in the USA.
cars.Origin = categorical(cellstr(cars.Origin)); cars.Origin = mergecats(cars.Origin,["France","Japan",... "Germany","Sweden","Italy","England"],"NotUSA");
Partition the data into training and test sets. Use approximately 80% of the observations for training, and 20% of the observations for testing. Use cvpartition
to partition the data.
rng("default") % For reproducibility of the data partition c = cvpartition(height(cars),"Holdout",0.2); trainingIdx = training(c); % Training set indices carsTrain = cars(trainingIdx,:); testIdx = test(c); % Test set indices carsTest = cars(testIdx,:);
Run fitrauto
Pass the training data to fitrauto
. By default, fitrauto
determines appropriate model types to try, uses Bayesian optimization to find good hyperparameter values, and returns a trained model autoMdl
with the best expected performance. Specify to optimize over all optimizable hyperparameters and run the optimization in parallel (requires Parallel Computing Toolbox™).
Expect this process to take some time. By default, fitrauto
provides a plot of the optimization and an iterative display of the optimization results. For more information on how to interpret these results, see Verbose Display.
options = struct("UseParallel",true); autoMdl = fitrauto(carsTrain,"MPG","OptimizeHyperparameters","all", ... "HyperparameterOptimizationOptions",options);
Copying objective function to workers... Done copying objective function to workers. Learner types to explore: ensemble, svm, tree Total iterations (MaxObjectiveEvaluations): 90 Total time (MaxTime): Inf ==========================================================================================================================================================  Iter  Active  Eval  log(1+valLoss) Time for training  Observed min  Estimated min  Learner  Hyperparameter: Value    workers  result   & validation (sec) validation loss  validation loss    ==========================================================================================================================================================  1  8  Best  4.1338  0.99198  4.1338  4.1338  svm  BoxConstraint: 0.095204           KernelScale: 63.457           Epsilon: 0.055423   2  6  Best  3.2109  1.5627  3.2109  3.9013  tree  MinLeafSize: 5           MaxNumSplits: 2           NumVariablesToSample: 3   3  6  Accept  4.1303  1.2106  3.2109  3.9013  svm  BoxConstraint: 0.0010671           KernelScale: 19.242           Epsilon: 44.847   4  6  Accept  4.1303  1.1875  3.2109  3.9013  svm  BoxConstraint: 0.73976           KernelScale: 2.7037           Epsilon: 38.421   5  7  Best  2.5852  0.55713  2.5852  2.722  tree  MinLeafSize: 2           MaxNumSplits: 120           NumVariablesToSample: 7   6  7  Accept  2.5852  0.34923  2.5852  2.5853  tree  MinLeafSize: 2           MaxNumSplits: 120           NumVariablesToSample: 7   7  8  Accept  4.5891  11.91  2.5852  2.5853  ensemble  Method: LSBoost           LearnRate: 0.0051188           MinLeafSize: 83           NumVariablesToSample: NaN   8  8  Accept  4.7998  14.032  2.5852  2.5853  ensemble  Method: LSBoost           LearnRate: 0.0042702           MinLeafSize: 31           NumVariablesToSample: NaN   9  8  Accept  2.6854  0.52668  2.5852  2.5853  tree  MinLeafSize: 5           MaxNumSplits: 61           NumVariablesToSample: 2   10  8  Accept  4.6825  17.644  2.5852  2.5853  ensemble  Method: LSBoost           LearnRate: 0.0045559           MinLeafSize: 1           NumVariablesToSample: NaN   11  8  Accept  2.6451  10.76  2.5852  2.5853  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 45           NumVariablesToSample: 6   12  8  Best  2.2978  14.905  2.2978  2.3021  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 2           NumVariablesToSample: 4   13  8  Accept  4.1303  1.228  2.2978  2.3021  svm  BoxConstraint: 27.717           KernelScale: 21.172           Epsilon: 390.93   14  8  Accept  2.8315  1.9272  2.2978  2.3021  svm  BoxConstraint: 0.038615           KernelScale: 0.26266           Epsilon: 5.8807   15  8  Accept  5.749  15.724  2.2978  2.2981  ensemble  Method: LSBoost           LearnRate: 0.0017397           MinLeafSize: 22           NumVariablesToSample: NaN   16  8  Accept  3.1368  0.83814  2.2978  2.2981  tree  MinLeafSize: 2           MaxNumSplits: 2           NumVariablesToSample: 5   17  8  Accept  5.749  17.391  2.2978  2.2982  ensemble  Method: LSBoost           LearnRate: 0.0017397           MinLeafSize: 22           NumVariablesToSample: NaN   18  8  Accept  2.3324  15.338  2.2978  2.2991  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 1           NumVariablesToSample: 3  19  8  Accept  4.1551  14.857  2.2978  2.2991  ensemble  Method: LSBoost           LearnRate: 0.014547           MinLeafSize: 145           NumVariablesToSample: NaN   20  8  Accept  2.6846  0.84437  2.2978  2.2991  tree  MinLeafSize: 1           MaxNumSplits: 248           NumVariablesToSample: 7  ==========================================================================================================================================================  Iter  Active  Eval  log(1+valLoss) Time for training  Observed min  Estimated min  Learner  Hyperparameter: Value    workers  result   & validation (sec) validation loss  validation loss    ==========================================================================================================================================================  21  8  Accept  4.1303  0.78252  2.2978  2.2991  svm  BoxConstraint: 117.04           KernelScale: 629.41           Epsilon: 729.47   22  8  Accept  2.9566  0.27604  2.2978  2.2991  tree  MinLeafSize: 30           MaxNumSplits: 21           NumVariablesToSample: 2   23  8  Accept  2.9315  0.25031  2.2978  2.2991  tree  MinLeafSize: 2           MaxNumSplits: 4           NumVariablesToSample: 4   24  8  Accept  2.3714  14.771  2.2978  2.3268  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 26           NumVariablesToSample: 6   25  8  Accept  2.483  0.22694  2.2978  2.3268  tree  MinLeafSize: 9           MaxNumSplits: 157           NumVariablesToSample: 6   26  8  Accept  3.1597  11.818  2.2978  2.2963  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 116           NumVariablesToSample: 6   27  7  Accept  2.5191  13.747  2.2978  2.2963  ensemble  Method: LSBoost           LearnRate: 0.9913           MinLeafSize: 4           NumVariablesToSample: NaN   28  7  Accept  2.6931  0.42871  2.2978  2.2963  svm  BoxConstraint: 8.3226           KernelScale: 22.717           Epsilon: 3.2417   29  6  Accept  2.5091  12.311  2.2978  2.2963  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 29           NumVariablesToSample: 3   30  6  Accept  4.1303  0.059515  2.2978  2.2963  svm  BoxConstraint: 0.0011506           KernelScale: 352.85           Epsilon: 163.49   31  6  Accept  3.3629  0.24098  2.2978  2.2963  tree  MinLeafSize: 72           MaxNumSplits: 251           NumVariablesToSample: 2  32  6  Accept  2.7559  0.24436  2.2978  2.2963  tree  MinLeafSize: 4           MaxNumSplits: 5           NumVariablesToSample: 6   33  7  Accept  4.1448  0.46872  2.2978  2.2963  svm  BoxConstraint: 1.8227           KernelScale: 977.3           Epsilon: 2.0809   34  7  Accept  4.1448  0.16547  2.2978  2.2963  svm  BoxConstraint: 1.8227           KernelScale: 977.3           Epsilon: 2.0809   35  6  Accept  2.5225  11.088  2.2978  2.1683  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 11           NumVariablesToSample: 1   36  6  Accept  4.1448  0.17587  2.2978  2.1683  svm  BoxConstraint: 1.8227           KernelScale: 977.3           Epsilon: 2.0809   37  6  Accept  2.3265  13.798  2.2978  2.297  ensemble  Method: LSBoost           LearnRate: 0.11482           MinLeafSize: 54           NumVariablesToSample: NaN   38  6  Accept  2.6212  0.33117  2.2978  2.297  tree  MinLeafSize: 2           MaxNumSplits: 257           NumVariablesToSample: 4   39  7  Accept  2.4301  0.15505  2.2978  2.297  tree  MinLeafSize: 7           MaxNumSplits: 16           NumVariablesToSample: 7   40  7  Accept  4.9666  15.768  2.2978  2.2967  ensemble  Method: LSBoost           LearnRate: 0.0037902           MinLeafSize: 14           NumVariablesToSample: NaN  ==========================================================================================================================================================  Iter  Active  Eval  log(1+valLoss) Time for training  Observed min  Estimated min  Learner  Hyperparameter: Value    workers  result   & validation (sec) validation loss  validation loss    ==========================================================================================================================================================  41  7  Accept  4.1303  11.764  2.2978  2.2969  ensemble  Method: LSBoost           LearnRate: 0.3879           MinLeafSize: 130           NumVariablesToSample: NaN   42  6  Accept  3.5395  11.506  2.2978  2.2969  ensemble  Method: LSBoost           LearnRate: 0.37414           MinLeafSize: 125           NumVariablesToSample: NaN   43  6  Accept  4.1303  0.40658  2.2978  2.2969  svm  BoxConstraint: 188.58           KernelScale: 0.009867           Epsilon: 27.186   44  7  Accept  2.3321  15.446  2.2978  2.2969  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 22           NumVariablesToSample: 6   45  7  Accept  4.1303  0.41087  2.2978  2.2969  svm  BoxConstraint: 216.26           KernelScale: 0.0026892           Epsilon: 19.364   46  8  Best  2.1813  16.513  2.1813  2.2049  ensemble  Method: LSBoost           LearnRate: 0.087793           MinLeafSize: 3           NumVariablesToSample: NaN   47  7  Accept  2.3844  14.01  2.1706  2.1777  ensemble  Method: LSBoost           LearnRate: 0.82262           MinLeafSize: 5           NumVariablesToSample: NaN   48  7  Best  2.1706  13.587  2.1706  2.1777  ensemble  Method: LSBoost           LearnRate: 0.088258           MinLeafSize: 3           NumVariablesToSample: NaN   49  7  Accept  2.9982  0.070188  2.1706  2.1777  tree  MinLeafSize: 17           MaxNumSplits: 5           NumVariablesToSample: 1   50  7  Accept  16.058  74.473  2.1706  2.1777  svm  BoxConstraint: 0.87942           KernelScale: 0.042698           Epsilon: 4.1252   51  6  Accept  2.1767  12.611  2.1706  2.1777  ensemble  Method: LSBoost           LearnRate: 0.0879           MinLeafSize: 3           NumVariablesToSample: NaN   52  6  Accept  2.2905  0.30143  2.1706  2.1777  svm  BoxConstraint: 0.43806           KernelScale: 4.5798           Epsilon: 0.054809   53  7  Accept  2.9896  0.11995  2.1706  2.1777  tree  MinLeafSize: 45           MaxNumSplits: 13           NumVariablesToSample: 4   54  6  Accept  5.2259  107.07  2.1706  2.178  svm  BoxConstraint: 0.95481           KernelScale: 0.0033698           Epsilon: 0.060804   55  6  Accept  2.272  13.584  2.1706  2.178  ensemble  Method: LSBoost           LearnRate: 0.27481           MinLeafSize: 1           NumVariablesToSample: NaN   56  4  Accept  5.803  68.125  2.1706  2.178  svm  BoxConstraint: 154.74           KernelScale: 0.43817           Epsilon: 0.069304   57  4  Accept  2.2597  15.707  2.1706  2.178  ensemble  Method: LSBoost           LearnRate: 0.27582           MinLeafSize: 1           NumVariablesToSample: NaN   58  4  Accept  4.1303  0.23007  2.1706  2.178  svm  BoxConstraint: 0.0077264           KernelScale: 11.935           Epsilon: 22.619   59  8  Accept  2.249  14.154  2.1706  2.1774  ensemble  Method: LSBoost           LearnRate: 0.27746           MinLeafSize: 1           NumVariablesToSample: NaN   60  4  Accept  4.1489  0.40814  2.1706  2.1774  svm  BoxConstraint: 0.0029753           KernelScale: 357.77           Epsilon: 1.3007  ==========================================================================================================================================================  Iter  Active  Eval  log(1+valLoss) Time for training  Observed min  Estimated min  Learner  Hyperparameter: Value    workers  result   & validation (sec) validation loss  validation loss    ==========================================================================================================================================================  61  4  Accept  4.1442  0.1383  2.1706  2.1774  svm  BoxConstraint: 0.29076           KernelScale: 499.42           Epsilon: 0.055935   62  4  Accept  4.1303  0.38562  2.1706  2.1774  svm  BoxConstraint: 0.057299           KernelScale: 55.001           Epsilon: 715.72   63  4  Accept  4.1303  0.25559  2.1706  2.1774  svm  BoxConstraint: 0.033314           KernelScale: 26.965           Epsilon: 44.582   64  4  Accept  2.5918  1.0499  2.1706  2.1774  svm  BoxConstraint: 0.037494           KernelScale: 2.7803           Epsilon: 0.17202   65  8  Accept  2.1966  11.397  2.1706  2.1792  ensemble  Method: LSBoost           LearnRate: 0.14121           MinLeafSize: 6           NumVariablesToSample: NaN   66  6  Accept  4.1303  0.15176  2.1706  2.1792  svm  BoxConstraint: 283.72           KernelScale: 0.66147           Epsilon: 1118.1   67  6  Accept  2.491  0.099708  2.1706  2.1792  tree  MinLeafSize: 10           MaxNumSplits: 228           NumVariablesToSample: 4   68  6  Accept  4.1303  0.10834  2.1706  2.1792  svm  BoxConstraint: 0.04198           KernelScale: 0.046234           Epsilon: 228.11   69  4  Accept  6.5341  58.263  2.1706  2.1792  svm  BoxConstraint: 40.96           KernelScale: 0.27094           Epsilon: 0.0092332   70  4  Accept  2.1832  13.19  2.1706  2.1792  ensemble  Method: LSBoost           LearnRate: 0.14232           MinLeafSize: 7           NumVariablesToSample: NaN   71  4  Accept  4.1303  0.37907  2.1706  2.1792  svm  BoxConstraint: 0.0026166           KernelScale: 29.227           Epsilon: 33.727   72  8  Accept  2.2519  11.02  2.1706  2.1357  ensemble  Method: LSBoost           LearnRate: 0.26185           MinLeafSize: 31           NumVariablesToSample: NaN   73  4  Accept  6.0271  16.021  2.1706  2.1357  ensemble  Method: LSBoost           LearnRate: 0.001014           MinLeafSize: 1           NumVariablesToSample: NaN   74  4  Accept  4.1303  0.39257  2.1706  2.1357  tree  MinLeafSize: 140           MaxNumSplits: 5           NumVariablesToSample: 2   75  4  Accept  3.3098  1.0689  2.1706  2.1357  tree  MinLeafSize: 71           MaxNumSplits: 234           NumVariablesToSample: 3   76  4  Accept  2.4236  0.24878  2.1706  2.1357  tree  MinLeafSize: 3           MaxNumSplits: 32           NumVariablesToSample: 6   77  4  Accept  2.1986  0.7499  2.1706  2.1357  svm  BoxConstraint: 11.863           KernelScale: 2.391           Epsilon: 0.028609   78  8  Accept  4.1303  0.17619  2.1706  2.1357  svm  BoxConstraint: 0.23675           KernelScale: 6.2812           Epsilon: 65.527   79  4  Accept  2.3907  11.733  2.1706  2.1357  ensemble  Method: LSBoost           LearnRate: 0.67002           MinLeafSize: 1           NumVariablesToSample: NaN   80  4  Accept  2.34  8.9321  2.1706  2.1357  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 15           NumVariablesToSample: 3  ==========================================================================================================================================================  Iter  Active  Eval  log(1+valLoss) Time for training  Observed min  Estimated min  Learner  Hyperparameter: Value    workers  result   & validation (sec) validation loss  validation loss    ==========================================================================================================================================================  81  4  Accept  2.2775  0.71031  2.1706  2.1357  svm  BoxConstraint: 0.063           KernelScale: 0.76611           Epsilon: 0.132   82  4  Accept  4.1303  0.13386  2.1706  2.1357  svm  BoxConstraint: 460.15           KernelScale: 467.91           Epsilon: 161.25   83  4  Accept  4.1303  0.45425  2.1706  2.1357  svm  BoxConstraint: 722.96           KernelScale: 1.0362           Epsilon: 57.277   84  8  Accept  2.1988  10.152  2.1706  2.136  ensemble  Method: LSBoost           LearnRate: 0.051529           MinLeafSize: 1           NumVariablesToSample: NaN   85  4  Accept  2.3265  10.946  2.1706  2.136  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 3           NumVariablesToSample: 3   86  4  Accept  2.6789  0.18857  2.1706  2.136  tree  MinLeafSize: 2           MaxNumSplits: 41           NumVariablesToSample: 5   87  4  Accept  4.1303  0.30453  2.1706  2.136  svm  BoxConstraint: 0.0015491           KernelScale: 749.22           Epsilon: 390.38   88  4  Accept  4.1303  0.18132  2.1706  2.136  svm  BoxConstraint: 0.86801           KernelScale: 27.78           Epsilon: 595.47   89  4  Accept  4.1303  0.2073  2.1706  2.136  svm  BoxConstraint: 85.305           KernelScale: 0.029663           Epsilon: 74.342   90  8  Accept  2.2299  10.96  2.1706  2.1402  ensemble  Method: LSBoost           LearnRate: 0.04273           MinLeafSize: 1           NumVariablesToSample: NaN  __________________________________________________________ Optimization completed. Total iterations: 90 Total elapsed time: 176.3064 seconds Total time for training and validation: 801.3902 seconds
Best observed learner is an ensemble model with: Learner: ensemble Method: LSBoost LearnRate: 0.088258 MinLeafSize: 3 NumVariablesToSample: NaN Observed log(1 + valLoss): 2.1706 Time for training and validation: 13.5866 seconds Best estimated learner (returned model) is an ensemble model with: Learner: ensemble Method: LSBoost LearnRate: 0.14232 MinLeafSize: 7 NumVariablesToSample: NaN Estimated log(1 + valLoss): 2.1402 Estimated time for training and validation: 13.2266 seconds Documentation for fitrauto display
The final model returned by fitrauto
corresponds to the best estimated learner. Before returning the model, the function retrains it using the entire training data (carsTrain
), the listed Learner
(or model) type, and the displayed hyperparameter values.
Create Simple Model
Create a simple linear regression model linearMdl
by using the fitlm
function.
linearMdl = fitlm(carsTrain);
Although the linearMdl
object does not have the exact same properties and methods as the autoMdl
object, you can use both models to predict response values for new data by using the predict
object function.
Compare Test Set Performance of Models
Compare the performance of the linearMdl
and autoMdl
models on the test data set. For each model, compute the test set mean squared error (MSE). Smaller MSE values indicate better performance.
ypred = predict(linearMdl,carsTest);
linearMSE = mean((carsTest.MPGypred).^2,"omitnan")
linearMSE = 10.0558
autoMSE = loss(autoMdl,carsTest,"MPG")
autoMSE = 7.4671
The autoMdl
model seems to outperform the linearMdl
model.
Input Arguments
Tbl
— Sample data
table
Sample data, specified as a table. Each row of Tbl
corresponds to one observation, and each column corresponds to one predictor. Optionally, Tbl
can contain one additional column for the response variable. Multicolumn variables and cell arrays other than cell arrays of character vectors are not accepted.
If Tbl
contains the response variable, and you want to use all remaining
variables in Tbl
as predictors, specify the response variable using
ResponseVarName
.
If Tbl
contains the response variable, and you want to use only a subset of the remaining variables in Tbl
as predictors, specify a formula using formula
.
If Tbl
does not contain the response variable, specify a response variable using Y
. The length of the response variable and the number of rows in Tbl
must be equal.
Data Types: table
ResponseVarName
— Response variable name
name of variable in Tbl
Response variable name, specified as the name of a variable in
Tbl
. The response variable must be a numeric vector.
You must specify ResponseVarName
as a character vector or string
scalar. For example, if the response variable Y
is stored as
Tbl.Y
, then specify it as "Y"
. Otherwise, the
software treats all columns of Tbl
, including Y
,
as predictors when training a model.
Data Types: char
 string
formula
— Explanatory model of response variable and subset of predictor variables
character vector  string scalar
Explanatory model of the response variable and a subset of the predictor variables,
specified as a character vector or string scalar in the form
"Y~x1+x2+x3"
. In this form, Y
represents the
response variable, and x1
, x2
, and
x3
represent the predictor variables.
To specify a subset of variables in Tbl
as predictors for
training the model, use a formula. If you specify a formula, then the software does not
use any variables in Tbl
that do not appear in
formula
.
The variable names in the formula must be both variable names in Tbl
(Tbl.Properties.VariableNames
) and valid MATLAB^{®} identifiers. You can verify the variable names in Tbl
by
using the isvarname
function. If the variable names
are not valid, then you can convert them by using the matlab.lang.makeValidName
function.
Data Types: char
 string
Y
— Response data
numeric vector
Response data, specified as a numeric vector. The length of Y
must be equal to the number of rows in Tbl
or
X
.
To specify the response variable name, use the ResponseName
namevalue argument.
Data Types: single
 double
X
— Predictor data
numeric matrix
Predictor data, specified as a numeric matrix.
Each row of X
corresponds to one observation, and each column corresponds to one predictor.
The length of Y
and the number of rows in X
must be equal.
To specify the names of the predictors in the order of their appearance in
X
, use the PredictorNames
namevalue
argument.
Data Types: single
 double
Note
The software treats NaN
, empty character vector
(''
), empty string (""
),
<missing>
, and <undefined>
elements as
missing data. The software removes rows of data corresponding to missing values in the
response variable. However, the treatment of missing values in the predictor data
X
or Tbl
varies among models (or
learners).
NameValue Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Namevalue arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: "HyperparameterOptimizationOptions",struct("MaxObjectiveEvaluations",200,"Verbose",2)
specifies to run 200 iterations of the optimization process (that is, try 200 model
hyperparameter combinations), and to display information in the Command Window about the
next model hyperparameter combination to be evaluated.
Learners
— Types of regression models
"auto"
(default)  "all"
 "alllinear"
 "allnonlinear"
 one or more learner names
Types of regression models to try during the optimization, specified as a value in the first table below or one or more learner names in the second table. Specify multiple learner names as a string or cell array.
Value  Description 

"auto" 
Note To provide the best hyperparameter optimization experience, the automatic selection of learners behavior is subject to frequent changes. For a more consistent selection of learners across software releases, explicitly specify the models you want to include. 
"all"  fitrauto selects all possible learners. 
"alllinear"  fitrauto selects linear
("linear" ) learners. 
"allnonlinear"  fitrauto selects all nonlinear learners:
"ensemble" , "gp" ,
"kernel" , "net" ,
"svm" (with a Gaussian or polynomial kernel), and
"tree" . 
Note
For greater efficiency, fitrauto
does not select the following combinations of models when you specify one of the previous values.
"kernel"
and"svm"
(with a Gaussian kernel) —fitrauto
chooses the first when the predictor data has more than 11,000 observations, and the second otherwise."linear"
and"svm"
(with a linear kernel) —fitrauto
chooses the first.
Learner Name  Description 

"ensemble"  Ensemble regression model 
"gp"  Gaussian process regression model 
"kernel"  Kernel regression model 
"linear"  Linear regression model for highdimensional data 
"net"  Neural network regression model 
"svm"  Support vector machine regression model 
"tree"  Binary decision regression tree 
Example: "Learners","all"
Example: "Learners","ensemble"
Example: "Learners",["gp","svm"]
OptimizeHyperparameters
— Hyperparameters to optimize
"auto"
(default)  "all"
Hyperparameters to optimize, specified as "auto"
or
"all"
. The optimizable hyperparameters depend on the model (or
learner), as described in this table.
Learner Name  Hyperparameters for "auto"  Additional Hyperparameters for "all"  Notes 

"ensemble"  Method , NumLearningCycles , LearnRate , MinLeafSize  MaxNumSplits ,
NumVariablesToSample  When the ensemble For more information, including
hyperparameter search ranges, see 
"gp"  Sigma , Standardize  BasisFunction , KernelFunction , KernelScale (KernelParameters ) 
For more information, including hyperparameter search
ranges, see 
"kernel"  Epsilon , KernelScale , Lambda , Standardize  Learner , NumExpansionDimensions  For more information, including hyperparameter search ranges, see
OptimizeHyperparameters . Note that you cannot change
hyperparameter search ranges when you use
fitrauto . 
"linear"  Lambda , Learner  Regularization  For more information, including hyperparameter search ranges, see
OptimizeHyperparameters . Note that you cannot change
hyperparameter search ranges when you use
fitrauto . 
"net"  Activations , Lambda , LayerSizes , Standardize  LayerBiasesInitializer , LayerWeightsInitializer  For more information, including hyperparameter search ranges, see
OptimizeHyperparameters . Note that you cannot change
hyperparameter search ranges when you use
fitrauto . 
"svm"  BoxConstraint ,
Epsilon , KernelScale ,
Standardize  KernelFunction ,
PolynomialOrder  When the For more information, including hyperparameter search
ranges, see 
"tree"  MinLeafSize  MaxNumSplits  For more information, including hyperparameter search ranges, see
OptimizeHyperparameters . Note that you cannot change
hyperparameter search ranges when you use
fitrauto . 
Note
When Learners
is set to a value other than
"auto"
, the default values for the model hyperparameters not
being optimized match the default fit function values, unless otherwise indicated in
the table notes. When Learners
is set to
"auto"
, the optimized hyperparameter search ranges and
nonoptimized hyperparameter values can vary, depending on the characteristics of the
training data. For more information, see Automatic Selection of Learners.
Example: "OptimizeHyperparameters","all"
HyperparameterOptimizationOptions
— Options for optimization
structure
Options for the optimization, specified as a structure. All fields in the structure are optional.
Field Name  Values  Default 

Optimizer 
 "bayesopt" 
MaxObjectiveEvaluations  Maximum number of iterations (objective function evaluations), specified as a positive integer 

MaxTime  Time limit, specified as a positive real number. The time limit is
in seconds, as measured by  Inf 
ShowPlots  Logical value indicating whether to show a plot of the optimization
progress. If true , this field plots the observed minimum
validation loss against the iteration number. When you use Bayesian
optimization, the plot also shows the estimated minimum validation
loss.  true 
SaveIntermediateResults  Logical value indicating whether to save results. If
true , this field overwrites a workspace variable at each
iteration. The variable is a BayesianOptimization object named
BayesoptResults if you use Bayesian optimization, and a
table named ASHAResults if you use ASHA
optimization.  false 
Verbose  Display at the command line:
 1 
UseParallel  Logical value indicating whether to run the optimization in parallel, which requires Parallel Computing Toolbox™. Due to the nonreproducibility of parallel timing, parallel optimization does not necessarily yield reproducible results.  false 
Repartition  Logical value indicating whether to repartition the
crossvalidation at every iteration. If
 false 
MaxTrainingSetSize  Maximum number of observations in each training set for ASHA optimization, specified as a positive integer. This value matches the largest training set size. Note If you want to specify this value, the  Largest available training partition size

MinTrainingSetSize  Minimum number of observations in each training set for ASHA optimization, specified as a positive integer. This value is a lower bound for the smallest training set size. Note If you want to specify this value, the  100 
Specify only one of the following three options.  
CVPartition  cvpartition object, created by cvpartition  "Kfold",5 if you do not specify any
crossvalidation field 
Holdout  Scalar in the range (0,1) representing the holdout
fraction  
Kfold  Integer greater than 1 
Example: "HyperparameterOptimizationOptions",struct("UseParallel",true)
CategoricalPredictors
— Categorical predictors list
vector of positive integers  logical vector  character matrix  string array  cell array of character vectors  "all"
Categorical predictors list, specified as one of the values in this table.
Value  Description 

Vector of positive integers 
Each entry in the vector is an index value indicating that the corresponding predictor is
categorical. The index values are between 1 and If 
Logical vector 
A 
Character matrix  Each row of the matrix is the name of a predictor variable. The names must match the entries in PredictorNames . Pad the names with extra blanks so each row of the character matrix has the same length. 
String array or cell array of character vectors  Each element in the array is the name of a predictor variable. The names must match the entries in PredictorNames . 
"all"  All predictors are categorical. 
By default, if the predictor data is in a table (Tbl
),
fitrauto
assumes that a variable is categorical if it is a
logical vector, categorical vector, character array, string array, or cell array of
character vectors. However, learners that use decision trees assume that
mathematically ordered categorical vectors are continuous variables. If the predictor
data is a matrix (X
), fitrauto
assumes
that all predictors are continuous. To identify any other predictors as categorical
predictors, specify them by using the CategoricalPredictors
namevalue argument.
For more information on how fitting functions treat categorical predictors, see Automatic Creation of Dummy Variables.
Example: "CategoricalPredictors","all"
Data Types: single
 double
 logical
 char
 string
 cell
PredictorNames
— Predictor variable names
string array of unique names  cell array of unique character vectors
Predictor variable names, specified as a string array of unique names or cell array of unique
character vectors. The functionality of PredictorNames
depends on the
way you supply the training data.
If you supply
X
andY
, then you can usePredictorNames
to assign names to the predictor variables inX
.The order of the names in
PredictorNames
must correspond to the column order ofX
. That is,PredictorNames{1}
is the name ofX(:,1)
,PredictorNames{2}
is the name ofX(:,2)
, and so on. Also,size(X,2)
andnumel(PredictorNames)
must be equal.By default,
PredictorNames
is{'x1','x2',...}
.
If you supply
Tbl
, then you can usePredictorNames
to choose which predictor variables to use in training. That is,fitrauto
uses only the predictor variables inPredictorNames
and the response variable during training.PredictorNames
must be a subset ofTbl.Properties.VariableNames
and cannot include the name of the response variable.By default,
PredictorNames
contains the names of all predictor variables.A good practice is to specify the predictors for training using either
PredictorNames
orformula
, but not both.
Example: "PredictorNames",["SepalLength","SepalWidth","PetalLength","PetalWidth"]
Data Types: string
 cell
ResponseName
— Response variable name
"Y"
(default)  character vector  string scalar
Response variable name, specified as a character vector or string scalar.
If you supply
Y
, then you can useResponseName
to specify a name for the response variable.If you supply
ResponseVarName
orformula
, then you cannot useResponseName
.
Example: "ResponseName","response"
Data Types: char
 string
Weights
— Observation weights
positive numeric vector  name of variable in Tbl
Observation weights, specified as a positive numeric vector or the name of a
variable in Tbl
. The software weights each observation in
X
or Tbl
with the corresponding value in
Weights
. The length of Weights
must equal
the number of rows in X
or Tbl
.
If you specify the input data as a table Tbl
, then
Weights
can be the name of a variable in
Tbl
that contains a numeric vector. In this case, you must
specify Weights
as a character vector or string scalar. For
example, if the weights vector W
is stored as
Tbl.W
, then specify it as "W"
. Otherwise, the
software treats all columns of Tbl
, including
W
, as predictors or the response variable when training the
model.
fitrauto
ignores observation weights for Gaussian process
regression models. That is, when Learners
includes
"gp"
models, the function ignores the Weights
namevalue argument for those models.
By default, Weights
is ones(n,1)
, where
n
is the number of observations in X
or
Tbl
.
The software normalizes Weights
to sum to 1.
Data Types: single
 double
 char
 string
Output Arguments
Mdl
— Trained regression model
regression model object
Trained regression model, returned as one of the regression model objects in this table.
Learner Name  Returned Model Object 

"ensemble"  CompactRegressionEnsemble 
"gp"  CompactRegressionGP 
"kernel"  RegressionKernel 
"linear"  RegressionLinear 
"net"  CompactRegressionNeuralNetwork 
"svm"  CompactRegressionSVM 
"tree"  CompactRegressionTree 
OptimizationResults
— Optimization results
BayesianOptimization
object  table
Optimization results, returned as a BayesianOptimization
object if you use Bayesian optimization or a table if
you use ASHA optimization. For more information, see Bayesian Optimization and ASHA Optimization.
More About
Verbose Display
When you set the Verbose
field of the
HyperparameterOptimizationOptions
namevalue argument to
1
or 2
, the fitrauto
function
provides an iterative display of the optimization results.
The following table describes the columns in the display and their entries.
Column Name  Description 

Iter  Iteration number — You can set a limit to the number of iterations by using
the MaxObjectiveEvaluations field of the
HyperparameterOptimizationOptions namevalue
argument. 
Active workers  Number of active parallel workers — This column appears only when you run the
optimization in parallel by setting the UseParallel field of
the HyperparameterOptimizationOptions namevalue argument to
true . 
Eval result  One of the following evaluation results:

log(1 + valLoss)  Logtransformed validation loss computed for the learner and hyperparameter
values at this iteration — In particular, fitrauto computes log(1 + valLoss), where valLoss is the crossvalidation mean
squared error (MSE) by default. You can change the validation scheme by using the
CVPartition , Holdout , or
Kfold field of the
'HyperparameterOptimizationOptions' namevalue
argument. 
Time for training & validation (sec)  Time taken to train and compute the validation loss for the model with the learner and hyperparameter values at this iteration (in seconds) — When you use Bayesian optimization, this value excludes the time required to update the objective function model maintained by the Bayesian optimization process. For more details, see Bayesian Optimization. 
Observed min log(1 + valLoss)  Observed minimum logtransformed validation loss computed so far — This
value corresponds to the smallest By default,

Estimated min log(1 + valLoss)  Estimated minimum logtransformed validation loss — When you use
Bayesian optimization, By default, Note This column appears only when you use Bayesian optimization, that is, when
the 
Training set size  Number of observations used in each training set at this iteration —
Use the Note This column appears only when you use ASHA optimization, that is, when the

Learner  Model type evaluated at this iteration — Specify the learners used in the
optimization by using the Learners namevalue
argument. 
Hyperparameter: Value  Hyperparameter values at this iteration — Specify the hyperparameters used in
the optimization by using the OptimizeHyperparameters
namevalue argument. 
The display also includes these model descriptions:
Best observed learner
— This model, with the listed learner type and hyperparameter values, yields the final observed minimum validation loss (logtransformed). (For more information, see the description forObserved min log(1 + valLoss)
in the previous table.) When you use ASHA optimization,fitrauto
retrains the model on the entire training data set and returns it as theMdl
output.Best estimated learner
— This model, with the listed learner type and hyperparameter values, yields the final estimated minimum validation loss (logtransformed) when you use Bayesian optimization. (For more information, see the description forEstimated min log(1 + valLoss)
in the previous table.) In this case,fitrauto
retrains the model on the entire training data set and returns it as theMdl
output.Note
The
Best estimated learner
model appears only when you use Bayesian optimization, that is, when theOptimizer
field of theHyperparameterOptimizationOptions
namevalue argument is set to"bayesopt"
.
Tips
Depending on the size of your data set, the number of learners you specify, and the optimization method you choose,
fitrauto
can take some time to run.If you have a Parallel Computing Toolbox license, you can speed up computations by running the optimization in parallel. To do so, specify
"HyperparameterOptimizationOptions",struct("UseParallel",true)
. You can include additional fields in the structure to control other aspects of the optimization. SeeHyperparameterOptimizationOptions
.If
fitrauto
with Bayesian optimization takes a long time to run because of the number of observations in your training set (for example, over 10,000), consider usingfitrauto
with ASHA optimization instead. ASHA optimization often finds good solutions faster than Bayesian optimization for data sets with many observations. To use ASHA optimization, specify"HyperparameterOptimizationOptions",struct("Optimizer","asha")
. You can include additional fields in the structure to control additional aspects of the optimization. In particular, if you have a time constraint, specify theMaxTime
field of theHyperparameterOptimizationOptions
structure to limit the number of secondsfitrauto
runs.
Algorithms
Automatic Selection of Learners
When you specify "Learners","auto"
, the fitrauto
function analyzes the predictor and response data in order to choose appropriate learners.
The function considers whether the data set has any of these characteristics:
Categorical predictors
Missing values for more than 5% of the data
Wide data, where the number of predictors is greater than or equal to the number of observations
Highdimensional data, where the number of predictors is greater than 100
Large data, where the number of observations is greater than 50,000
The selected learners are always a subset of those listed in the Learners
table.
However, the associated models tried during the optimization process can have different
default values for hyperparameters not being optimized, as well as different search ranges
for hyperparameters being optimized.
Bayesian Optimization
The goal of Bayesian optimization, and optimization in general, is to find a point that
minimizes an objective function. In the context of fitrauto
, a point is
a learner type together with a set of hyperparameter values for the learner (see Learners
and
OptimizeHyperparameters
), and the objective function is log(1 + valLoss), where valLoss is the crossvalidation mean squared
error (MSE), by default. The Bayesian optimization implemented in
fitrauto
internally maintains a multiRegressionGP
model of the objective function. That is, the objective function
model splits along the learner type and, for a given learner, the model is a Gaussian
process regression (GPR) model. (This underlying model differs from the single GPR model
employed by other Statistics and Machine Learning Toolbox™ functions that use Bayesian optimization.) Bayesian optimization trains the
underlying model by using objective function evaluations, and determines the next point to
evaluate by using an acquisition function ("expectedimprovement"
). For
more information, see Expected Improvement. The acquisition function balances between sampling at
points with low modeled objective function values and exploring areas that are not well
modeled yet. At the end of the optimization, fitrauto
chooses the point
with the minimum objective function model value, among the points evaluated during the
optimization. For more information, see the
"Criterion","minvisitedmean"
namevalue argument of bestPoint
.
ASHA Optimization
The asynchronous successive halving algorithm (ASHA) in fitrauto
randomly chooses several models with different hyperparameter values (see Learners
and
OptimizeHyperparameters
) and trains them on a small subset of the training
data. If the performance of a particular model is promising, the model is promoted and
trained on a larger amount of the training data. This process repeats, and successful models
are trained on progressively larger amounts of data. By default, at the end of the
optimization, fitrauto
chooses the model that has the lowest log(1 + valLoss) value, where valLoss is the crossvalidation mean
squared error (MSE).
At each iteration, ASHA either chooses a previously trained model and promotes it (that is, retrains the model using more training data), or selects a new model (learner type and hyperparameter values) using random search. ASHA promotes models as follows:
The algorithm searches for the group of models with the largest training set size for which this condition does not hold:
floor(g/4)
of the models have been promoted, whereg
is the number of models in the group.Among the group of models, ASHA chooses the model with the lowest log(1 + valLoss) value and retrains that model with
4*(Training Set Size)
observations.If no such group of models exists, then ASHA selects a new model instead of promoting an old one, and trains the new model using the smallest training set size.
When a model is trained on a subset of the training data, ASHA computes the crossvalidation MSE in the as follows:
For each training fold, the algorithm selects a random sample of the observations (of size
Training set size
) using nonstratified sampling, and then trains a model on that subset of data.The algorithm then tests the fitted model on the test fold (that is, the observations not in the training fold) and computes the MSE.
Finally, the algorithm averages the results across all folds.
For more information on ASHA, see [1].
Number of ASHA Iterations
When you use ASHA optimization, the default number of iterations depends on the number
of observations in the data, the number of learner types, the use of parallel processing,
and the type of crossvalidation. The algorithm selects the number of iterations such that,
for L learner types (see Learners
),
fitrauto
trains L models on the largest training
set size.
This table describes the default number of iterations based on the given specifications when you use 5fold crossvalidation. Note that n represents the number of observations and L represents the number of learner types.
Number of Observations n  Default Number of Iterations (run in serial)  Default Number of Iterations (run in parallel) 

n < 500  30*L — n is too small to implement ASHA
optimization, and fitrauto implements random search to find and
assess models instead.  30*L — n is too small to implement ASHA
optimization, and fitrauto implements random search to find and
assess models instead. 
500 ≤ n < 2000  5*L  5*(L + 1) 
2000 ≤ n < 8000  21*L  21*(L + 1) 
8000 ≤ n < 32,000  85*L  85*(L + 1) 
32,000 ≤ n  341*L  341*(L + 1) 
Alternative Functionality
If you are unsure which models work best for your data set, you can alternatively use the Regression Learner app. Using the app, you can perform hyperparameter tuning for different models, and choose the optimized model that performs best. Although you must select a specific model before you can tune the model hyperparameters, Regression Learner provides greater flexibility for selecting optimizable hyperparameters and setting hyperparameter values. The app also allows you to train a variety of linear regression models (see Linear Regression Models). However, you cannot optimize in parallel, specify observation weights, or use ASHA optimization in the app. For more information, see Hyperparameter Optimization in Regression Learner App.
If you know which models might suit your data, you can alternatively use the corresponding model fit functions and specify the
OptimizeHyperparameters
namevalue argument to tune hyperparameters. You can compare the results across the models to select the best regression model. For an example of this process applied to classification models, see Moving Towards Automating Model Selection Using Bayesian Optimization.
References
[1] Li, Liam, Kevin Jamieson, Afshin Rostamizadeh, Ekaterina Gonina, Moritz Hardt, Benjamin Recht, and Ameet Talwalkar. “A System for Massively Parallel Hyperparameter Tuning.” ArXiv:1810.05934v5 [Cs], March 16, 2020. https://arxiv.org/abs/1810.05934v5.
Extended Capabilities
Automatic Parallel Support
Accelerate code by automatically running computation in parallel using Parallel Computing Toolbox™.
To perform parallel hyperparameter optimization, use the
"HyperparameterOptimizationOptions",struct("UseParallel",true)
namevalue argument in the call to this function.
For more general information about parallel computing, see Run MATLAB Functions with Automatic Parallel Support (Parallel Computing Toolbox).
Version History
Introduced in R2020bR2023b: "auto"
option of OptimizeHyperparameters
includes Standardize
for Gaussian process regression (GPR), kernel, and support
vector machine (SVM) models
Starting in R2023b, when the Learners
value includes Gaussian
process regression ("gp"
), kernel ("kernel"
), or
support vector machine ("svm"
) models, the
fitrauto
function optimizes the Standardize
hyperparameter of the models by default. That is, if the
OptimizeHyperparameters
value is "auto"
, then
Standardize
is an optimizable hyperparameter of GPR, kernel, and SVM
models.
R2023b: KernelScale
hyperparameter search range does not depend on predictor data during optimization of GPR models
Starting in R2023b, fitrauto
optimizes the kernel scale parameter for Gaussian process regression (GPR) models by using the default search range [1e3,1e3]
. That is, when you specify to optimize the GPR hyperparameter KernelScale
by using the OptimizeHyperparameters
namevalue argument, the function searches among positive values logscaled in the range [1e3,1e3]
.
In previous releases, the default search range for the KernelScale
hyperparameter was [1e3*MaxPredictorRange,MaxPredictorRange]
, where MaxPredictorRange = max(max(X)  min(X))
.
R2023a: Gaussian Process Regression (GPR) learners can include ARD kernels
The fitrauto
function includes ARD kernel options for Gaussian
process regression ("gp"
) learners when the
OptimizeHyperparameters
value is "all"
and the
data set has these characteristics:
The number of observations is less than or equal to 10,000.
The number of predictors is less than or equal to 32 after the expansion of the categorical predictors (see Automatic Creation of Dummy Variables).
When the KernelFunction
value of a GPR model is an ARD kernel option,
fitrauto
trains the model using these GPR tolerance and initial step
size values:
The
OptimizerOptions
value matches the followingoptions
structure:options = statset("fitrgp"); options.MaxIter = 1000; options.TolFun = 1e3; options.TolX = 1e3;
The
InitialStepSize
value is"auto"
.
In previous releases, the fitrauto
function ignored all ARD kernel
options for GPR learners, regardless of the size of the data set.
R2022a: Learners include neural network models
Starting in R2022a, the list of available learners includes neural network models. When you
specify "all"
or "allnonlinear"
for the
Learners
namevalue argument, fitrauto
includes neural network models as part of the model selection and hyperparameter tuning
process. The function also considers neural network models when you specify
Learners
as "auto"
, depending on the
characteristics of your data set.
To omit neural network models from the model selection process, you can explicitly specify the
models you want to include. For example, to use tree and ensemble models only, specify
"Learners",["tree","ensemble"]
.
R2022a: Automatic selection of learners includes linear models when data is wide after categorical expansion
Starting in R2022a, if you specify Learners
as
"auto"
and the data has more predictors than observations after the
expansion of the categorical predictors (see Automatic Creation of Dummy Variables), then
fitrauto
includes linear learners ("linear"
)
along with other models during the hyperparameter optimization. In previous releases, linear
learners were not considered.
R2021a: Regularization method determines the linear learner solver used during the optimization process
Starting in R2021a, when you specify to try a linear learner
("linear"
), fitrauto
uses either a
Limitedmemory BFGS (LBFGS) solver or a Sparse Reconstruction by Separable Approximation
(SpaRSA) solver, depending on the regularization type selected during that iteration of the
optimization process.
When
Regularization
is'ridge'
, the function sets theSolver
value to'lbfgs'
by default.When
Regularization
is'lasso'
, the function sets theSolver
value to'sparsa'
by default.
In previous releases, the default solver selection during the optimization process
depended on various factors, including the regularization type, learner type, and number of
predictors. For more information, see Solver
.
Ouvrir l'exemple
Vous possédez une version modifiée de cet exemple. Souhaitezvous ouvrir cet exemple avec vos modifications ?
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB :
Pour exécuter la commande, saisissezla dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)