Main Content

kfoldfun

Cross-validate function for classification

    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 classification tree classifier, and then cross-validate it using a customk-fold loss function.

    Load Fisher’s iris data set.

    loadfisheriris

    Train a classification tree classifier.

    Mdl = fitctree(meas,species);

    Mdlis aClassificationTreemodel.

    Cross-validateMdlusing the default 10-fold cross-validation. Compute the classification error (proportion of misclassified observations) for the validation-fold observations.

    rng(1);% For reproducibilityCVMdl = crossval(Mdl); L = kfoldLoss(CVMdl,'LossFun','classiferror')
    L = 0.0467

    Examine the result when the cost of misclassifying a flower asversicoloris10, and the cost of any other misclassification is1. Create the custom functionnoversicolor(shown at the end of this example). This function attributes a cost of10for misclassifying a flower asversicolor, and a cost of1for any other misclassification.

    Compute the mean misclassification error with thenoversicolorcost.

    mean(kfoldfun(CVMdl,@noversicolor))
    ans = 0.2267

    This code creates the functionnoversicolor.

    functionaverageCost =也不要sicolor(CMP,~,~,~,Xtest,Ytest,~)% noversicolor Example custom cross-validation function% Attributes a cost of 10 for misclassifying versicolor irises, and 1 for% the other irises. This example function requires the fisheriris data% set.Ypredict = predict(CMP,Xtest); misclassified = not(strcmp(Ypredict,Ytest));% Different resultclassifiedAsVersicolor = strcmp(Ypredict,'versicolor');% Index of bad decisionscost = sum(misclassified) +...9*sum(misclassified & classifiedAsVersicolor);% Total differencesaverageCost = cost/numel(Ytest);% Average errorend

    Input Arguments

    collapse all

    Cross-validated model, specified as aClassificationPartitionedModelobject,ClassificationPartitionedEnsembleobject, orClassificationPartitionedGAMobject.

    Cross-validated function, specified as a function handle.fun语法:

    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.

    • XtestandYtestare 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, iftestvalsfrom every fold is a numeric vector of lengthN,kfoldfun返回一个KFold-by-Nnumeric matrix with one row per fold.

    Data Types:double

    Extended Capabilities

    Version History

    Introduced in R2011a