Categories &

Functions List

Class Definition: ClassificationDiscriminant

statistics: ClassificationDiscriminant

Discriminant analysis classification

The ClassificationDiscriminant class implements a discriminant analysis classifier object, which can predict responses for new data using the predict method.

Discriminant analysis classification is a statistical method used to classify observations into predefined groups based on their characteristics. It estimates the parameters of different distributions for each class and predicts the class of new observations by finding the one with the smallest misclassification cost.

Create a ClassificationDiscriminant object by using the fitcdiscr function or the class constructor.

Six discriminant types are available, in two families. The linear family, 'linear', 'diagLinear' and 'pseudoLinear', pools one covariance across the classes and separates them with a hyperplane. The quadratic family, 'quadratic', 'diagQuadratic' and 'pseudoQuadratic', estimates a covariance per class and separates them with a quadric. A 'diag' type keeps only the variances, which is the same model as a Gamma of 1, and a 'pseudo' type inverts a singular covariance rather than refusing it.

DiscrimType may be assigned after fitting, but only within its own family: the family is fixed when the model is fitted, because it decides which covariances the fit has to estimate. Assigning it, or Gamma, re-derives Sigma, LogDetSigma and Coeffs without refitting.

See also: fitcdiscr

Source Code: ClassificationDiscriminant

The ClassificationDiscriminant class contains the following properties:

A numeric column vector with one entry per observation used for fitting. Every observation carries the same weight, so the vector sums to one. This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A numeric matrix containing the unstandardized predictor data. Each column of X represents one predictor (variable), and each row represents one observation. This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

Specified as a logical or numeric column vector, or as a character array or a cell array of character vectors with the same number of rows as the predictor data. Each row in Y is the observed class label for the corresponding row in X. This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A positive integer value specifying the number of observations in the training dataset used for training the ClassificationDiscriminant model. This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A logical column vector with the same length as the observations in the original predictor data X, true for each row that was used for fitting the ClassificationDiscriminant model. It is empty, [], when every observation was used, so a non-empty value means that rows holding missing values were dropped. This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A positive integer value specifying the number of predictors in the training dataset used for training the ClassificationDiscriminant model. This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A cell array of character vectors specifying the names of the predictor variables. The names are in the order in which they appear in the training dataset. This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A P-by-P matrix holding the covariance of the class means about the overall mean, weighted by how many observations each class contributes. With n_k observations in class k, p_k = n_k / n and \bar{\mu} = \sum_k p_k \mu_k, it is

 
 BetweenSigma = sum_k n_k (Mu(k,:) - mubar)' * (Mu(k,:) - mubar)
                / (n * (1 - sum_k p_k^2))

The denominator is the unbiased one for a weighted covariance, so a balanced fit divides by n (K-1) / K. It reads the class sizes, not Prior: assigning a prior leaves it where it was. It is estimated for every discriminant type, the quadratic family included, since it describes the classes rather than the fit. This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A numeric vector of column indices into X naming the predictors treated as categorical, and empty when none is. This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A cell array of character vectors. It matches PredictorNames unless a categorical predictor was expanded into indicator variables. This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A character vector specifying the name of the response variable Y. This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

An array of unique values of the response variable Y, which has the same data types as the data in Y. This property is read-only. ClassNames can have any of the following datatypes:

  • Cell array of character vectors
  • Character array
  • Logical vector
  • Numeric vector

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A numeric array whose shape follows DiscrimType, with P predictors and K classes:

DiscrimTypeSigmaLogDetSigma
'linear', 'pseudoLinear'PxP scalar
'quadratic', 'pseudoQuadratic'PxPxK Kx1
'diagLinear'1xPscalar
'diagQuadratic'1xPxKKx1

The linear family pools one covariance across the classes and the quadratic family estimates one per class. This property is read-only, but it is re-derived whenever DiscrimType or Gamma is assigned.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A K×P numeric matrix specifying the mean of the multivariate normal distribution of each corresponding class, where K is the number of classes and P is the number of predictors in X. This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A K×K structure containing the coefficient matrices, where K is the number of classes. If the 'FillCoeffs' parameter was set to 'off' in either the fitcdiscr function or the ClassificationDiscriminant constructor, then Coeffs is empty ([]). This property is read-only.

Coeffs(i,j) contains the coefficients of the boundary between the classes i and j in the following fields:

  • DiscrimType - A character vector
  • Class1 - ClassNames(i)
  • Class2 - ClassNames(j)
  • Const - A scalar
  • Linear - A vector with length as the number of predictors.
  • Quadratic - The quadratic family only. A PxP matrix, or a 1xP vector for 'diagQuadratic', following the shape of Sigma.

The diagonal entries carry the two class names and nothing else. The structure is rebuilt whenever DiscrimType, Gamma or Prior is assigned.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A row vector with one entry per predictor, the value of Delta at which that predictor’s coefficient is zero for every class and the predictor leaves the model altogether. It is all zeros for the quadratic family, which has no linear coefficients to eliminate.

This property is read-only, and it describes the fit rather than the threshold: assigning Delta does not move it.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A scalar from 0 to 1, the least regularization that leaves the correlation matrix invertible. It is 0 when the matrix is already invertible, and positive when the predictors are collinear, in which case a plain 'linear' or 'quadratic' fit is raised to it rather than failing. Assigning a Gamma below it is refused.

This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A scalar for the linear family and a Kx1 vector for the quadratic one, one entry per class. It is computed in correlation space, as the sum of the logarithms of the predictor variances plus the log determinant of the correlation matrix, which is far better conditioned than the covariance when the data are nearly collinear. A predictor with no variance contributes nothing rather than an infinity, and the 'pseudo' types sum only over the directions that carry variance.

This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A matrix of the same size as X and the values in X with the corresponding class means subtracted. This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A cell array with one entry per predictor, holding that predictor’s bin edges where the learner discretized it before fitting. It is empty here and stays empty: this learner fits the predictors as they are, and MATLAB’s reports an empty cell for it as well.

This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A structure holding the parameters of the fit: DiscrimType, Gamma, Delta, FillCoeffs, and the Version, Method and Type tags.

MATLAB reports a SaveMemory field beside these. This class has no such option and always stores the full covariance, so there is no setting to report and the field is absent rather than answering for a knob that does not exist. This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

Always empty. It is declared for MATLAB compatibility, where it holds what an automatic search over the hyperparameters found. This class fits the parameters it is given and runs no such search, so there is nothing to report. This property is read-only.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A character vector naming the discriminant model, one of 'linear', 'quadratic', 'diagLinear', 'diagQuadratic', 'pseudoLinear' or 'pseudoQuadratic'. A linear type pools one covariance across the classes; a quadratic type estimates one per class. A 'diag' type keeps only the variances, and a 'pseudo' type inverts a singular covariance instead of refusing it.

This property may be assigned, but only within its own family: the three linear types interchange freely and so do the three quadratic ones, while no assignment moves a model between the two. The family is fixed when the model is fitted, because it decides which covariances the fit has to estimate. Assigning re-derives Sigma, LogDetSigma, Gamma and Coeffs.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A scalar from 0 to 1 shrinking the covariance towards its diagonal. Gamma and DiscrimType are one state: a value of 1 is the diagonal type, so assigning it renames DiscrimType to 'diagLinear' or 'diagQuadratic', and assigning a diagonal type sets Gamma to 1.

The quadratic family admits 0 and 1 only. A value below MinGamma is refused, since it would leave the covariance singular. Assigning re-derives Sigma, LogDetSigma and Coeffs.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A nonnegative scalar that eliminates predictors. A per-class linear coefficient is set to zero when it falls below Delta, and the comparison is made on the standardized coefficient, the coefficient times the within-class standard deviation of its predictor. Scaling matters here: a threshold on the raw coefficients would depend on the units each predictor is measured in, so the same model in centimetres and in metres would drop different predictors.

DeltaPredictor reports, per predictor, the value at which it drops out of every class at once.

It applies to the linear family only, a quadratic discriminant having no linear coefficients to eliminate. Assigning it rebuilds Coeffs and changes what predict answers.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A square matrix specifying the cost of misclassification of a point. 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.

Add or change the Cost property using dot notation as in:

  • obj.Cost = costMatrix

A cost may also be given as a struct with the fields ClassNames and ClassificationCosts, which names the order its own matrix is written in. That matrix is permuted into the order of ClassNames above, so a caller need not know which order the classes were sorted into. It must name every class.

A cost must be floating point, not sparse, not complex, non-negative and zero down its diagonal, and must hold no NaN or Inf. A single is widened to double.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

A numeric vector specifying the prior probabilities for each class. The order of the elements in Prior corresponds to the order of the classes in ClassNames.

Add or change the Prior property using dot notation as in:

  • obj.Prior = priorVector

Specified as a row vector with one entry per class, in the order of ClassNames, and rescaled to sum to one. It may be given as 'empirical', 'uniform', a numeric vector, or a structure with ClassNames and ClassProbs fields, which assigns each probability by class name rather than by position.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

Specified as a function handle for transforming the classification scores. Add or change the ScoreTransform property using dot notation as in:

  • obj.ScoreTransform = 'function_name'
  • obj.ScoreTransform = @function_handle

When specified as a character vector, it can be any of the following built-in functions. Nevertheless, the ScoreTransform property always stores their function handle equivalent.

ValueDescription
'doublelogit'1 ./ (1 + exp (-2 × x))
'invlogit'log (x ./ (1 - x))
'ismax'Sets the score for the class with the largest score to 1, and for all other classes to 0
'logit'1 ./ (1 + exp (-x))
'none'x (no transformation)
'identity'x (no transformation)
'sign' -1 for x < 0, 0 for x = 0, 1 for x > 0
'symmetric'2 × x - 1
'symmetricismax'Sets the score for the class with the largest score to 1, and for all other classes to -1
'symmetriclogit'2 ./ (1 + exp (-x)) - 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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

The ClassificationDiscriminant class offers the following public methods:

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

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 the following name, value paired input arguments:

NameValue
'PredictorNames'A cell array of character vectors specifying the names of the predictors. The length of this array must match the number of columns in X.
'ResponseName'A character vector specifying the name of the response variable.
'ClassNames'Names of the classes in the class labels, Y, used for fitting the Discriminant model. ClassNames are of the same type as the class labels in Y.
'Cost'An N×R numeric matrix containing misclassification cost for the corresponding instances in X, where R is the number of unique categories in Y. If an instance is correctly classified into its category the cost is calculated to be 1, otherwise 0. The cost matrix can be altered by using Mdl.cost = somecost. By default, its value is cost = ones (rows (X), numel (unique (Y))).
'Prior'A numeric vector specifying the prior probabilities for each class. The order of the elements in Prior corresponds to the order of the classes in ClassNames. Alternatively, you can specify 'empirical' to use the empirical class probabilities or 'uniform' to assume equal class probabilities.
'ScoreTransform'A user-defined function handle or a character vector specifying one of the following builtin functions specifying the transformation applied to predicted classification scores. Supported values include 'doublelogit', 'invlogit', 'ismax', 'logit', 'none', 'identity', 'sign', 'symmetric', 'symmetricismax', and 'symmetriclogit'.
'DiscrimType'A character vector or string scalar specifying the type of discriminant analysis to perform. The only supported value is 'linear'.
'FillCoeffs'A character vector or string scalar with values 'on' or 'off' specifying whether to fill the coefficients after fitting. If set to 'on', the coefficients are computed during model fitting, which can be useful for prediction.
'Gamma'A numeric scalar specifying the regularization parameter for the covariance matrix. It adjusts the linear discriminant analysis to make the model more stable in the presence of multicollinearity or small sample sizes. A value of 0 corresponds to no regularization, while a value of 1 corresponds to a completely regularized model.

See also: fitcdiscr

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
ClassificationDiscriminant: n = nLinearCoeffs (obj)
ClassificationDiscriminant: n = nLinearCoeffs (obj, delta)

n = nLinearCoeffs (obj) returns the number of predictors the discriminant keeps at its own Delta.

n = nLinearCoeffs (obj, delta) returns the number it would keep at each threshold in delta, as a column vector however delta is shaped.

A predictor survives a threshold when its DeltaPredictor reaches it, the comparison including equality, so delta at exactly a predictor’s own value still counts it. A threshold above every DeltaPredictor therefore leaves nothing and returns zero.

The count is taken whatever the DiscrimType, as MATLAB takes it, even though Delta regularizes the linear types alone.

See also: fitcdiscr, ClassificationDiscriminant, CompactClassificationDiscriminant

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
ClassificationDiscriminant: label = predict (obj, XC)
ClassificationDiscriminant: [label, score, cost] = predict (obj, XC)

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

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
ClassificationDiscriminant: L = loss (obj, X, Y)
ClassificationDiscriminant: L = loss (…, name, value)

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

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
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

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
ClassificationDiscriminant: CVMdl = crossval (obj)
ClassificationDiscriminant: CVMdl = crossval (…, Name, Value)

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

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
ClassificationDiscriminant: CVMdl = compact (obj)

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

See also: fitcdiscr, ClassificationDiscriminant, CompactClassificationDiscriminant

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
ClassificationDiscriminant: e = edge (obj, X, Y)
ClassificationDiscriminant: e = edge (…, "Weights", w)

e = edge (obj, X, Y) reduces the vector that margin returns to a single number, the mean margin over the rows of X. It says how far the model puts the true class ahead of its nearest rival on average, so a larger edge is a better model, and unlike a loss it is not bounded above and rewards confidence rather than bare correctness.

e = edge (…, "Weights", w) takes the weighted mean instead, with one weight per row of X.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
ClassificationDiscriminant: M = mahal (obj, X)
ClassificationDiscriminant: M = mahal (…, 'ClassLabels', labels)

M = mahal (obj, X) returns an NxK matrix whose element (i,j) is the squared Mahalanobis distance from observation i to the mean of class j, measured against the covariance that class carries: the one shared covariance for a linear discriminant and the class’s own for a quadratic one.

  • obj must be a ClassificationDiscriminant object.
  • X must be an NxP numeric matrix with one column per predictor of the trained model.

M = mahal (…, 'ClassLabels', labels) returns an Nx1 vector instead, holding for each observation the distance to the mean of the class labels names for it. labels must have one entry per row of X, each of them one of ClassNames.

The distance is measured against the covariance the model reports, so a regularized model is measured against its regularized covariance. The prior does not enter it.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
ClassificationDiscriminant: lp = logp (obj, X)

lp = logp (obj, X) returns an Nx1 vector holding, for each row of X, the natural logarithm of P(x) = sum_k P(k) P(x|k), the density of the observation summed over the classes with each class weighted by its prior P(k). Each P(x|k) is the multivariate normal density of class k.

  • obj must be a ClassificationDiscriminant object.
  • X must be an NxP numeric matrix with one column per predictor of the trained model.

An unusually low value marks an observation the model finds unlikely under every class, which is what makes this an outlier test rather than a classification.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
ClassificationDiscriminant: label = resubPredict (obj)
ClassificationDiscriminant: [label, score, cost] = resubPredict (obj)

label = resubPredict (obj) is predict applied to the observations the model was fitted on, which it holds in X. Handing them over yourself is not the same thing: a row dropped for a missing response is not in X, so the original matrix and the model’s own are different data.

The result measures fit and not generalization, and is optimistic by construction. crossval is what estimates performance on data the model has not seen.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
ClassificationDiscriminant: m = resubMargin (obj)

m = resubMargin (obj) is margin applied to the observations the model was fitted on, one number per observation. Being a resubstitution quantity it is optimistic by construction.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
ClassificationDiscriminant: e = resubEdge (obj)

e = resubEdge (obj) is edge applied to the observations the model was fitted on, the mean of resubMargin.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
ClassificationDiscriminant: L = resubLoss (obj)
ClassificationDiscriminant: L = resubLoss (…, name, value)

L = resubLoss (obj) is loss applied to the observations the model was fitted on, defaulting to 'mincost', and it accepts the same Name-Value pairs.

Being a resubstitution quantity it is a lower bound on the error rather than an estimate of it. It is worth least on a lazy learner: a one-neighbour ClassificationKNN has a resubstitution loss of exactly zero, every training point being its own nearest neighbour.

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
ClassificationDiscriminant: savemodel (obj, filename)

savemodel (obj, filename) saves each property of a ClassificationDiscriminant object into an Octave binary file, the name of which is specified in filename, along with an extra variable, which defines the type classification object these variables constitute. Use loadmodel in order to load a classification object into Octave’s workspace.

See also: loadmodel, fitcdiscr, ClassificationDiscriminant

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
ClassificationDiscriminant: err = cvshrink (obj)
ClassificationDiscriminant: [err, gamma] = cvshrink (obj)
ClassificationDiscriminant: [err, gamma, delta] = cvshrink (obj)
ClassificationDiscriminant: [err, gamma, delta, numpred] = cvshrink (obj)
ClassificationDiscriminant: […] = cvshrink (…, Name, Value)

err = cvshrink (obj) cross validates obj over a grid of Gamma values and returns the misclassification rate at each of them, so that a regularization can be chosen by what it costs on held-out data rather than on the data it was fitted to.

[err, gamma, delta, numpred] = cvshrink (obj) also returns the grid itself and the number of predictors surviving at each point of it. gamma is a column with one entry per Gamma; err, delta and numpred carry one row per Gamma and one column per Delta.

NameValue
'NumGamma'The number of Gamma intervals, a positive integer, 10 by default, giving NumGamma + 1 values evenly spaced from 0 to 1.
'NumDelta'The number of Delta intervals, a non-negative integer, 0 by default. For each Gamma the Delta values run from 0 to the point at which every predictor has been eliminated, so the grid is not the same in every row.
'Gamma'The Gamma values to try, given explicitly as a vector, in place of 'NumGamma'.
'Delta'The Delta values to try, given explicitly, in place of 'NumDelta': a vector used for every Gamma, or a matrix with one row per Gamma.

Every point of the grid is cross validated against the same partition, so the errors differ by the regularization and not by the split. The partition is drawn at random, so err is not reproducible across runs and does not match MATLAB’s; gamma, delta and numpred are deterministic and do.

See also: ClassificationDiscriminant, fitcdiscr, nLinearCoeffs

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);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'

Examples

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcdiscr (x, y);
 [label, score, cost] = predict (obj, xc);
 load fisheriris
 model = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Compute loss for discriminant model

 L = loss (model, X, Y)
L = 0
 load fisheriris
 mdl = fitcdiscr (meas, species);
 X = mean (meas);
 Y = {'versicolor'};

Margin for discriminant model

 m = margin (mdl, X, Y)
m = 1.0000
 load fisheriris
 x = meas;
 y = species;
 obj = fitcdiscr (x, y, 'gamma', 0.4);

Cross-validation for discriminant model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'Discriminant'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'