Main Content

ClassificationPartitionedLinearECOC

Package:classreg.learning.partition
Superclasses:ClassificationPartitionedModel

Cross-validated linear error-correcting output codes model for multiclass classification of high-dimensional data

Description

ClassificationPartitionedLinearECOCis a set of error-correcting output codes (ECOC) models composed of linear classification models, trained on cross-validated folds. Estimate the quality of classification by cross-validation using one or more “kfold” functions:kfoldPredict,kfoldLoss,kfoldMargin, andkfoldEdge.

Every “kfold” method uses models trained on in-fold observations to predict the response for out-of-fold observations. For example, suppose that you cross-validate using five folds. In this case, the software randomly assigns each observation into five roughly equal-sized groups. Thetraining foldcontains four of the groups (that is, roughly 4/5 of the data) and thetest foldcontains the other group (that is, roughly 1/5 of the data). In this case, cross-validation proceeds as follows.

  1. The software trains the first model (stored inCVMdl.Trained{1}) using the observations in the last four groups and reserves the observations in the first group for validation.

  2. The software trains the second model (stored inCVMdl.Trained{2}) using the observations in the first group and last three groups. The software reserves the observations in the second group for validation.

  3. The software proceeds in a similar fashion for the third, fourth, and fifth models.

If you validate by callingkfoldPredict, it computes predictions for the observations in group 1 using the first model, group 2 for the second model, and so on. In short, the software estimates a response for every observation using the model trained without that observation.

Note

ClassificationPartitionedLinearECOCmodel objects do not store the predictor data set.

Construction

CVMdl = fitcecoc(X,Y,'Learners',t,Name,Value)returns a cross-validated, linear ECOC model when:

  • tis'Linear'or a template object returned bytemplateLinear.

  • Nameis one of'CrossVal','CVPartition','Holdout', or'KFold'.

For more details, seefitcecoc.

Properties

expand all

Cross-Validation Properties

旨在模型名称,指定为一个生产er vector.

For example,'ECOC'specifies a cross-validated ECOC model.

Data Types:char

Number of cross-validated folds, specified as a positive integer.

Data Types:double

交叉验证参数值,例如,这个名字-value pair argument values used to cross-validate the ECOC classifier, specified as an object.ModelParametersdoes not contain estimated parameters.

Access properties ofModelParametersusing dot notation.

Number of observations in the training data, specified as a positive numeric scalar.

Data Types:double

Data partition indicating how the software splits the data into cross-validation folds, specified as acvpartitionmodel.

在交叉验证fo紧凑的分类器训练lds, specified as a cell array ofCompactClassificationECOCmodels.Trainedhaskcells, wherekis the number of folds.

Data Types:cell

Observation weights used to cross-validate the model, specified as a numeric vector.WhasNumObservationselements.

The software normalizes the weights used for training so thatsum(W,'omitnan')is1.

Data Types:single|double

Observed class labels used to cross-validate the model, specified as a categorical or character array, logical or numeric vector, or cell array of character vectors.YhasNumObservationselements, and is the same data type as the input argumentYthat you passed tofitcecocto cross-validate the model.(The software treats string arrays as cell arrays of character vectors.)

Each row ofYrepresents the observed classification of the observation in the predictor data.

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

ECOC Properties

This property is read-only.

Binary learner loss function, specified as a character vector representing the loss function name.

By default, if all binary learners are linear classification models using SVM, thenBinaryLossis'hinge'. If all binary learners are linear classification models using logistic regression, thenBinaryLossis'quadratic'. To potentially increase accuracy, specify a binary loss function other than the default during a prediction or loss computation by using theBinaryLossname-value argument ofkfoldPredictorkfoldLoss.

For the list of supported binary loss functions, seeBinary Loss.

Data Types:char

Binary learner class labels, specified as a numeric matrix or[].

  • If the coding matrix is the same across folds, thenBinaryYis aNumObservations-by-Lmatrix, whereLis the number of binary learners (size(CodingMatrix,2)).

    Elements ofBinaryYare-1,0, or1, and the value corresponds to a dichotomous class assignment. This table describes how learnerjassigns observationkto a dichotomous class corresponding to the value ofBinaryY(k,j).

    Value Dichotomous Class Assignment
    –1 Learnerjassigns observationkto a negative class.
    0 Before training, learnerjremoves observationkfrom the data set.
    1 Learnerjassigns observationkto a positive class.

  • If the coding matrix varies across folds, thenBinaryYis empty ([]).

Data Types:double

Codes specifying class assignments for the binary learners, specified as a numeric matrix or[].

  • If the coding matrix is the same across folds, thenCodingMatrixis aK-by-Lmatrix.Kis the number of classes andLis the number of binary learners.

    Elements ofCodingMatrixare-1,0, or1, and the value corresponds to a dichotomous class assignment. This table describes how learnerjassigns observations in classito a dichotomous class corresponding to the value ofCodingMatrix(i,j).

    Value Dichotomous Class Assignment
    –1 Learnerjassigns observations in classito a negative class.
    0 Before training, learnerjremoves observations in classifrom the data set.
    1 Learnerjassigns observations in classito a positive class.

  • If the coding matrix varies across folds, thenCodingMatrixis empty ([]). Obtain the coding matrix for each fold using theTrainedproperty. For example,CVMdl.Trained{1}.CodingMatrixis the coding matrix in the first fold of the cross-validated ECOC modelCVMdl.

Data Types:double|single|int8|int16|int32|int64

Other Classification Properties

Categorical predictor indices, specified as a vector of positive integers.CategoricalPredictorscontains index values indicating that the corresponding predictors are categorical. The index values are between 1 andp, wherepis the number of predictors used to train the model. If none of the predictors are categorical, then this property is empty ([]).

Data Types:single|double

Unique class labels used in training, specified as a categorical or character array, logical or numeric vector, or cell array of character vectors.ClassNameshas the same data type as the class labelsY.(The software treats string arrays as cell arrays of character vectors.)ClassNamesalso determines the class order.

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

This property is read-only.

Misclassification costs, specified as a square numeric matrix.CosthasKrows and columns, whereKis the number of classes.

Cost(i,j)is the cost of classifying a point into classjif its true class isi. The order of the rows and columns ofCostcorresponds to the order of the classes inClassNames.

Data Types:double

Predictor names in order of their appearance in the predictor data, specified as a cell array of character vectors. The length ofPredictorNamesis equal to the number of variables in the training dataXorTblused as predictor variables.

Data Types:cell

This property is read-only.

Prior class probabilities, specified as a numeric vector.Priorhas as many elements as the number of classes inClassNames, and the order of the elements corresponds to the order of the classes inClassNames.

fitcecocincorporates misclassification costs differently among different types of binary learners.

Data Types:double

Response variable name, specified as a character vector.

Data Types:char

Score transformation function to apply to predicted scores, specified as a function name or function handle.

For linear classification models and before transformation, the predicted classification score for the observationx(row vector) isf(x) =xβ+b, whereβandbcorrespond toMdl.BetaandMdl.Bias, respectively.

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

  • For a built-in function, enter this code and replacefunctionwith a value in the table.

    Mdl.ScoreTransform = 'function';

    Value Description
    "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“身份” x(no transformation)
    "sign" –1 forx< 0
    0 forx= 0
    1 forx> 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;

    functionmust accept a matrix of the original scores for each class, and then return a matrix of the same size representing the transformed scores for each class.

Data Types:char|function_handle

Methods

kfoldEdge 分类边观察t used for training
kfoldLoss Classification loss for observations not used in training
kfoldMargin Classification margins for observations not used in training
kfoldPredict Predict labels for observations not used for training

Copy Semantics

Value. To learn how value classes affect copy operations, seeCopying Objects.

Examples

collapse all

Load the NLP data set.

loadnlpdata

Xis a sparse matrix of predictor data, andYis a categorical vector of class labels.

Cross-validate a multiclass, linear classification model that can identify which MATLAB® toolbox a documentation web page is from based on counts of words on the page.

rng(1);% For reproducibilityCVMdl = fitcecoc(X,Y,'Learners','linear','CrossVal','on')
CVMdl = ClassificationPartitionedLinearECOC CrossValidatedModel: 'LinearECOC' ResponseName: 'Y' NumObservations: 31572 KFold: 10 Partition: [1x1 cvpartition] ClassNames: [comm dsp ecoder fixedpoint ... ] ScoreTransform: 'none' Properties, Methods

CVMdlis aClassificationPartitionedLinearECOCcross-validated model. Becausefitcecocimplements 10-fold cross-validation by default,CVMdl.Trainedcontains a 10-by-1 cell vector of tenCompactClassificationECOCmodels that contain the results of training ECOC models composed of binary, linear classification models for each of the folds.

Estimate labels for out-of-fold observations and estimate the generalization error by passingCVMdltokfoldPredictandkfoldLoss, respectively.

oofLabels = kfoldPredict(CVMdl); ge = kfoldLoss(CVMdl)
ge = 0.0958

The estimated generalization error is about 10% misclassified observations.

To improve generalization error, try specifying another solver, such as LBFGS. To change default options when training ECOC models composed of linear classification models, create a linear classification model template usingtemplateLinear, and then pass the template tofitcecoc.

To determine a good lasso-penalty strength for an ECOC model composed of linear classification models that use logistic regression learners, implement 5-fold cross-validation.

Load the NLP data set.

loadnlpdata

Xis a sparse matrix of predictor data, andYis a categorical vector of class labels.

For simplicity, use the label 'others' for all observations inYthat are not'simulink','dsp', or'comm'.

Y(~(ismember(Y,{'simulink','dsp','comm'}))) ='others';

Create a set of 11 logarithmically-spaced regularization strengths from 1 0 - 7 through 1 0 - 2 .

Lambda = logspace(-7,-2,11);

Create a linear classification model template that specifies to use logistic regression learners, use lasso penalties with strengths inLambda, train using SpaRSA, and lower the tolerance on the gradient of the objective function to1e-8.

t = templateLinear('Learner','logistic','Solver','sparsa',...'Regularization','lasso','Lambda',Lambda,'GradientTolerance',1e-8);

Cross-validate the models. To increase execution speed, transpose the predictor data and specify that the observations are in columns.

X = X'; rng(10);% For reproducibilityCVMdl = fitcecoc(X,Y,'Learners',t,'ObservationsIn','columns','KFold',5);

CVMdlis aClassificationPartitionedLinearECOCmodel.

DissectCVMdl, and each model within it.

numECOCModels = numel(CVMdl.Trained)
numECOCModels = 5
ECOCMdl1 = CVMdl.Trained{1}
ECOCMdl1 = CompactClassificationECOC ResponseName: 'Y' ClassNames: [comm dsp simulink others] ScoreTransform: 'none' BinaryLearners: {6×1 cell} CodingMatrix: [4×6 double] Properties, Methods
numCLModels = numel(ECOCMdl1.BinaryLearners)
numCLModels = 6
CLMdl1 = ECOCMdl1.BinaryLearners{1}
CLMdl1 = ClassificationLinear ResponseName: 'Y' ClassNames: [-1 1] ScoreTransform: 'logit' Beta: [34023×11 double] Bias: [-0.3169 -0.3169 -0.3168 -0.3168 -0.3168 -0.3167 -0.1725 -0.0805 -0.1762 -0.3450 -0.5174] Lambda: [1.0000e-07 3.1623e-07 1.0000e-06 3.1623e-06 1.0000e-05 3.1623e-05 1.0000e-04 3.1623e-04 1.0000e-03 0.0032 0.0100] Learner: 'logistic' Properties, Methods

Becausefitcecocimplements 5-fold cross-validation,CVMdlcontains a 5-by-1 cell array ofCompactClassificationECOCmodels that the software trains on each fold. TheBinaryLearnersproperty of eachCompactClassificationECOCmodel contains theClassificationLinearmodels. The number ofClassificationLinearmodels within each compact ECOC model depends on the number of distinct labels and coding design. BecauseLambdais a sequence of regularization strengths, you can think ofCLMdl1as 11 models, one for each regularization strength inLambda.

Determine how well the models generalize by plotting the averages of the 5-fold classification error for each regularization strength. Identify the regularization strength that minimizes the generalization error over the grid.

ce = kfoldLoss(CVMdl); figure; plot(log10(Lambda),log10(ce)) [~,minCEIdx] = min(ce); minLambda = Lambda(minCEIdx); holdonplot(log10(minLambda),log10(ce(minCEIdx)),'ro'); ylabel('log_{10} 5-fold classification error') xlabel('log_{10} Lambda') legend('MSE','Min classification error') holdoff

Train an ECOC model composed of linear classification model using the entire data set, and specify the minimal regularization strength.

t = templateLinear('Learner','logistic','Solver','sparsa',...'Regularization','lasso','Lambda',minLambda,'GradientTolerance',1e-8); MdlFinal = fitcecoc(X,Y,'Learners',t,'ObservationsIn','columns');

To estimate labels for new observations, passMdlFinaland the new data topredict.

Version History

Introduced in R2016a