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

    %% Specify Custom Loss Function  

%% 
% Load the sample data and store in a |table|. 
load fisheriris
tbl = table(meas(:,1),meas(:,2),meas(:,3),meas(:,4),species,...
'VariableNames',{'meas1','meas2','meas3','meas4','species'});  

%% 
% Fit a GPR model using the first measurement as the response and the other
% variables as the predictors. 
mdl = fitrgp(tbl,'meas1');  

%% 
% Predict the responses using the trained model. 
ypred = predict(mdl,tbl);  

%% 
% Compute the mean absolute error. 
n = height(tbl);
y = tbl.meas1;
fun = @(y,ypred,w) sum(abs(y-ypred))/n;
L = loss(mdl,tbl,'lossfun',fun)