Contenido principal

ClassificationPartitionedNeuralNetwork

Cross-validated classification neural network

Since R2026a

    Description

    ClassificationPartitionedNeuralNetwork is a set of classification neural network models trained on cross-validated folds. You can estimate the quality of the classification by using one or more kfold functions: kfoldPredict, kfoldLoss, kfoldMargin, kfoldEdge, and kfoldfun.

    Each kfold function uses models trained on training-fold (in-fold) observations to predict the response for validation-fold (out-of-fold) observations. For example, when you use kfoldPredict with a k-fold cross-validated model, the software estimates a response for every observation using the model trained without that observation. For more information, see Partitioned Models.

    Creation

    You can create a ClassificationPartitionedNeuralNetwork object in two ways:

    • Create a cross-validated model from a full classification model object by using the crossval object function.

    • Create a cross-validated model by using the fitcnet function and specifying one of the name-value arguments CrossVal, KFold, Holdout, Leaveout, or CVPartition.

    Properties

    expand all

    Cross-Validation Properties

    This property is read-only.

    Cross-validated model name, returned as 'NeuralNetwork'.

    Data Types: char

    This property is read-only.

    Number of folds in the cross-validated model, returned as a positive integer.

    Data Types: double

    This property is read-only.

    Cross-validation parameter values, returned as an EnsembleParams object. The parameter values correspond to the values of the name-value arguments used to cross-validate the neural network model. ModelParameters does not contain estimated parameters.

    You can access the properties of ModelParameters using dot notation.

    This property is read-only.

    Partition used in the cross-validation, returned as a cvpartition object.

    This property is read-only.

    Compact models trained on the cross-validation folds, returned as a cell array of CompactClassificationNeuralNetwork model objects. Trained has k cells, where k is the number of folds.

    Data Types: cell

    Other Classification Properties

    This property is read-only.

    Categorical predictor indices, returned as a vector of positive integers. CategoricalPredictors contains index values indicating that the corresponding predictors are categorical. The index values are between 1 and p, where p is the number of predictors used to train the model. If none of the predictors are categorical, then this property is empty ([]).

    Data Types: double

    This property is read-only.

    Unique class labels used in training, returned as a categorical or character array, logical or numeric vector, or cell array of character vectors. ClassNames has the same data type as the class labels Y. (The software treats string arrays as cell arrays of character vectors.) ClassNames also determines the class order.

    Data Types: categorical | char | logical | single | double | cell

    Misclassification costs, specified as a square numeric matrix. Cost has K rows and columns, where K is the number of classes.

    Cost(i,j) is the cost of classifying a point into class j if its true class is i. The order of the rows and columns of Cost corresponds to the order of the classes in ClassNames.

    You can change the cost matrix using dot notation. For example, for a cross-validated model CVMdl and a cost matrix costMatrix, you can specify:

    CVMdl.Cost = costMatrix;

    Data Types: double

    This property is read-only.

    Number of observations in the training data stored in X and Y, returned as a positive integer scalar.

    Data Types: double

    This property is read-only.

    Predictor variable names, returned as a cell array of character vectors. The order of the elements in PredictorNames corresponds to the order in which the predictor names appear in the training data.

    Data Types: cell

    This property is read-only.

    Prior probabilities for each class, returned as a numeric vector. The order of the elements of Prior corresponds to the order of the classes in ClassNames.

    Data Types: double

    This property is read-only.

    Response variable name, returned as a character vector.

    Data Types: char

    Score transformation function, specified as a character vector, string scalar, or function handle. ScoreTransform represents a built-in transformation function or a function handle for transforming predicted classification scores.

    To change the score transformation function to function, for example, use dot notation.

    • For a built-in function, enter a character vector or string scalar.

      Mdl.ScoreTransform = "function";

      This table lists the values for the available built-in functions.

      ValueDescription
      "doublelogit"1/(1 + e–2x)
      "invlogit"log(x / (1 – x))
      "ismax"Sets the score for the class with the largest score to 1, and sets the scores for all other classes to 0
      "logit"1/(1 + ex)
      "none" or "identity"x (no transformation)
      "sign"–1 for x < 0
      0 for x = 0
      1 for x > 0
      "symmetric"2x – 1
      "symmetricismax"Sets the score for the class with the largest score to 1, and sets the scores for all other classes to –1
      "symmetriclogit"2/(1 + ex) – 1

    • For a MATLAB® function or a function that you define, enter its function handle.

      Mdl.ScoreTransform = @function;

      function must accept a matrix (the original scores) and return a matrix of the same size (the transformed scores).

    Data Types: char | string | function_handle

    This property is read-only.

    Observation weights, returned as an n-by-1 numeric vector, where n is the number of observations (NumObservations).

    Data Types: double

    This property is read-only.

    Unstandardized predictors used to cross-validate the model, returned as a numeric matrix or a table. X retains its original orientation, with observations in rows or columns depending on the value of the ObservationsIn name-value argument in the call to fitcnet.

    Data Types: single | double | table

    This property is read-only.

    Class labels used to cross-validate the model, returned as a numeric vector, logical vector, character array, cell array of character vectors, or categorical array. Each row of Y represents the classification of the corresponding observation in X.

    Data Types: single | double | logical | char | cell | categorical

    Object Functions

    gatherGather properties of Statistics and Machine Learning Toolbox object from GPU
    kfoldEdgeClassification edge for cross-validated classification model
    kfoldLossClassification loss for cross-validated classification model
    kfoldMarginClassification margins for cross-validated classification model
    kfoldPredictClassify observations in cross-validated classification model
    kfoldfunCross-validate function for classification

    Examples

    collapse all

    Cross-validate a neural network classifier using the default 10-fold cross-validation.

    Load the ionosphere data set, which includes radar signal data. X contains the predictor data, and Y is the response variable, whose values represent either good ("g") or bad ("b") radar signals.

    load ionosphere

    Train a neural network classifier using the fitcnet function. Specify 35 outputs for the first fully connected layer and 20 outputs for the second fully connected layer.

    Mdl = fitcnet(X,Y,LayerSizes=[35 20])
    Mdl = 
      ClassificationNeuralNetwork
                 ResponseName: 'Y'
        CategoricalPredictors: []
                   ClassNames: {'b'  'g'}
               ScoreTransform: 'none'
              NumObservations: 351
                   LayerSizes: [35 20]
                  Activations: 'relu'
        OutputLayerActivation: 'softmax'
                       Solver: 'LBFGS'
              ConvergenceInfo: [1×1 struct]
              TrainingHistory: [72×7 table]
    
    
      Properties, Methods
    
    

    Mdl is a trained ClassificationNeuralNetwork model object.

    Cross-validate the classifier using 10-fold cross-validation. During cross-validation, the software completes these steps:

    1. Randomly partition the data into 10 sets (folds) of equal size.

    2. For each set, reserve the set as validation data, and train a neural network classifier on the other nine sets.

    3. Store the 10 trained models in a 10-by-1 cell vector in the Trained property of the cross-validated model object.

    CVMdl = crossval(Mdl)
    CVMdl = 
      ClassificationPartitionedNeuralNetwork
        CrossValidatedModel: 'NeuralNetwork'
             PredictorNames: {'x1'  'x2'  'x3'  'x4'  'x5'  'x6'  'x7'  'x8'  'x9'  'x10'  'x11'  'x12'  'x13'  'x14'  'x15'  'x16'  'x17'  'x18'  'x19'  'x20'  'x21'  'x22'  'x23'  'x24'  'x25'  'x26'  'x27'  'x28'  'x29'  'x30'  'x31'  'x32'  'x33'  'x34'}
               ResponseName: 'Y'
            NumObservations: 351
                      KFold: 10
                  Partition: [1×1 cvpartition]
                 ClassNames: {'b'  'g'}
             ScoreTransform: 'none'
    
    
      Properties, Methods
    
    

    CVMdl is a ClassificationPartitionedNeuralNetwork object.

    Display the first model in CVMdl.Trained.

    FirstModel = CVMdl.Trained{1}
    FirstModel = 
      CompactClassificationNeuralNetwork
                   LayerSizes: [35 20]
                  Activations: 'relu'
        OutputLayerActivation: 'softmax'
    
    
      Properties, Methods
    
    

    FirstModel is the first of the 10 trained models contained in CVMdl. The trained models are all CompactClassificationNeuralNetwork objects.

    After creating a cross-validated neural network model object, you can estimate the generalized classification error by passing the model to kfoldLoss.

    Algorithms

    expand all

    Extended Capabilities

    expand all

    Version History

    Introduced in R2026a