kfoldfun
Cross-validate function for classification
Syntax
Description
Examples
Estimate Classification Loss Using Custom Loss Function
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);
Mdl
is aClassificationTree
model.
Cross-validateMdl
using 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 asversicolor
is10
, and the cost of any other misclassification is1
. Create the custom functionnoversicolor
(shown at the end of this example). This function attributes a cost of10
for misclassifying a flower asversicolor
, and a cost of1
for any other misclassification.
Compute the mean misclassification error with thenoversicolor
cost.
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
CVMdl
—Cross-validated model
ClassificationPartitionedModel
object|ClassificationPartitionedEnsemble
object|ClassificationPartitionedGAM
object
Cross-validated model, specified as aClassificationPartitionedModel
object,ClassificationPartitionedEnsemble
object, orClassificationPartitionedGAM
object.
fun
—Cross-validated function
function handle
Cross-validated function, specified as a function handle.fun
语法:
testvals = fun(CMP,Xtrain,Ytrain,Wtrain,Xtest,Ytest,Wtest)
CMP
is a compact model stored in one element of theCVMdl
.Trained
property.Xtrain
is the training matrix of predictor values.Ytrain
is the training array of response values.Wtrain
are the training weights for observations.Xtest
andYtest
are the test data, with associated weightsWtest
.The returned value
testvals
must have the same size across all folds.
Data Types:function_handle
Output Arguments
vals
— Cross-validation results
numeric matrix
Cross-validation results, returned as a numeric matrix.vals
contains the arrays oftestvals
output, concatenated vertically over all folds. For example, iftestvals
from every fold is a numeric vector of lengthN
,kfoldfun
返回一个KFold
-by-N
numeric matrix with one row per fold.
Data Types:double
Extended Capabilities
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
This function fully supports GPU arrays for the following cross-validated model objects:
Ensemble classifier trained with
fitcensemble
k-nearest neighbor classifier trained with
fitcknn
Support vector machine classifier trained with
fitcsvm
Binary decision tree for multiclass classification trained with
fitctree
For more information, seeRun MATLAB Functions on a GPU(Parallel Computing Toolbox).
Version History
Beispiel öffnen
Sie haben eine geänderte Version dieses Beispiels. Möchten Sie dieses Beispiel mit Ihren Änderungen öffnen?
MATLAB-Befehl
Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:
Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
你can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)