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  1.2897  3.3416  3.3416  tree  MinLeafSize: 118   2  Accept  4.1303  0.62208  3.3416  3.3416  svm  BoxConstraint: 16.579          KernelScale: 0.0045538          Epsilon: 657.79   3  Best  2.5197  0.18007  2.5197  2.6121  tree  MinLeafSize: 2   4  Best  2.3335  5.719  2.3335  2.3335  ensemble  Method: Bag          NumLearningCycles: 291          MinLeafSize: 9   5  Accept  2.3398  3.7552  2.3335  2.3366  ensemble  Method: Bag          NumLearningCycles: 206          MinLeafSize: 13   6  Best  2.204  5.1042  2.204  2.2049  ensemble  Method: LSBoost          NumLearningCycles: 256          MinLeafSize: 12   7  Accept  4.1303  0.098853  2.204  2.2049  svm  BoxConstraint: 0.0048178          KernelScale: 0.011576          Epsilon: 441.39   8  Accept  2.4787  0.074041  2.204  2.2049  tree  MinLeafSize: 9   9  Accept  4.1303  0.070297  2.204  2.2049  svm  BoxConstraint: 8.581          KernelScale: 61.095          Epsilon: 296.69   10  Accept  4.1303  0.053978  2.204  2.2049  svm  BoxConstraint: 140.96          KernelScale: 0.012197          Epsilon: 69.002   11  Accept  2.9157  0.045178  2.204  2.2049  tree  MinLeafSize: 32   12  Accept  3.2199  0.050483  2.204  2.2049  tree  MinLeafSize: 64   13  Accept  2.4157  0.048328  2.204  2.2049  tree  MinLeafSize: 4   14  Accept  4.1303  0.076781  2.204  2.2049  svm  BoxConstraint: 1.3859          KernelScale: 71.061          Epsilon: 181.44   15  Accept  3.4156  0.037288  2.204  2.2049  tree  MinLeafSize: 102   16  Accept  2.5197  0.054567  2.204  2.2049  tree  MinLeafSize: 2   17  Accept  5.4306  31.526  2.204  2.2049  svm  BoxConstraint: 0.0018102          KernelScale: 0.016815          Epsilon: 8.1687   18  Accept  3.1121  4.403  2.204  2.2042  ensemble  Method: Bag          NumLearningCycles: 288          MinLeafSize: 106   19  Best  2.1971  4.0613  2.1971  2.1972  ensemble  Method: LSBoost          NumLearningCycles: 227          MinLeafSize: 2   20  Best  2.1971  3.985  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  4.723  2.1971  2.1972  ensemble  Method: LSBoost          NumLearningCycles: 262          MinLeafSize: 5   22  Accept  2.5925  4.6352  2.1971  2.1972  ensemble  Method: Bag          NumLearningCycles: 283          MinLeafSize: 39   23  Accept  2.1971  5.0225  2.1971  2.1972  ensemble  Method: LSBoost          NumLearningCycles: 285          MinLeafSize: 2   24  Accept  2.3345  2.9953  2.1971  2.1972  svm  BoxConstraint: 2.2648          KernelScale: 0.92531          Epsilon: 0.51865   25  Accept  2.9882  3.887  2.1971  2.1971  ensemble  Method: Bag          NumLearningCycles: 247          MinLeafSize: 73   26  Accept  2.3583  4.2468  2.1971  2.1971  ensemble  Method: Bag          NumLearningCycles: 255          MinLeafSize: 12   27  Accept  2.6476  0.042392  2.1971  2.1971  tree  MinLeafSize: 28   28  Accept  2.4016  0.044492  2.1971  2.1971  tree  MinLeafSize: 6   29  Accept  3.7573  0.065073  2.1971  2.1971  svm  BoxConstraint: 9.4057          KernelScale: 100.66          Epsilon: 0.24447   30  Accept  2.6046  0.039155  2.1971  2.1971  tree  MinLeafSize: 24   31  Accept  2.4157  0.045287  2.1971  2.1971  tree  MinLeafSize: 4   32  Accept  4.1303  0.050301  2.1971  2.1971  svm  BoxConstraint: 303.85          KernelScale: 0.0083624          Epsilon: 39.54   33  Accept  4.146  0.056571  2.1971  2.1971  svm  BoxConstraint: 0.16546          KernelScale: 248.79          Epsilon: 1.1182   34  Accept  3.0466  0.038386  2.1971  2.1971  tree  MinLeafSize: 46   35  Accept  2.3417  4.1224  2.1971  2.1971  ensemble  Method: Bag          NumLearningCycles: 246          MinLeafSize: 12   36  Accept  2.7264  4.748  2.1971  2.1972  ensemble  Method: LSBoost          NumLearningCycles: 274          MinLeafSize: 105   37  Accept  2.5457  4.1701  2.1971  2.1972  ensemble  Method: Bag          NumLearningCycles: 257          MinLeafSize: 33   38  Accept  2.6603  0.055123  2.1971  2.1972  tree  MinLeafSize: 1   39  Accept  2.36  5.3754  2.1971  2.1972  svm  BoxConstraint: 56.509          KernelScale: 1.509          Epsilon: 0.5604   40  Accept  4.1303  0.048283  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.040831  2.1971  2.1972  tree  MinLeafSize: 15   42  Best  2.1945  5.1078  2.1945  2.1966  ensemble  Method: LSBoost          NumLearningCycles: 289          MinLeafSize: 6   43  Accept  3.9873  0.056392  2.1945  2.1966  svm  BoxConstraint: 31.423          KernelScale: 0.16609          Epsilon: 14.619   44  Accept  4.0639  0.058227  2.1945  2.1966  svm  BoxConstraint: 42.958          KernelScale: 459.03          Epsilon: 0.98679   45  Accept  2.5389  0.055917  2.1945  2.1966  svm  BoxConstraint: 32.844          KernelScale: 24.244          Epsilon: 2.8938   46  Accept  4.1222  0.055003  2.1945  2.1966  svm  BoxConstraint: 0.001348          KernelScale: 5.1158          Epsilon: 2.4534   47  Best  2.1533  0.074854  2.1533  2.1534  svm  BoxConstraint: 161.96          KernelScale: 7.1682          Epsilon: 1.6972   48  Best  2.1044  0.058537  2.1044  2.1045  svm  BoxConstraint: 10.597          KernelScale: 4.8052          Epsilon: 0.68924   49  Accept  2.1323  0.063815  2.1044  2.1045  svm  BoxConstraint: 12.625          KernelScale: 3.7951          Epsilon: 1.9243   50  Accept  2.1143  0.10138  2.1044  2.1042  svm  BoxConstraint: 2.9811          KernelScale: 2.2304          Epsilon: 0.11742   51  Accept  2.1121  0.097271  2.1044  2.1044  svm  BoxConstraint: 4.6122          KernelScale: 2.5002          Epsilon: 0.24122   52  Accept  2.3152  4.2279  2.1044  2.1044  svm  BoxConstraint: 13.002          KernelScale: 1.2194          Epsilon: 0.0097793   53  Accept  2.1474  0.11001  2.1044  2.1041  svm  BoxConstraint: 113.51          KernelScale: 5.567          Epsilon: 0.052251   54  Accept  2.1274  0.064355  2.1044  2.1045  svm  BoxConstraint: 51.97          KernelScale: 7.9449          Epsilon: 0.31232   55  Accept  2.1313  0.10387  2.1044  2.1046  svm  BoxConstraint: 17.469          KernelScale: 3.3438          Epsilon: 0.050142   56  Accept  2.2052  5.3754  2.1044  2.1046  ensemble  Method: LSBoost          NumLearningCycles: 299          MinLeafSize: 1   57  Accept  2.1125  0.090322  2.1044  2.1047  svm  BoxConstraint: 50.684          KernelScale: 4.764          Epsilon: 0.45053   58  Accept  2.3465  3.4461  2.1044  2.1047  ensemble  Method: Bag          NumLearningCycles: 201          MinLeafSize: 1   59  Accept  2.1222  0.11319  2.1044  2.105  svm  BoxConstraint: 859.14          KernelScale: 12.367          Epsilon: 0.2022   60  Accept  2.1461  0.19386  2.1044  2.105  svm  BoxConstraint: 968.32          KernelScale: 8.9428          Epsilon: 0.01767  ================================================================================================================================================  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.1407  0.22769  2.1044  2.1051  svm  BoxConstraint: 971.76          KernelScale: 8.0121          Epsilon: 0.27594   62  Accept  2.132  0.063274  2.1044  2.1051  svm  BoxConstraint: 7.3349          KernelScale: 4.5654          Epsilon: 0.010146   63  Accept  2.1457  0.068571  2.1044  2.1054  svm  BoxConstraint: 182.05          KernelScale: 13.913          Epsilon: 0.010813   64  Accept  2.1195  0.068772  2.1044  2.1069  svm  BoxConstraint: 7.653          KernelScale: 4.1874          Epsilon: 0.13638   65  Accept  2.1127  0.09654  2.1044  2.1075  svm  BoxConstraint: 1.8061          KernelScale: 2.0447          Epsilon: 0.010408   66  Accept  2.1374  0.093402  2.1044  2.1076  svm  BoxConstraint: 984.16          KernelScale: 17.924          Epsilon: 0.013101   67  Accept  2.3467  3.4222  2.1044  2.1076  ensemble  Method: Bag          NumLearningCycles: 201          MinLeafSize: 3   68  Accept  2.1251  0.084559  2.1044  2.1078  svm  BoxConstraint: 4.8829          KernelScale: 2.7157          Epsilon: 0.011329   69  Accept  4.1303  0.048665  2.1044  2.1045  svm  BoxConstraint: 981.43          KernelScale: 3.7956          Epsilon: 524.32   70  Best  2.0946  0.078617  2.0946  2.0958  svm  BoxConstraint: 9.3796          KernelScale: 3.6153          Epsilon: 0.64581   71  Accept  2.1625  0.059021  2.0946  2.0957  svm  BoxConstraint: 0.14003          KernelScale: 2.3859          Epsilon: 0.0096989   72  Accept  2.1254  0.062282  2.0946  2.0957  svm  BoxConstraint: 1.0541          KernelScale: 2.8873          Epsilon: 0.016892   73  Accept  2.2047  0.42757  2.0946  2.0957  svm  BoxConstraint: 0.20258          KernelScale: 0.89876          Epsilon: 0.0094548   74  Accept  4.1303  0.056501  2.0946  2.0957  svm  BoxConstraint: 705.97          KernelScale: 0.0010614          Epsilon: 0.011029   75  Accept  2.1076  0.093847  2.0946  2.0956  svm  BoxConstraint: 0.57149          KernelScale: 1.6966          Epsilon: 0.028341   76  Accept  2.1051  0.074496  2.0946  2.0972  svm  BoxConstraint: 17.713          KernelScale: 4.1474          Epsilon: 0.46742   77  Accept  2.0999  0.072478  2.0946  2.0977  svm  BoxConstraint: 16.834          KernelScale: 4.194          Epsilon: 0.7058   78  Accept  2.117  0.073675  2.0946  2.0976  svm  BoxConstraint: 0.80221          KernelScale: 2.1521          Epsilon: 0.009312   79  Accept  2.1063  0.071597  2.0946  2.0998  svm  BoxConstraint: 11.766          KernelScale: 4.0434          Epsilon: 0.77028   80  Accept  2.131  0.11884  2.0946  2.0997  svm  BoxConstraint: 639.99          KernelScale: 11.976          Epsilon: 0.045587  ================================================================================================================================================  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.1353  0.12266  2.0946  2.0998  svm  BoxConstraint: 833.03          KernelScale: 12.286          Epsilon: 0.009267   82  Accept  2.1002  0.075115  2.0946  2.0995  svm  BoxConstraint: 28.193          KernelScale: 5.1564          Epsilon: 0.54563   83  Accept  2.1087  0.083842  2.0946  2.0993  svm  BoxConstraint: 22.81          KernelScale: 4.8839          Epsilon: 0.81407   84  Accept  2.1047  0.08404  2.0946  2.0998  svm  BoxConstraint: 11.5          KernelScale: 3.7878          Epsilon: 0.38006   85  Accept  2.0999  0.076743  2.0946  2.0996  svm  BoxConstraint: 7.9463          KernelScale: 3.3494          Epsilon: 0.56823   86  Accept  2.121  0.12711  2.0946  2.0996  svm  BoxConstraint: 1.4247          KernelScale: 1.7405          Epsilon: 0.030609   87  Accept  4.1303  0.056272  2.0946  2.0995  svm  BoxConstraint: 0.0020273          KernelScale: 0.001035          Epsilon: 832.86   88  Accept  2.114  0.070002  2.0946  2.1014  svm  BoxConstraint: 10.323          KernelScale: 3.9111          Epsilon: 0.90254   89  Accept  2.1042  0.073487  2.0946  2.1015  svm  BoxConstraint: 6.5035          KernelScale: 3.1694          Epsilon: 0.63488   90  Accept  2.102  0.072913  2.0946  2.1015  svm  BoxConstraint: 23.367          KernelScale: 4.8485          Epsilon: 0.51211 
__________________________________________________________ Optimization completed. Total iterations: 90 Total elapsed time: 172.6365 seconds Total time for training and validation: 131.3656 seconds Best observed learner is an svm model with: Learner: svm BoxConstraint: 9.3796 KernelScale: 3.6153 Epsilon: 0.64581 Observed log(1 + valLoss): 2.0946 Time for training and validation: 0.078617 seconds Best estimated learner (returned model) is an svm model with: Learner: svm BoxConstraint: 9.3796 KernelScale: 3.6153 Epsilon: 0.64581 Estimated log(1 + valLoss): 2.1015 Estimated time for training and validation: 0.076063 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.1805
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 'local' profile ... Connected to the parallel pool (number of workers: 8). 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  5  Best  3.1329  1.9087  3.1329  3.1399  tree  MinLeafSize: 5   2  5  Accept  3.1329  1.9094  3.1329  3.1399  tree  MinLeafSize: 5   3  5  Accept  4.1701  1.9944  3.1329  3.1399  svm  BoxConstraint: 0.033502           KernelScale: 153.38           Epsilon: 0.095234   4  5  Accept  3.1539  1.9051  3.1329  3.1399  tree  MinLeafSize: 9   5  7  Best  3.0917  0.20244  3.0917  3.0919  tree  MinLeafSize: 14   6  7  Accept  4.1645  0.20868  3.0917  3.0919  tree  MinLeafSize: 158   7  8  Accept  3.2871  0.14105  3.0917  3.0919  tree  MinLeafSize: 2   8  8  Accept  3.2871  0.12338  3.0917  3.0919  tree  MinLeafSize: 2   9  8  Accept  4.1645  1.3189  3.0917  3.0919  svm  BoxConstraint: 0.003952           KernelScale: 0.0015586           Epsilon: 31.184   10  8  Accept  3.2871  0.19579  3.0917  3.0919  tree  MinLeafSize: 2   11  8  Accept  4.1646  8.3996  3.0917  3.0919  ensemble  Method: Bag           NumLearningCycles: 257           MinLeafSize: 154   12  8  Best  2.9469  8.7243  2.9469  2.968  ensemble  Method: LSBoost           NumLearningCycles: 287           MinLeafSize: 1   13  8  Best  2.9388  9.9614  2.9388  2.942  ensemble  Method: LSBoost           NumLearningCycles: 288           MinLeafSize: 3   14  7  Accept  4.1645  1.9698  2.9388  2.9411  svm  BoxConstraint: 159.44           KernelScale: 34.732           Epsilon: 412.2   15  7  Accept  2.9581  8.4974  2.9388  2.9411  ensemble  Method: LSBoost           NumLearningCycles: 287           MinLeafSize: 62   16  7  Accept  3.1637  0.98081  2.9388  2.9411  tree  MinLeafSize: 6   17  7  Accept  3.1539  0.58494  2.9388  2.9411  tree  MinLeafSize: 9   18  5  Accept  2.9287  9.2877  2.9287  2.9411  ensemble  Method: LSBoost           NumLearningCycles: 283           MinLeafSize: 16   19  5  Best  2.9287  8.4297  2.9287  2.9411  ensemble  Method: LSBoost           NumLearningCycles: 262           MinLeafSize: 5   20  5  Accept  3.9011  0.19628  2.9287  2.9411  svm  BoxConstraint: 9.4057           KernelScale: 100.66           Epsilon: 0.2386  ==========================================================================================================================================================  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.1862  0.23729  2.9287  2.9411  svm  BoxConstraint: 7.487           KernelScale: 81.753           Epsilon: 12.782   22  5  Accept  3.1593  0.12638  2.9287  2.9411  tree  MinLeafSize: 8   23  5  Accept  3.1163  0.2154  2.9287  2.9411  tree  MinLeafSize: 42   24  5  Accept  4.1645  0.13652  2.9287  2.9411  svm  BoxConstraint: 760.95           KernelScale: 1.8573           Epsilon: 83.037   25  5  Accept  3.0374  0.14  2.9287  2.9411  tree  MinLeafSize: 16   26  8  Accept  3.1014  0.066518  2.9287  2.9411  tree  MinLeafSize: 13   27  7  Accept  2.9493  0.57729  2.9287  2.9411  svm  BoxConstraint: 574.43           KernelScale: 7.3229           Epsilon: 3.991   28  7  Accept  3.3681  0.19666  2.9287  2.9411  tree  MinLeafSize: 69   29  5  Accept  3.1815  7.8877  2.9287  2.9411  ensemble  Method: Bag           NumLearningCycles: 247           MinLeafSize: 74   30  5  Accept  4.1645  1.2207  2.9287  2.9411  svm  BoxConstraint: 5.2634           KernelScale: 0.058706           Epsilon: 105.48   31  5  Accept  3.6021  0.091822  2.9287  2.9411  tree  MinLeafSize: 1   32  8  Accept  3.1539  0.23324  2.9287  2.9411  tree  MinLeafSize: 9   33  6  Accept  2.9351  12.312  2.9197  2.9198  ensemble  Method: LSBoost           NumLearningCycles: 282           MinLeafSize: 2   34  6  Accept  4.2521  0.1172  2.9197  2.9198  svm  BoxConstraint: 449.81           KernelScale: 19.912           Epsilon: 17.095   35  6  Best  2.9197  0.085188  2.9197  2.9198  svm  BoxConstraint: 1.0008           KernelScale: 2.1267           Epsilon: 0.034964   36  7  Accept  4.1091  0.081418  2.9197  2.9411  svm  BoxConstraint: 42.958           KernelScale: 459.03           Epsilon: 0.96311   37  7  Accept  4.1091  0.064642  2.9197  2.9198  svm  BoxConstraint: 42.958           KernelScale: 459.03           Epsilon: 0.96311   38  7  Accept  2.9423  4.1841  2.9197  2.9198  ensemble  Method: LSBoost           NumLearningCycles: 202           MinLeafSize: 50   39  7  Accept  3.1132  0.066762  2.9197  2.9198  tree  MinLeafSize: 38   40  7  Accept  9.5074  20.851  2.9197  2.9198  svm  BoxConstraint: 336.91           KernelScale: 0.0018275           Epsilon: 0.10919  ==========================================================================================================================================================  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.1645  0.31377  2.9197  2.9198  svm  BoxConstraint: 582.79           KernelScale: 7.6869           Epsilon: 74.905   42  7  Accept  4.1645  4.6708  2.9197  2.9198  ensemble  Method: LSBoost           NumLearningCycles: 233           MinLeafSize: 134   43  7  Accept  2.935  5.2719  2.9197  2.9198  ensemble  Method: Bag           NumLearningCycles: 280           MinLeafSize: 45   44  7  Accept  3.1177  0.88837  2.9197  2.9198  svm  BoxConstraint: 127.48           KernelScale: 105           Epsilon: 0.58941   45  8  Accept  3.1329  0.10668  2.9197  2.9198  tree  MinLeafSize: 5   46  8  Best  2.8874  7.2754  2.8874  2.8877  ensemble  Method: Bag           NumLearningCycles: 271           MinLeafSize: 1   47  8  Accept  3.9204  0.2276  2.8874  2.8877  svm  BoxConstraint: 0.014973           KernelScale: 4.398           Epsilon: 0.055595   48  8  Accept  2.8964  5.3009  2.8874  2.8877  ensemble  Method: Bag           NumLearningCycles: 233           MinLeafSize: 20   49  8  Accept  2.8941  10.022  2.8874  2.8908  ensemble  Method: Bag           NumLearningCycles: 300           MinLeafSize: 1   50  8  Accept  2.9227  8.3714  2.8874  2.8908  ensemble  Method: LSBoost           NumLearningCycles: 360           MinLeafSize: 9   51  8  Accept  3.0945  0.10022  2.8874  2.8908  tree  MinLeafSize: 11   52  8  Accept  2.9326  7.3667  2.8874  2.8908  ensemble  Method: LSBoost           NumLearningCycles: 316           MinLeafSize: 51   53  8  Accept  2.9223  6.3741  2.8874  2.8908  ensemble  Method: Bag           NumLearningCycles: 234           MinLeafSize: 35   54  6  Best  2.8833  12.385  2.8833  2.8875  ensemble  Method: Bag           NumLearningCycles: 300           MinLeafSize: 1   55  6  Accept  4.1645  6.146  2.8833  2.8875  svm  BoxConstraint: 1.246           KernelScale: 0.048145           Epsilon: 337.42   56  6  Accept  3.5019  4.3292  2.8833  2.8875  ensemble  Method: Bag           NumLearningCycles: 219           MinLeafSize: 126   57  6  Best  2.8758  4.1728  2.8758  2.8875  ensemble  Method: Bag           NumLearningCycles: 201           MinLeafSize: 4   58  6  Accept  3.978  0.12062  2.8758  2.8875  svm  BoxConstraint: 0.015506           KernelScale: 4.6409           Epsilon: 2.9763   59  7  Accept  3.4183  0.049204  2.8758  2.8875  tree  MinLeafSize: 93   60  7  Accept  2.9269  4.8518  2.8758  2.8759  ensemble  Method: LSBoost           NumLearningCycles: 236           MinLeafSize: 19  ==========================================================================================================================================================  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  7  Accept  3.0652  0.088667  2.8758  2.8759  svm  BoxConstraint: 862.71           KernelScale: 216.51           Epsilon: 1.9551   62  7  Accept  2.8816  4.0609  2.8758  2.8767  ensemble  Method: Bag           NumLearningCycles: 205           MinLeafSize: 4   63  7  Accept  4.1645  0.1847  2.8758  2.8767  svm  BoxConstraint: 654.1           KernelScale: 0.0009277           Epsilon: 0.67992   64  8  Accept  4.1645  0.04689  2.8758  2.8767  svm  BoxConstraint: 0.0022454           KernelScale: 1.2218           Epsilon: 1223.3   65  8  Accept  2.8789  5.3  2.8758  2.8788  ensemble  Method: Bag           NumLearningCycles: 279           MinLeafSize: 4   66  8  Accept  3.1637  0.099659  2.8758  2.8788  tree  MinLeafSize: 6   67  8  Accept  2.8772  5.6605  2.8758  2.8784  ensemble  Method: Bag           NumLearningCycles: 280           MinLeafSize: 4   68  8  Accept  3.2871  0.074658  2.8758  2.8784  tree  MinLeafSize: 2   69  8  Best  2.8743  6.7691  2.8743  2.8744  ensemble  Method: Bag           NumLearningCycles: 296           MinLeafSize: 7   70  8  Accept  4.8948  28.692  2.8743  2.8744  svm  BoxConstraint: 0.093799           KernelScale: 0.0053728           Epsilon: 17.621   71  7  Accept  2.8767  6.4563  2.8743  2.8743  ensemble  Method: Bag           NumLearningCycles: 298           MinLeafSize: 9   72  7  Accept  2.88  5.7962  2.8743  2.8743  ensemble  Method: Bag           NumLearningCycles: 299           MinLeafSize: 9   73  7  Accept  2.8843  4.9403  2.8743  2.8743  ensemble  Method: Bag           NumLearningCycles: 207           MinLeafSize: 2   74  7  Accept  4.1645  0.087433  2.8743  2.8743  svm  BoxConstraint: 10.694           KernelScale: 42.019           Epsilon: 58.68   75  8  Accept  3.1608  0.045498  2.8743  2.8743  tree  MinLeafSize: 3   76  8  Accept  2.8845  5.7044  2.8743  2.8743  ensemble  Method: Bag           NumLearningCycles: 298           MinLeafSize: 2   77  8  Accept  2.9155  5.6911  2.8743  2.8743  ensemble  Method: LSBoost           NumLearningCycles: 268           MinLeafSize: 27   78  8  Accept  4.1132  0.10111  2.8743  2.8743  svm  BoxConstraint: 0.0012611           KernelScale: 2.8609           Epsilon: 0.037899   79  8  Accept  3.2869  0.080313  2.8743  2.8743  tree  MinLeafSize: 66   80  8  Accept  3.4233  6.4584  2.8743  2.8745  ensemble  Method: LSBoost           NumLearningCycles: 301           MinLeafSize: 119  ==========================================================================================================================================================  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  6  Accept  3.2102  6.5686  2.8743  2.8744  ensemble  Method: Bag           NumLearningCycles: 360           MinLeafSize: 102   82  6  Accept  2.8842  5.6027  2.8743  2.8744  ensemble  Method: Bag           NumLearningCycles: 274           MinLeafSize: 14   83  6  Accept  2.8763  4.6428  2.8743  2.8744  ensemble  Method: Bag           NumLearningCycles: 205           MinLeafSize: 14   84  7  Accept  2.8807  4.3081  2.8743  2.8744  ensemble  Method: Bag           NumLearningCycles: 202           MinLeafSize: 14   85  8  Accept  3.0046  0.078152  2.8743  2.8744  tree  MinLeafSize: 27   86  8  Accept  3.1256  6.3616  2.8743  2.8744  ensemble  Method: Bag           NumLearningCycles: 314           MinLeafSize: 70   87  8  Best  2.8718  5.9613  2.8718  2.8719  ensemble  Method: Bag           NumLearningCycles: 300           MinLeafSize: 5   88  8  Accept  3.0843  0.048367  2.8718  2.8719  tree  MinLeafSize: 47   89  8  Accept  2.8808  5.6249  2.8718  2.8743  ensemble  Method: Bag           NumLearningCycles: 298           MinLeafSize: 5   90  7  Accept  2.9088  7.3928  2.8718  2.8743  ensemble  Method: LSBoost           NumLearningCycles: 367           MinLeafSize: 7   91  7  Accept  3.0105  0.039942  2.8718  2.8743  tree  MinLeafSize: 26 
__________________________________________________________ Optimization completed. Total iterations: 91 Total elapsed time: 65.6875 seconds Total time for training and validation: 337.1441 seconds Best observed learner is an ensemble model with: Learner: ensemble Method: Bag NumLearningCycles: 300 MinLeafSize: 5 Observed log(1 + valLoss): 2.8718 Time for training and validation: 5.9613 seconds Best estimated learner (returned model) is an ensemble model with: Learner: ensemble Method: Bag NumLearningCycles: 296 MinLeafSize: 7 Estimated log(1 + valLoss): 2.8743 Estimated time for training and validation: 6.2667 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.6519
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...
Warning: Files that have already been attached are being ignored. To see which files are attached see the 'AttachedFiles' property of the parallel pool.
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.1155  0.593  3.1155  3.1155  tree  MinLeafSize: 5           MaxNumSplits: 2           NumVariablesToSample: 3   2  6  Accept  4.1303  0.57793  3.1155  3.1155  svm  BoxConstraint: 0.73976           KernelScale: 2.7037           Epsilon: 38.421   3  6  Accept  4.1303  0.65317  3.1155  3.1155  svm  BoxConstraint: 0.0010671           KernelScale: 19.242           Epsilon: 44.847   4  6  Accept  4.1338  0.68024  3.1155  3.1155  svm  BoxConstraint: 0.095204           KernelScale: 63.457           Epsilon: 0.055423   5  6  Accept  4.1303  0.23392  3.1155  3.1155  svm  BoxConstraint: 0.01028           KernelScale: 0.0032203           Epsilon: 36.299   6  6  Best  2.5852  0.17018  2.5852  2.7155  tree  MinLeafSize: 2           MaxNumSplits: 120           NumVariablesToSample: 7   7  8  Accept  4.5891  4.8163  2.5852  2.7155  ensemble  Method: LSBoost           LearnRate: 0.0051188           MinLeafSize: 83           NumVariablesToSample: NaN   8  8  Accept  4.7998  6.0958  2.5852  2.7155  ensemble  Method: LSBoost           LearnRate: 0.0042702           MinLeafSize: 31           NumVariablesToSample: NaN   9  8  Accept  2.6407  5.1761  2.5852  2.6533  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 45           NumVariablesToSample: 6   10  8  Accept  5.749  5.7541  2.5852  2.6407  ensemble  Method: LSBoost           LearnRate: 0.0017397           MinLeafSize: 22           NumVariablesToSample: NaN   11  8  Accept  5.749  5.8128  2.5852  2.6408  ensemble  Method: LSBoost           LearnRate: 0.0017397           MinLeafSize: 22           NumVariablesToSample: NaN   12  8  Accept  5.749  6.3666  2.5852  2.6408  ensemble  Method: LSBoost           LearnRate: 0.0017397           MinLeafSize: 22           NumVariablesToSample: NaN   13  7  Accept  2.8653  3.1249  2.5852  2.6408  tree  MinLeafSize: 5           MaxNumSplits: 61           NumVariablesToSample: 2   14  7  Accept  4.1303  0.21536  2.5852  2.6408  svm  BoxConstraint: 27.717           KernelScale: 21.172           Epsilon: 390.93   15  5  Accept  4.6825  10.638  2.3273  2.6408  ensemble  Method: LSBoost           LearnRate: 0.0045559           MinLeafSize: 1           NumVariablesToSample: NaN   16  5  Best  2.3273  4.6997  2.3273  2.6408  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 1           NumVariablesToSample: 3   17  5  Accept  2.8316  0.35817  2.3273  2.6408  svm  BoxConstraint: 0.038615           KernelScale: 0.26266           Epsilon: 5.8807   18  8  Accept  3.1542  0.091401  2.3273  2.6408  tree  MinLeafSize: 2           MaxNumSplits: 2           NumVariablesToSample: 5   19  8  Accept  2.6177  0.097231  2.3273  2.6179  svm  BoxConstraint: 38.6           KernelScale: 50.168           Epsilon: 2.0294   20  7  Accept  4.1551  4.3963  2.3273  2.5864  ensemble  Method: LSBoost           LearnRate: 0.014547           MinLeafSize: 145           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    ==========================================================================================================================================================  21  7  Accept  2.6846  0.20978  2.3273  2.5864  tree  MinLeafSize: 1           MaxNumSplits: 248           NumVariablesToSample: 7   22  7  Accept  4.1303  0.086412  2.3273  2.5864  svm  BoxConstraint: 117.04           KernelScale: 629.41           Epsilon: 729.47   23  7  Accept  2.8059  0.051613  2.3273  2.586  tree  MinLeafSize: 2           MaxNumSplits: 4           NumVariablesToSample: 4   24  8  Accept  2.496  4.9911  2.3273  2.3286  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 29           NumVariablesToSample: 3   25  8  Best  2.3255  5.025  2.3255  2.3426  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 11           NumVariablesToSample: 3   26  8  Accept  2.504  0.1041  2.3255  2.3426  tree  MinLeafSize: 9           MaxNumSplits: 157           NumVariablesToSample: 6   27  6  Accept  2.3265  5.0488  2.3255  2.3426  ensemble  Method: LSBoost           LearnRate: 0.11482           MinLeafSize: 54           NumVariablesToSample: NaN   28  6  Accept  2.6931  0.12749  2.3255  2.3426  svm  BoxConstraint: 8.3226           KernelScale: 22.717           Epsilon: 3.2417   29  6  Accept  4.1303  0.05622  2.3255  2.3426  svm  BoxConstraint: 0.0011506           KernelScale: 352.85           Epsilon: 163.49   30  7  Accept  2.5184  5.2768  2.3255  2.3299  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 11           NumVariablesToSample: 1   31  6  Accept  2.5058  5.8769  2.2734  2.2739  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 11           NumVariablesToSample: 1   32  6  Best  2.2734  0.74899  2.2734  2.2739  svm  BoxConstraint: 344.75           KernelScale: 3.7441           Epsilon: 0.20094   33  5  Accept  2.2734  1.1325  2.2734  2.2739  svm  BoxConstraint: 344.75           KernelScale: 3.7441           Epsilon: 0.20094   34  5  Accept  2.6958  0.05227  2.2734  2.2739  tree  MinLeafSize: 4           MaxNumSplits: 5           NumVariablesToSample: 6   35  8  Accept  4.1448  0.085799  2.2734  2.2735  svm  BoxConstraint: 1.8227           KernelScale: 977.3           Epsilon: 2.0809   36  6  Accept  2.9801  0.064816  2.2734  2.2735  tree  MinLeafSize: 3           MaxNumSplits: 72           NumVariablesToSample: 1   37  6  Accept  2.6751  0.27642  2.2734  2.2735  tree  MinLeafSize: 7           MaxNumSplits: 31           NumVariablesToSample: 3   38  6  Accept  4.1303  0.11917  2.2734  2.2735  svm  BoxConstraint: 0.032336           KernelScale: 0.34645           Epsilon: 382.33   39  6  Best  2.1806  0.3992  2.1806  2.1809  svm  BoxConstraint: 197.52           KernelScale: 4.7757           Epsilon: 0.029282   40  7  Accept  2.6007  0.052877  2.1806  2.1809  tree  MinLeafSize: 14           MaxNumSplits: 311           NumVariablesToSample: 4  ==========================================================================================================================================================  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  8  Accept  2.4301  0.064636  2.1806  2.1809  tree  MinLeafSize: 7           MaxNumSplits: 16           NumVariablesToSample: 7   42  8  Accept  2.3683  4.1134  2.1806  2.1809  ensemble  Method: LSBoost           LearnRate: 0.52017           MinLeafSize: 3           NumVariablesToSample: NaN   43  8  Accept  4.1303  0.077599  2.1806  2.1809  svm  BoxConstraint: 188.58           KernelScale: 0.009867           Epsilon: 27.186   44  8  Accept  2.2817  4.1073  2.1806  2.1809  ensemble  Method: LSBoost           LearnRate: 0.39343           MinLeafSize: 1           NumVariablesToSample: NaN   45  8  Accept  2.2905  0.33312  2.1806  2.181  svm  BoxConstraint: 3.1495           KernelScale: 1.5767           Epsilon: 0.015324   46  8  Accept  2.1862  4.5949  2.1806  2.181  ensemble  Method: LSBoost           LearnRate: 0.39847           MinLeafSize: 4           NumVariablesToSample: NaN   47  8  Accept  4.0238  0.1936  2.1806  2.181  svm  BoxConstraint: 47.384           KernelScale: 407.1           Epsilon: 5.789   48  8  Accept  2.3271  5.613  2.1806  2.181  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 22           NumVariablesToSample: 6   49  8  Accept  4.1303  4.2256  2.1806  2.181  ensemble  Method: LSBoost           LearnRate: 0.21695           MinLeafSize: 156           NumVariablesToSample: NaN   50  8  Accept  4.1303  3.9433  2.1806  2.181  ensemble  Method: LSBoost           LearnRate: 0.24031           MinLeafSize: 156           NumVariablesToSample: NaN   51  8  Accept  5.9837  25.376  2.1806  2.1808  svm  BoxConstraint: 3.3336           KernelScale: 0.20068           Epsilon: 1.2333   52  8  Best  2.1775  4.8819  2.1775  2.1775  ensemble  Method: LSBoost           LearnRate: 0.13486           MinLeafSize: 2           NumVariablesToSample: NaN   53  8  Accept  4.1472  0.098786  2.1775  2.1775  svm  BoxConstraint: 0.024211           KernelScale: 234.23           Epsilon: 0.65694   54  7  Accept  16.058  30.775  2.1775  2.1774  svm  BoxConstraint: 0.87942           KernelScale: 0.042698           Epsilon: 4.1252   55  7  Accept  4.1295  3.4872  2.1775  2.1774  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 165           NumVariablesToSample: 5   56  7  Accept  4.1303  0.064431  2.1775  2.1774  svm  BoxConstraint: 0.0077264           KernelScale: 11.935           Epsilon: 22.619   57  6  Accept  5.2259  40.653  2.1775  2.1773  svm  BoxConstraint: 0.95481           KernelScale: 0.0033698           Epsilon: 0.060804   58  6  Accept  4.5815  6.4959  2.1775  2.1773  ensemble  Method: LSBoost           LearnRate: 0.0048265           MinLeafSize: 7           NumVariablesToSample: NaN   59  5  Accept  5.8709  9.4038  2.1775  2.1773  svm  BoxConstraint: 0.046968           KernelScale: 0.0027305           Epsilon: 1.2478   60  5  Accept  4.1489  0.059253  2.1775  2.1773  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  8  Accept  2.1923  4.9912  2.1775  2.177  ensemble  Method: LSBoost           LearnRate: 0.027552           MinLeafSize: 15           NumVariablesToSample: NaN   62  6  Accept  2.3235  0.13508  2.1775  2.177  svm  BoxConstraint: 0.060693           KernelScale: 1.9336           Epsilon: 2.9377   63  6  Accept  4.1303  0.10264  2.1775  2.177  svm  BoxConstraint: 283.72           KernelScale: 0.66147           Epsilon: 800.44   64  6  Accept  3.2671  0.056364  2.1775  2.177  tree  MinLeafSize: 3           MaxNumSplits: 2           NumVariablesToSample: 2   65  7  Accept  4.1303  0.072693  2.1775  2.177  svm  BoxConstraint: 958.92           KernelScale: 0.70563           Epsilon: 537.94   66  7  Accept  2.1872  4.1631  2.1775  2.1772  ensemble  Method: LSBoost           LearnRate: 0.12361           MinLeafSize: 15           NumVariablesToSample: NaN   67  7  Accept  4.1303  0.18462  2.1775  2.1772  tree  MinLeafSize: 140           MaxNumSplits: 5           NumVariablesToSample: 2   68  6  Accept  6.622  24.87  2.1775  1.4529  svm  BoxConstraint: 154.74           KernelScale: 0.43817           Epsilon: 0.069304   69  6  Accept  2.2017  4.3959  2.1775  1.4529  ensemble  Method: LSBoost           LearnRate: 0.10067           MinLeafSize: 13           NumVariablesToSample: NaN   70  5  Accept  2.2891  4.5089  2.1775  2.1774  ensemble  Method: LSBoost           LearnRate: 0.024227           MinLeafSize: 35           NumVariablesToSample: NaN   71  5  Accept  4.1303  0.062327  2.1775  2.1774  svm  BoxConstraint: 1246.2           KernelScale: 0.053143           Epsilon: 53.963   72  8  Accept  2.2118  4.6015  2.1775  2.1774  ensemble  Method: LSBoost           LearnRate: 0.11646           MinLeafSize: 14           NumVariablesToSample: NaN   73  6  Accept  2.1969  4.3467  2.1775  2.1774  ensemble  Method: LSBoost           LearnRate: 0.12319           MinLeafSize: 14           NumVariablesToSample: NaN   74  6  Accept  4.1303  0.10057  2.1775  2.1774  svm  BoxConstraint: 0.50824           KernelScale: 0.015015           Epsilon: 96.096   75  6  Accept  2.932  0.057265  2.1775  2.1774  tree  MinLeafSize: 7           MaxNumSplits: 22           NumVariablesToSample: 1   76  7  Accept  2.7649  0.12548  2.1775  2.1774  tree  MinLeafSize: 16           MaxNumSplits: 43           NumVariablesToSample: 3   77  7  Accept  2.1913  4.8165  2.1775  2.1761  ensemble  Method: LSBoost           LearnRate: 0.057539           MinLeafSize: 3           NumVariablesToSample: NaN   78  7  Accept  2.4788  4.6057  2.1775  2.1774  ensemble  Method: LSBoost           LearnRate: 0.99513           MinLeafSize: 19           NumVariablesToSample: NaN   79  8  Accept  6.1274  5.0102  2.1775  2.1759  ensemble  Method: LSBoost           LearnRate: 0.000767           MinLeafSize: 37           NumVariablesToSample: NaN   80  8  Accept  4.13  3.7349  2.1775  2.176  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 171           NumVariablesToSample: 5  ==========================================================================================================================================================  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  8  Accept  2.2406  6.658  2.1775  2.176  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 3           NumVariablesToSample: 7   82  8  Accept  2.6476  0.054688  2.1775  2.176  tree  MinLeafSize: 28           MaxNumSplits: 129           NumVariablesToSample: 7   83  8  Accept  2.255  6.6774  2.1775  2.176  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 3           NumVariablesToSample: 7   84  7  Accept  2.6645  10.337  2.1775  2.176  svm  BoxConstraint: 334.64           KernelScale: 2.0733           Epsilon: 0.039959   85  7  Accept  4.1303  0.062936  2.1775  2.176  svm  BoxConstraint: 88.122           KernelScale: 0.00080559           Epsilon: 80.763   86  7  Accept  2.2384  5.8157  2.1775  2.1761  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 1           NumVariablesToSample: 6   87  7  Accept  6.0666  6.0896  2.1775  2.1759  ensemble  Method: LSBoost           LearnRate: 0.00091284           MinLeafSize: 1           NumVariablesToSample: NaN   88  7  Accept  3.2269  0.045413  2.1775  2.1759  tree  MinLeafSize: 14           MaxNumSplits: 6           NumVariablesToSample: 1   89  8  Accept  2.2427  6.3348  2.1775  2.176  ensemble  Method: Bag           LearnRate: NaN           MinLeafSize: 3           NumVariablesToSample: 7   90  6  Accept  4.5354  8.5876  2.1775  2.1768  svm  BoxConstraint: 0.022073           KernelScale: 0.0034124           Epsilon: 2.9088   91  6  Accept  2.2072  4.8166  2.1775  2.1768  ensemble  Method: LSBoost           LearnRate: 0.067559           MinLeafSize: 1           NumVariablesToSample: NaN   92  6  Accept  3.0936  4.1763  2.1775  2.1768  ensemble  Method: LSBoost           LearnRate: 0.011017           MinLeafSize: 76           NumVariablesToSample: NaN 
__________________________________________________________ Optimization completed. Total iterations: 92 Total elapsed time: 65.349 seconds Total time for training and validation: 369.4973 seconds Best observed learner is an ensemble model with: Learner: ensemble Method: LSBoost LearnRate: 0.13486 MinLeafSize: 2 NumVariablesToSample: NaN Observed log(1 + valLoss): 2.1775 Time for training and validation: 4.8819 seconds Best estimated learner (returned model) is an ensemble model with: Learner: ensemble Method: LSBoost LearnRate: 0.13486 MinLeafSize: 2 NumVariablesToSample: NaN Estimated log(1 + valLoss): 2.1768 Estimated time for training and validation: 4.7705 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.2140
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  BasisFunction ,
KernelFunction ,
KernelScale (KernelParameters ), Standardize 
For more information, including hyperparameter search
ranges, see 
"kernel"  Epsilon , KernelScale , Lambda  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  KernelFunction ,
PolynomialOrder , Standardize  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, optimize
"linear"
or"kernel"
learners, 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 R2020bR2023a: 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
.
MATLAB 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 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)