Main Content

kfoldfun

Cross-validate function for regression

    Description

    example

    vals= kfoldfun(CVMdl,fun)cross-validates the functionfunby applyingfunto the data stored in the cross-validated modelCVMdl. You must passfunas a function handle.

    Examples

    collapse all

    Train a regression tree model, and then cross-validate it using a customk-fold loss function.

    Load theimports-85data set. Train a regression tree using a subset of the data.

    loadimports-85Mdl = fitrtree(X(:,[4 5]),X(:,16),...'PredictorNames',{'长度','Width'},...'ResponseName','Price');

    Cross-validate the regression tree, and obtain the mean squared error.

    CVMdl = crossval(Mdl); L = kfoldLoss(CVMdl)
    L = 1.9167e+07

    Examine the error when you use a simple averaging of training responses instead of predictions in the calculation.

    f = @(CMP,Xtrain,Ytrain,Wtrain,Xtest,Ytest,Wtest)...mean((Ytest-mean(Ytrain)).^2)
    f =function_handle with value:@(CMP,XTrain,Ytrain,WTRAIN,XTEST,YTEST,WTEST)平均值((ytest-incl-yan(YTrain))。^ 2)
    mean(kfoldfun(CVMdl,f))
    ans = 6.3586e+07

    Input Arguments

    collapse all

    Cross-validated model, specified as a回归PartitionedModel目的,回归PartitionedEnsemble目的,回归PartitionedGAM目的,or回归PartitionedSVMobject.

    Cross-validated function, specified as a function handle.funhas the syntax:

    testvals = fun(CMP,Xtrain,Ytrain,Wtrain,Xtest,Ytest,Wtest)
    • CMPis a compact model stored in one element of theCVMdl.Trainedproperty.

    • Xtrainis the training matrix of predictor values.

    • Ytrainis the training array of response values.

    • Wtrainare the training weights for observations.

    • XTest.andYtestare the test data, with associated weightsWtest.

    • The returned valuetestvalsmust have the same size across all folds.

    Data Types:function_handle

    Output Arguments

    collapse all

    Cross-validation results, returned as a numeric matrix.valscontains the arrays oftestvalsoutput, concatenated vertically over all folds. For example, iftestvals从每一个折叠都是长度的数字矢量N,kfoldfun返回一个KFold-by-Nnumeric matrix with one row per fold.

    Data Types:double

    Extended Capabilities

    Introduced in R2011a