compact multiclass model for support vector machines (svms) and other classifiers -凯发k8网页登录
compact multiclass model for support vector machines (svms) and other classifiers
description
compactclassificationecoc
is a compact version of the multiclass
error-correcting output codes (ecoc) model. the compact classifier does not include the data
used for training the multiclass ecoc model. therefore, you cannot perform certain tasks, such
as cross-validation, using the compact classifier. use a compact multiclass ecoc model for
tasks such as classifying new data (predict
).
creation
you can create a compactclassificationecoc
model in two ways:
create a compact ecoc model from a trained
classificationecoc
model by using the object function.create a compact ecoc model by using the
fitcecoc
function and specifying the'learners'
name-value pair argument as'linear'
,'kernel'
, atemplatelinear
or object, or a cell array of such objects.
properties
after you create a compactclassificationecoc
model object, you can use
dot notation to access its properties. for an example, see train and cross-validate ecoc classifier.
ecoc properties
binarylearners
— trained binary learners
cell vector of model objects
trained binary learners, specified as a cell vector of model objects. the number of binary
learners depends on the number of classes in y
and the coding
design.
the software trains binarylearner{j}
according to the binary problem
specified by codingmatrix
(:,j)
. for example, for
multiclass learning using svm learners, each element of
binarylearners
is a compactclassificationsvm
classifier.
data types: cell
binaryloss
— binary learner loss function
'binodeviance'
| 'exponential'
| 'hamming'
| 'hinge'
| 'linear'
| 'logit'
| 'quadratic'
binary learner loss function, specified as a character vector representing the loss function name.
this table identifies the default binaryloss
value, which depends on the
score ranges returned by the binary learners.
assumption | default value |
---|---|
all binary learners are any of the following:
| 'quadratic' |
all binary learners are svms or linear or kernel classification models of svm learners. | 'hinge' |
all binary learners are ensembles trained by
adaboostm1 or
gentleboost . | 'exponential' |
all binary learners are ensembles trained by
logitboost . | 'binodeviance' |
you specify to predict class posterior probabilities by setting
'fitposterior',true in fitcecoc . | 'quadratic' |
binary learners are heterogeneous and use different loss functions. | 'hamming' |
to check the default value, use dot notation to display the binaryloss
property of the trained model at the command line.
to potentially increase accuracy, specify a binary loss function other than the
default during a prediction or loss computation by using the
binaryloss
name-value argument of predict
or . for more information, see binary loss.
data types: char
codingmatrix
— class assignment codes
numeric matrix
class assignment codes for the binary learners, specified as a numeric matrix.
codingmatrix
is a k-by-l
matrix, where k is the number of classes and l is
the number of binary learners.
the elements of codingmatrix
are –1
,
0
, and 1
, and the values correspond to
dichotomous class assignments. this table describes how learner j
assigns observations in class i
to a dichotomous class corresponding
to the value of codingmatrix(i,j)
.
value | dichotomous class assignment |
---|---|
–1 | learner j assigns observations in class i to a negative
class. |
0 | before training, learner j removes observations
in class i from the data set. |
1 | learner j assigns observations in class i to a positive
class. |
data types: double
| single
| int8
| int16
| int32
| int64
learnerweights
— binary learner weights
numeric row vector
binary learner weights, specified as a numeric row vector. the length of
learnerweights
is equal to the
number of binary learners
(length(mdl.binarylearners)
).
learnerweights(j)
is the sum of the observation weights that binary learner
j
uses to train its classifier.
the software uses learnerweights
to fit posterior probabilities by
minimizing the kullback-leibler divergence. the software ignores
learnerweights
when it uses the
quadratic programming method of estimating posterior
probabilities.
data types: double
| single
other classification properties
categoricalpredictors
— categorical predictor indices
vector of positive integers | []
categorical predictor
indices, specified 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: single
| double
classnames
— unique class labels
categorical array | character array | logical vector | numeric vector | cell array of character vectors
unique class labels used in training, specified 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
cost
— misclassification costs
square numeric matrix
this property is read-only.
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
.
data types: double
predictornames
— predictor names
cell array of character vectors
predictor names in order of their appearance in the predictor data, specified as a
cell array of character vectors. the length of predictornames
is
equal to the number of variables in the training data x
or
tbl
used as predictor variables.
data types: cell
expandedpredictornames
— expanded predictor names
cell array of character vectors
expanded predictor names, specified as a cell array of character vectors.
if the model uses encoding for categorical variables, then
expandedpredictornames
includes the names that describe the
expanded variables. otherwise, expandedpredictornames
is the same as
predictornames
.
data types: cell
prior
— prior class probabilities
numeric vector
this property is read-only.
prior class probabilities, specified as a numeric vector. prior
has as
many elements as the number of classes in
classnames
, and the order of
the elements corresponds to the order of the classes in
classnames
.
fitcecoc
incorporates misclassification
costs differently among different types of binary learners.
data types: double
responsename
— response variable name
character vector
response variable name, specified as a character vector.
data types: char
scoretransform
— score transformation function to apply to predicted scores
'none'
this property is read-only.
score transformation function to apply to the predicted scores, specified as
'none'
. an ecoc model does not support score transformation.
object functions
compare accuracies of two classification models using new data | |
discard support vectors of linear svm binary learners in ecoc model | |
classification edge for multiclass error-correcting output codes (ecoc) model | |
gather properties of statistics and machine learning toolbox object from gpu | |
convert multiclass error-correcting output codes (ecoc) model to incremental learner | |
lime | local interpretable model-agnostic explanations (lime) |
classification loss for multiclass error-correcting output codes (ecoc) model | |
classification margins for multiclass error-correcting output codes (ecoc) model | |
partialdependence | compute partial dependence |
plotpartialdependence | create partial dependence plot (pdp) and individual conditional expectation (ice) plots |
predict | classify observations using multiclass error-correcting output codes (ecoc) model |
shapley | shapley values |
choose subset of multiclass ecoc models composed of binary
classificationlinear learners | |
update | update model parameters for code generation |
examples
reduce size of full ecoc model
reduce the size of a full ecoc model by removing the training data. full ecoc models (classificationecoc
models) hold the training data. to improve efficiency, use a smaller classifier.
load fisher's iris data set. specify the predictor data x
, the response data y
, and the order of the classes in y
.
load fisheriris
x = meas;
y = categorical(species);
classorder = unique(y);
train an ecoc model using svm binary classifiers. standardize the predictor data using an svm template t
, and specify the order of the classes. during training, the software uses default values for empty options in t
.
t = templatesvm('standardize',true); mdl = fitcecoc(x,y,'learners',t,'classnames',classorder);
mdl
is a classificationecoc
model.
reduce the size of the ecoc model.
compactmdl = compact(mdl)
compactmdl = compactclassificationecoc responsename: 'y' categoricalpredictors: [] classnames: [setosa versicolor virginica] scoretransform: 'none' binarylearners: {3x1 cell} codingmatrix: [3x3 double] properties, methods
compactmdl
is a compactclassificationecoc
model. compactmdl
does not store all of the properties that mdl
stores. in particular, it does not store the training data.
display the amount of memory each classifier uses.
whos('compactmdl','mdl')
name size bytes class attributes compactmdl 1x1 15116 classreg.learning.classif.compactclassificationecoc mdl 1x1 28357 classificationecoc
the full ecoc model (mdl
) is approximately double the size of the compact ecoc model (compactmdl
).
to label new observations efficiently, you can remove mdl
from the matlab® workspace, and then pass compactmdl
and new predictor values to predict
.
train and cross-validate ecoc classifier
train and cross-validate an ecoc classifier using different binary learners and the one-versus-all coding design.
load fisher's iris data set. specify the predictor data x
and the response data y
. determine the class names and the number of classes.
load fisheriris x = meas; y = species; classnames = unique(species(~strcmp(species,''))) % remove empty classes
classnames = 3x1 cell
{'setosa' }
{'versicolor'}
{'virginica' }
k = numel(classnames) % number of classes
k = 3
you can use classnames
to specify the order of the classes during training.
for a one-versus-all coding design, this example has k
= 3 binary learners. specify templates for the binary learners such that:
binary learner 1 and 2 are naive bayes classifiers. by default, each predictor is conditionally, normally distributed given its label.
binary learner 3 is an svm classifier. specify to use the gaussian kernel.
rng(1); % for reproducibility tnb = templatenaivebayes(); tsvm = templatesvm('kernelfunction','gaussian'); tlearners = {tnb tnb tsvm};
tnb
and tsvm
are template objects for naive bayes and svm learning, respectively. the objects indicate which options to use during training. most of their properties are empty, except those specified by name-value pair arguments. during training, the software fills in the empty properties with their default values.
train and cross-validate an ecoc classifier using the binary learner templates and the one-versus-all coding design. specify the order of the classes. by default, naive bayes classifiers use posterior probabilities as scores, whereas svm classifiers use distances from the decision boundary. therefore, to aggregate the binary learners, you must specify to fit posterior probabilities.
cvmdl = fitcecoc(x,y,'classnames',classnames,'crossval','on',... 'learners',tlearners,'fitposterior',true);
cvmdl
is a classificationpartitionedecoc
cross-validated model. by default, the software implements 10-fold cross-validation. the scores across the binary learners have the same form (that is, they are posterior probabilities), so the software can aggregate the results of the binary classifications properly.
inspect one of the trained folds using dot notation.
cvmdl.trained{1}
ans = compactclassificationecoc responsename: 'y' categoricalpredictors: [] classnames: {'setosa' 'versicolor' 'virginica'} scoretransform: 'none' binarylearners: {3x1 cell} codingmatrix: [3x3 double] properties, methods
each fold is a compactclassificationecoc
model trained on 90% of the data.
you can access the results of the binary learners using dot notation and cell indexing. display the trained svm classifier (the third binary learner) in the first fold.
cvmdl.trained{1}.binarylearners{3}
ans = compactclassificationsvm responsename: 'y' categoricalpredictors: [] classnames: [-1 1] scoretransform: '@(s)sigmoid(s,-4.016619e 00,-3.243499e-01)' alpha: [33x1 double] bias: -0.1345 kernelparameters: [1x1 struct] supportvectors: [33x4 double] supportvectorlabels: [33x1 double] properties, methods
estimate the generalization error.
generror = kfoldloss(cvmdl)
generror = 0.0333
on average, the generalization error is approximately 3%.
more about
error-correcting output codes model
an error-correcting output codes (ecoc) model reduces the problem of classification with three or more classes to a set of binary classification problems.
ecoc classification requires a coding design, which determines the classes that the binary learners train on, and a decoding scheme, which determines how the results (predictions) of the binary classifiers are aggregated.
assume the following:
the classification problem has three classes.
the coding design is one-versus-one. for three classes, this coding design is
you can specify a different coding design by using the
coding
name-value argument when you create a classification model.the model determines the predicted class by using the loss-weighted decoding scheme with the binary loss function g. the software also supports the loss-based decoding scheme. you can specify the decoding scheme and binary loss function by using the
decoding
andbinaryloss
name-value arguments, respectively, when you call object functions, such aspredict
,loss
,margin
,edge
, and so on.
the ecoc algorithm follows these steps.
learner 1 trains on observations in class 1 or class 2, and treats class 1 as the positive class and class 2 as the negative class. the other learners are trained similarly.
let m be the coding design matrix with elements mkl, and sl be the predicted classification score for the positive class of learner l. the algorithm assigns a new observation to the class () that minimizes the aggregation of the losses for the b binary learners.
ecoc models can improve classification accuracy, compared to other multiclass models [1].
coding design
the coding design is a matrix whose elements direct which classes are trained by each binary learner, that is, how the multiclass problem is reduced to a series of binary problems.
each row of the coding design corresponds to a distinct class, and each column corresponds to a binary learner. in a ternary coding design, for a particular column (or binary learner):
a row containing 1 directs the binary learner to group all observations in the corresponding class into a positive class.
a row containing –1 directs the binary learner to group all observations in the corresponding class into a negative class.
a row containing 0 directs the binary learner to ignore all observations in the corresponding class.
coding design matrices with large, minimal, pairwise row distances based on the hamming measure are optimal. for details on the pairwise row distance, see and [2].
this table describes popular coding designs.
coding design | description | number of learners | minimal pairwise row distance |
---|---|---|---|
one-versus-all (ova) | for each binary learner, one class is positive and the rest are negative. this design exhausts all combinations of positive class assignments. | k | 2 |
one-versus-one (ovo) | for each binary learner, one class is positive, one class is negative, and the rest are ignored. this design exhausts all combinations of class pair assignments. | k(k – 1)/2 | 1 |
binary complete | this design partitions the classes into all binary
combinations, and does not ignore any classes. that is, all class
assignments are | 2k – 1 – 1 | 2k – 2 |
ternary complete | this design partitions the classes into all ternary
combinations. that is, all class assignments are
| (3k – 2k 1 1)/2 | 3k – 2 |
ordinal | for the first binary learner, the first class is negative and the rest are positive. for the second binary learner, the first two classes are negative and the rest are positive, and so on. | k – 1 | 1 |
dense random | for each binary learner, the software randomly assigns classes into positive or negative classes, with at least one of each type. for more details, see . | random, but approximately 10 log2k | variable |
sparse random | for each binary learner, the software randomly assigns classes as positive or negative with probability 0.25 for each, and ignores classes with probability 0.5. for more details, see . | random, but approximately 15 log2k | variable |
this plot compares the number of binary learners for the coding designs with an increasing number of classes (k).
algorithms
random coding design matrices
for a given number of classes k, the software generates random coding design matrices as follows.
the software generates one of these matrices:
dense random — the software assigns 1 or –1 with equal probability to each element of the k-by-ld coding design matrix, where .
sparse random — the software assigns 1 to each element of the k-by-ls coding design matrix with probability 0.25, –1 with probability 0.25, and 0 with probability 0.5, where .
if a column does not contain at least one 1 and one –1, then the software removes that column.
for distinct columns u and v, if u = v or u = –v, then the software removes v from the coding design matrix.
the software randomly generates 10,000 matrices by default, and retains the matrix with the largest, minimal, pairwise row distance based on the hamming measure ([2]) given by
where mkjl is an element of coding design matrix j.
support vector storage
by default and for efficiency, fitcecoc
empties the alpha
, supportvectorlabels
,
and supportvectors
properties
for all linear svm binary learners. fitcecoc
lists beta
, rather than
alpha
, in the model display.
to store alpha
, supportvectorlabels
, and
supportvectors
, pass a linear svm template that specifies storing
support vectors to fitcecoc
. for example,
enter:
t = templatesvm('savesupportvectors',true) mdl = fitcecoc(x,y,'learners',t);
you can remove the support vectors and related values by passing the resulting
classificationecoc
model to
discardsupportvectors
.
references
[1] fürnkranz, johannes. “round robin classification.” j. mach. learn. res., vol. 2, 2002, pp. 721–747.
[2] escalera, s., o. pujol, and p. radeva. “separability of ternary codes for sparse designs of error-correcting output codes.” pattern recog. lett., vol. 30, issue 3, 2009, pp. 285–297.
extended capabilities
c/c code generation
generate c and c code using matlab® coder™.
usage notes and limitations:
when you train an ecoc model by using
fitcecoc
, the following restrictions apply.all binary learners must be svm classifiers or linear classification models. for the
learners
name-value argument, you can specify:'svm'
or'linear'
an svm template object or a cell array of such objects (see
templatesvm
)a linear classification model template object or a cell array of such objects (see
templatelinear
)
code generation limitations for the binary learners used in the ecoc classifier also apply to the ecoc classifier. for linear classification models, you can specify only one regularization strength—
'auto'
or a nonnegative scalar for thelambda
name-value argument.for code generation with a coder configurer, the following additional restrictions apply.
if you use a cell array of svm template objects, the value of
standardize
for svm learners must be consistent. for example, if you specify'standardize',true
for one svm learner, you must specify the same value for all svm learners.if you use a cell array of svm template objects, and you use one svm learner with a linear kernel (
'kernelfunction','linear'
) and another with a different type of kernel function, then you must specify
for the learner with a linear kernel.'savesupportvectors'
,truecategorical predictors (
logical
,categorical
,char
,string
, orcell
) are not supported. you cannot use thecategoricalpredictors
name-value argument. to include categorical predictors in a model, preprocess them by using before fitting the model.class labels with the
categorical
data type are not supported. both the class label value in the training data (tbl
ory
) and the value of theclassnames
name-value argument cannot be an array with thecategorical
data type.for more details, see . for information on name-value arguments that you cannot modify when you retrain a model, see .
for more information, see introduction to code generation.
gpu arrays
accelerate code by running on a graphics processing unit (gpu) using parallel computing toolbox™.
usage notes and limitations:
the following object functions fully support gpu arrays:
the following object functions offer limited support for gpu arrays:
the object functions execute on a gpu if either of the following apply:
the model was fitted with gpu arrays.
the predictor data that you pass to the object function is a gpu array.
for more information, see run matlab functions on a gpu (parallel computing toolbox).
version history
introduced in r2014b
see also
classificationecoc
| fitcecoc
| | | |
打开示例
您曾对此示例进行过修改。是否要打开带有您的编辑的示例?
matlab 命令
您点击的链接对应于以下 matlab 命令:
请在 matlab 命令行窗口中直接输入以执行命令。web 浏览器不支持 matlab 命令。
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: .
you 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
- (español)
- (english)
- (english)
europe
- (english)
- (english)
- (deutsch)
- (español)
- (english)
- (français)
- (english)
- (italiano)
- (english)
- (english)
- (english)
- (deutsch)
- (english)
- (english)
- switzerland
- (english)
asia pacific
- (english)
- (english)
- (english)
- 中国
- (日本語)
- (한국어)