www.gusucode.com > stats 源码程序 matlab案例代码 > stats/OptimizeALinearRegressionExample.m

    %% Optimize a Linear Regression
% This example shows how to optimize hyperparameters automatically using
% |fitrlinear|. The example uses artificial (simulated) data for the model
%
% $$y = x_{100} + 2x_{200} + e.$$
%
% * $X = \{x_1,...,x_{1000}\}$ is a 10000-by-1000 sparse matrix with 10%
% nonzero standard normal elements.
% * _e_ is random normal error with mean 0 and standard deviation 0.3.
rng(1) % For reproducibility
n = 1e4;
d = 1e3;
nz = 0.1;
X = sprandn(n,d,nz);
Y = X(:,100) + 2*X(:,200) + 0.3*randn(n,1);
%%
% Find hyperparameters that minimize five-fold cross validation loss by
% using automatic hyperparameter optimization.
%
% For reproducibility, use the |'expected-improvement-plus'| acquisition
% function.
hyperopts = struct('AcquisitionFunctionName','expected-improvement-plus');
[Mdl,FitInfo,HyperparameterOptimizationResults] = fitrlinear(X,Y,...
    'OptimizeHyperparameters','auto',...
    'HyperparameterOptimizationOptions',hyperopts)
%%
% This optimization technique is simpler than that shown in
% <docid:stats_ug.bu5tqz1-1>, but does not allow you to trade off model
% complexity and cross-validation loss.