Categories &

Functions List

Class Definition: ClassificationPartitionedModel

statistics: CVMdl = ClassificationPartitionedModel (Mdl, Partition)

Create a ClassificationPartitionedModel class for cross-validation of classification models.

CVMdl = ClassificationPartitionedModel (Mdl, Partition) returns a ClassificationPartitionedModel object, with Mdl as the trained ClassificationKNN or ClassificationSVM object and Partition as the partitioning object obtained using cvpartition function.

A ClassificationPartitionedModel object, CVMdl, stores the classification models trained on cross-validated folds and various parameters for the cross-validated model, which can be accessed in the following fields:

FieldDescription
XUnstandardized predictor data, specified as a numeric matrix. Each column of X represents one predictor (variable), and each row represents one observation.
YClass labels, specified as a logical or numeric vector, or cell array of character vectors. Each value in Y is the observed class label for the corresponding row in X.
ClassNamesNames of the classes in the training data Y with duplicates removed, specified as a cell array of character vectors.
CostCost of the misclassification of a point, specified as a square matrix. Cost(i,j) is the cost of classifying a point into class j if its true class is i (that is, the rows correspond to the true class and the columns correspond to the predicted class). The order of the rows and columns in Cost corresponds to the order of the classes in ClassNames. The number of rows and columns in Cost is the number of unique classes in the response. By default, Cost(i,j) = 1 if i != j, and Cost(i,j) = 0 if i = j. In other words, the cost is 0 for correct classification and 1 for incorrect classification.
CrossValidatedModelClass of the cross-validated model, specified as a character vector. This field contains the type of model that was used for the training, e.g., "ClassificationKNN".
KFoldNumber of cross-validated folds, specified as a positive interger scalar. Represents how many folds the data was divided into for cross-validation purposes.
ModelParametersModel parameters used during training, specified as a structure. This includes any model-specific parameters that were configured prior to training, such as NumNeighbors or Distance in the case of a KNN model.
NumObservationsNumber of observations used in training the ClassificationKNN model, specified as a positive integer scalar. This number can be less than the number of rows in the training data because rows containing NaN values are not part of the fit.
PartitionPartition configuration used for cross-validation, specified as a cvpartition object. This field stores the cvpartition instance that describes how the data was split into training and validation sets.
PredictorNamesPredictor variable names, specified as a cell array of character vectors. The variable names are in the same order in which they appear in the training data X.
PriorPrior probabilities for each class, specified as a numeric vector. The order of the elements in Prior corresponds to the order of the classes in ClassNames.
ResponseNameResponse variable name, specified as a character vector.
TrainedModels trained on each fold, specified as a cell array of models. Each cell contains a model trained on the minus-one fold of the data (all but one fold used for training and the remaining fold used for validation).

See also: cvpartition, ClassificationDiscriminant, ClassificationGAM, ClassificationKNN, ClassificationNeuralNetwork, ClassificationSVM

Source Code: ClassificationPartitionedModel

Method: kfoldPredict

ClassificationPartitionedModel: label = kfoldPredict (CVMdl)
ClassificationPartitionedModel: [label, score, cost] = kfoldPredict (CVMdl)

Predict responses for observations not used for training in a cross-validated classification model.

[label, Score, Cost] = kfoldPredict (CVMdl) returns the predicted class labels, classification scores, and classification costs for the data used to train the cross-validated model CVMdl.

CVMdl is a ClassificationPartitionedModel object. The function predicts the response for each observation that was held out during training in the cross-validation process.

OutputDescription
labelPredicted class labels, returned as a vector or cell array. The type of label matches the type of Y in the original training data. Each element of label corresponds to the predicted class label for the corresponding row in X.
ScoreClassification scores, returned as a numeric matrix. Each row of Score corresponds to an observation, and each column corresponds to a class. The value in row i and column j is the classification score for class j for observation i.
CostClassification costs, returned as a numeric matrix. Each row of Cost corresponds to an observation, and each column corresponds to a class. The value in row i and column j is the classification cost for class j for observation i. This output is optional and only returned if requested.

See also: ClassificationKNN, ClassificationSVM, ClassificationPartitionedModel

Example: 1

 


 load fisheriris
 x = meas;
 y = species;

 ## Create a KNN classifier model
 obj = fitcknn (x, y, "NumNeighbors", 5, "Standardize", 1);

 ## Create a partition for 5-fold cross-validation
 partition = cvpartition (y, "KFold", 5);

 ## Create the ClassificationPartitionedModel object
 cvModel = crossval (obj, 'cvPartition', partition)

cvModel =

  ClassificationPartitionedModel object with properties:

                   BinEdges: [0x0 double]
      CategoricalPredictors: [0x0 double]
                 ClassNames: [3x1 cell]
                       Cost: [3x3 double]
        CrossValidatedModel: ClassificationKNN
                      KFold: [1x1 double]
            ModelParameters: [1x1 struct]
            NumObservations: [1x1 double]
                  Partition: [1x1 cvpartition]
             PredictorNames: [1x4 cell]
                      Prior: [3x1 double]
               ResponseName: Y
             ScoreTransform: none
                Standardize: 1
                    Trained: [5x1 cell]
                          X: [150x4 double]
                          Y: [150x1 cell]

                    

Example: 2

 


 load fisheriris
 x = meas;
 y = species;

 ## Create a KNN classifier model
 obj = fitcknn (x, y, "NumNeighbors", 5, "Standardize", 1);

 ## Create the ClassificationPartitionedModel object
 cvModel = crossval (obj);

 ## Predict the class labels for the observations not used for training
 [label, score, cost] = kfoldPredict (cvModel);
 fprintf ("Cross-validated accuracy = %1.2f%% (%d/%d)\n", ...
          sum (strcmp (label, y)) / numel (y) *100, ...
          sum (strcmp (label, y)), numel (y))

Cross-validated accuracy = 97.33% (146/150)