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

    %% Optimize GPR Regression
% This example shows how to optimize hyperparameters automatically using
% |fitrgp|. The example uses the |gprdata2| data that ships with your
% software.
%
% Load the data.
load(fullfile(matlabroot,'examples','stats','gprdata2.mat'))
%%
% The data has one predictor variable and continuous response. This is
% simulated data.
%
% Fit a GPR model using the squared exponential kernel function with
% default kernel parameters.
gprMdl1 = fitrgp(x,y,'KernelFunction','squaredexponential');

%%
% Find hyperparameters that minimize five-fold cross-validation loss by
% using automatic hyperparameter optimization.
%
% For reproducibility, set the random seed and use the
% |'expected-improvement-plus'| acquisition function.
rng default
gprMdl2 = fitrgp(x,y,'KernelFunction','squaredexponential',...
    'OptimizeHyperparameters','auto','HyperparameterOptimizationOptions',...
    struct('AcquisitionFunctionName','expected-improvement-plus'));
%%
% Compare the pre- and post-optimization fits.
ypred1 = resubPredict(gprMdl1);
ypred2 = resubPredict(gprMdl2);

figure();
plot(x,y,'r.');
hold on
plot(x,ypred1,'b');
plot(x,ypred2,'k','LineWidth',2);
xlabel('x');
ylabel('y');
legend({'data','Initial Fit','Optimized Fit'},'Location','Best');
title('Impact of Optimization');
hold off