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

    %% Cross-Validate SVM Regression Model 
%%
% Load the |carsmall| data set.
load carsmall
rng 'default'  % For reproducibility
%%
% Specify |Horsepower| and |Weight| as the predictor variables (|X|) and
% |MPG| as the response variable (|Y|).
X = [Horsepower Weight];
Y = MPG;
%%
% Cross-validate two SVM regression models using 5-fold cross-validation.
% For both models, specify to standardize the predictors.  For one of the
% models, specify to train using the default linear kernel, and the
% Gaussian kernel for the other model. 
MdlLin = fitrsvm(X,Y,'Standardize',true,'KFold',5)
MdlGau = fitrsvm(X,Y,'Standardize',true,'KFold',5,'KernelFunction','gaussian')
MdlLin.Trained
%%
% |MdlLin| and |MdlGau| are |RegressionPartitionedSVM| cross-validated
% models.  The |Trained| property of each model is a 5-by-1 cell array of
% |CompactRegressionSVM| models.  The models in the cell store the results
% of training on 4 folds of observations, and leaving one fold of
% observations out.
%%
% Compare the generalization error of the models.  In this case, the
% generalization error is the out-of-sample mean-squared error.
mseLin = kfoldLoss(MdlLin)
mseGau = kfoldLoss(MdlGau)
%%
% The SVM regression model using the Gaussian kernel performs better than
% the one using the linear kernel.
%%
% Create a model suitable for making predictions by passing the entire data
% set to |fitrsvm|, and specify all name-value pair arguments that yielded
% the better-performing model. However, do not specify any cross-validation
% options.
MdlGau = fitrsvm(X,Y,'Standardize',true,'KernelFunction','gaussian');
%%
% To predict the MPG of a set of cars, pass |Mdl| and a table containing
% the horsepower and weight measurements of the cars to |predict|.