Categories &

Functions List

Function Reference: fitcecoc

statistics: Mdl = fitcecoc (X, Y)

statistics: Mdl = fitcecoc (Tbl, ResponseVarName)

statistics: Mdl = fitcecoc (Tbl, formula)

statistics: Mdl = fitcecoc (Tbl, Y)

statistics: Mdl = fitcecoc (…, name, value)

Fit a multiclass model from binary learners.

Mdl = fitcecoc (X, Y) turns the multiclass problem in X and Y into a set of two class problems, fits one binary learner to each, and returns them as a ClassificationECOC object. A coding matrix says which classes each learner calls +1, which it calls -1, and which sit it out; a new observation is sent to every learner and given the class whose row of that matrix its scores match most closely.

  • X must be a NxP numeric matrix of predictor data.
  • Y must be a Nx1 vector of class labels, of any type ClassNames accepts, with at least two distinct values.
NameValue
'Learners'The binary learner, either a name, 'svm' (default), 'tree', 'knn', 'naivebayes', 'discriminant', 'linear', 'kernel' or 'ensemble', a LogitBoost ensemble of 100 trees, or a template from templateSVM and its siblings, templateEnsemble among them, which also carries the options that learner is to be fitted with.
'Coding'The coding design, either a name designecoc accepts, 'onevsone' by default, or a coding matrix given outright, which sets CodingName to 'custom'.
'BinaryLoss'The loss the binary scores are read with. The default follows the learner: 'exponential' for an AdaBoostM1 or GentleBoost ensemble, 'binodeviance' for a LogitBoost one, and otherwise 'hinge' for one scoring on (-Inf,+Inf) and 'quadratic' for one scoring on [0,1], as a bagged, random subspace or RUSBoost ensemble does.
'ClassNames'The classes to fit, and the order the rows of the coding matrix, Prior and Cost take them in. By default they are sorted.
'Cost'A KxK matrix of misclassification costs. The default is 1 - eye (K).
'Prior''empirical' (default), 'uniform', or a vector with one element per class.
'Weights'A nonnegative single or double vector with one element per observation. The default is uniform. The model’s W keeps the class of the weights, while every computation runs in double, so Prior is double where MATLAB returns single.
'CategoricalPredictors'The predictors whose values are levels, as indices, a logical vector or 'all', passed as given to every binary learner, which codes them its own way; a nearest neighbour learner takes only 'all' and a discriminant learner none. A predictor may be named rather than indexed, as a character matrix of one padded name per row, a string array or a cellstr; a name must match an entry of 'PredictorNames' exactly, its case included.
'PredictorNames'A cellstr of predictor names.
'ResponseName'The name of the response variable.
'ScoreTransform'A transform applied to the returned scores. The default is 'none'.

A linear or kernel learner carries no training data of its own, so fitcecoc returns a CompactClassificationECOC for those two and a ClassificationECOC for the other five, which is what R2024a does.

'FitPosterior' is refused rather than quietly ignored: it installs a fitted score transform on each binary learner, which needs the posterior fitting this package does not have yet.

An ensemble template may name any classification method of fitcensemble; a regression template is refused, as in MATLAB.

A RUSBoost ensemble scores each class with the weighted sum of its trees’ class probabilities, which no binary loss can read: MATLAB sets the loss to 'unknown' and then cannot predict. Here those scores, and the binary scores predict returns, are divided by the total weight of the trees, which gives the weighted mean of their class probabilities, and are read with 'quadratic' as a bagged ensemble’s are. This follows R’s ebmc and adabag packages and scikit-learn, whose AdaBoostClassifier and imbalanced-learn’s RUSBoostClassifier built on it scale a boosted ensemble’s weighted votes the same way.

A binary learner that cannot be fitted, such as an AdaBoostM2 ensemble, which needs three classes, stops the fit with its own error, where MATLAB warns and predicts the majority class. An AdaBoostM1 learner whose first tree separates its two classes keeps that tree, as fitcensemble does; MATLAB keeps no tree and never predicts those classes from it.

Each binary learner is fitted with the weight its observations carry, which the prior and any 'Weights' decide.

See also: ClassificationECOC, CompactClassificationECOC, designecoc, templateSVM, templateTree

Source Code: fitcecoc

Fit from a table, and predict on one

 load fisheriris
 T = table (meas(:,1), meas(:,2), meas(:,3), meas(:,4), ...
            'VariableNames', {'SL', 'SW', 'PL', 'PW'});
 T.Species = categorical (species);

A column holding levels is a categorical predictor without being named one

 T.Wide = categorical (meas(:,2) > 3, [false true], {'narrow', 'wide'});

The response is named by its column, and everything else is a predictor

 Mdl = fitcecoc (T, 'Species');
 Mdl.PredictorNames
ans =
  1x5 cell array

    {'SL'}    {'SW'}    {'PL'}    {'PW'}    {'Wide'}
 Mdl.CategoricalPredictors
ans = 5

A model formula names them instead, holding main effects only

 Mdl2 = fitcecoc (T, 'Species ~ PL + PW');
 Mdl2.PredictorNames
ans =
  1x2 cell array

    {'PL'}    {'PW'}

predict reads a table by the names the model was fitted on, so the columns may come in any order

 label = predict (Mdl, T(1:5, [6, 5, 4, 3, 2, 1]));
 label'
ans =
  1x5 categorical array

    setosa    setosa    setosa    setosa    setosa