Categories &

Functions List

Class Definition: ClassificationDiscriminant

statistics: obj = ClassificationDiscriminant (X, Y)
statistics: obj = ClassificationDiscriminant (…, name, value)

Create a ClassificationDiscriminant class object containing a discriminant analysis model.

obj = ClassificationDiscriminant (X, Y) returns a ClassificationDiscriminant object, with X as the predictor data and Y containing the class labels of observations in X.

  • X must be a N×P numeric matrix of input data where rows correspond to observations and columns correspond to features or variables. X will be used to train the discriminant model.
  • Y is N×1 matrix or cell matrix containing the class labels of corresponding predictor data in X. Y can contain any type of categorical data. Y must have the same number of rows as X.

obj = ClassificationDiscriminant (…, name, value) returns a ClassificationDiscriminant object with parameters specified by Name-Value pair arguments. Type help ClassificationDiscriminant for more info.

A ClassificationDiscriminant object, obj, stores the labeled training data and various parameters for the discriminant analysis 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, numeric vector, or cell array of character vectors. Each value in Y is the observed class label for the corresponding row in X.
NumObservationsNumber of observations used in training the ClassificationDiscriminant model, specified as a positive integer scalar.
RowsUsedRows of the original training data used in fitting the ClassificationDiscriminant model, specified as a numerical vector.
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.
ResponseNameResponse variable name, specified as a character vector.
ClassNamesNames of the classes in the training data Y with duplicates removed, specified as a cell array of character vectors.
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.
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.
SigmaWithin-class covariance matrix, specified as a numeric matrix. For ’linear’ discriminant type matrix is of size p×p, where p is the number of predictors.
MuClass means, specified as a K×p real matrix. K is the number of classes, and p is the number of predictors.
CoeffsCoefficient matrices, specified as a struct array.
DeltaThreshold for linear discriminant model, specified as a numeric scalar.
DiscrimTypeDiscriminant type, specified as a character vector.
GammaGamma regularization parameter, specified as a numeric scalar.
MinGammaMinimum value of Gamma so that the correlation matrix is invertible, specified as nonnegative scalar.
LogDetSigmaLogarithm of the determinant of the within-class covariance matrix. For linear discriminant analysis it is specified as a numeric scalar.
XCenteredX data with class means subtracted, returned as a real matrix.

See also: fitcdiscr

Source Code: ClassificationDiscriminant

Method: compact

ClassificationDiscriminant: CVMdl = compact (obj)

Create a CompactClassificationDiscriminant object.

CVMdl = compact (obj) creates a compact version of the ClassificationDiscriminant object, obj.

See also: fitcdiscr, ClassificationDiscriminant, CompactClassificationDiscriminant

Method: crossval

ClassificationDiscriminant: CVMdl = crossval (obj)
ClassificationDiscriminant: CVMdl = crossval (…, Name, Value)

Cross Validate a Discriminant classification object.

CVMdl = crossval (obj) returns a cross-validated model object, CVMdl, from a trained model, obj, using 10-fold cross-validation by default.

CVMdl = crossval (obj, name, value) specifies additional name-value pair arguments to customize the cross-validation process.

NameValue
"KFold"Specify the number of folds to use in k-fold cross-validation. "KFold", k, where k is an integer greater than 1.
"Holdout"Specify the fraction of the data to hold out for testing. "Holdout", p, where p is a scalar in the range (0,1).
"Leaveout"Specify whether to perform leave-one-out cross-validation. "Leaveout", Value, where Value is ’on’ or ’off’.
"CVPartition"Specify a cvpartition object used for cross-validation. "CVPartition", cv, where isa (cv, "cvpartition") = 1.

See also: fitcdiscr, ClassificationDiscriminant, cvpartition, ClassificationPartitionedModel

Method: loss

ClassificationDiscriminant: L = loss (obj, X, Y)
ClassificationDiscriminant: L = loss (…, name, value)

Compute loss for a trained ClassificationDiscriminant object.

L = loss (obj, X, Y) computes the loss, L, using the default loss function 'mincost'.

  • obj is a ClassificationDiscriminant object trained on X and Y.
  • X must be a N×P numeric matrix of input data where rows correspond to observations and columns correspond to features or variables.
  • Y is N×1 matrix or cell matrix containing the class labels of corresponding predictor data in X. Y must have same numbers of Rows as X.

L = loss (…, name, value) allows additional options specified by name-value pairs:

NameValue
"LossFun"Specifies the loss function to use. Can be a function handle with four input arguments (C, S, W, Cost) which returns a scalar value or one of: ’binodeviance’, ’classifcost’, ’classiferror’, ’exponential’, ’hinge’, ’logit’,’mincost’, ’quadratic’.
  • C is a logical matrix of size N×K, where N is the number of observations and K is the number of classes. The element C(i,j) is true if the class label of the i-th observation is equal to the j-th class.
  • S is a numeric matrix of size N×K, where each element represents the classification score for the corresponding class.
  • W is a numeric vector of length N, representing the observation weights.
  • Cost is a K×K matrix representing the misclassification costs.
"Weights"Specifies observation weights, must be a numeric vector of length equal to the number of rows in X. Default is ones (size (X, 1)). loss normalizes the weights so that observation weights in each class sum to the prior probability of that class. When you supply Weights, loss computes the weighted classification loss.

See also: ClassificationDiscriminant

Method: margin

ClassificationDiscriminant: m = margin (obj, X, Y)

m = margin (obj, X, Y) returns the classification margins for obj with data X and classification Y. m is a numeric vector of length size (X,1).

  • obj is a ClassificationDiscriminant object trained on X and Y.
  • X must be a N×P numeric matrix of input data where rows correspond to observations and columns correspond to features or variables.
  • Y is N×1 matrix or cell matrix containing the class labels of corresponding predictor data in X. Y must have same numbers of Rows as X.

The classification margin for each observation is the difference between the classification score for the true class and the maximal classification score for the false classes.

See also: fitcdiscr, ClassificationDiscriminant

Method: predict

ClassificationDiscriminant: label = predict (obj, XC)
ClassificationDiscriminant: [label, score, cost] = predict (obj, XC)

Classify new data points into categories using the discriminant analysis model from a ClassificationDiscriminant object.

label = predict (obj, XC) returns the vector of labels predicted for the corresponding instances in XC, using the predictor data in obj.X and corresponding labels, obj.Y, stored in the ClassificationDiscriminant model, obj.

  • obj must be a ClassificationDiscriminant class object.
  • XC must be an M×P numeric matrix with the same number of features P as the corresponding predictors of the discriminant model in obj.

[label, score, cost] = predict (obj, XC) also returns score, which contains the predicted class scores or posterior probabilities for each instance of the corresponding unique classes, and cost, which is a matrix containing the expected cost of the classifications.

The score matrix contains the posterior probabilities for each class, calculated using the multivariate normal probability density function and the prior probabilities of each class. These scores are normalized to ensure they sum to 1 for each observation.

The cost matrix contains the expected classification cost for each class, computed based on the posterior probabilities and the specified misclassification costs.

See also: ClassificationDiscriminant, fitcdiscr

Method: savemodel

ClassificationDiscriminant: savemodel (obj, filename)

Save a ClassificationDiscriminant object.

savemodel (obj, filename) saves a ClassificationDiscriminant object into a file defined by filename.

See also: loadmodel, fitcdiscr, ClassificationDiscriminant

Example: 1

 

 ## Create discriminant classifier
 ## Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcdiscr (x, y);
 [label, score, cost] = predict (obj, xc);

                    

Example: 2

 

 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 ## Compute loss for discriminant model
 L = loss (model, X, Y)

L = 0
                    

Example: 3

 

 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 ## Margin for discriminant model
 m = margin (mdl, X, Y)

m = 1.0000
                    

Example: 4

 

 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, "gamma", 0.4);
 ## Cross-validation for discriminant model
 CVMdl = crossval (obj)

CVMdl =

  ClassificationPartitionedModel object with properties:

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