Categories &

Functions List

Function Reference: fitcdiscr

statistics: Mdl = fitcdiscr (X, Y)
statistics: Mdl = fitcdiscr (…, name, value)

Fit a Linear Discriminant Analysis classification model.

Mdl = fitcdiscr (X, Y) returns a Linear Discriminant Analysis (LDA) classification model, Mdl, with X being the predictor data, and Y the class labels of observations in X.

  • X must be a N×P numeric matrix of predictor 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 can be numerical, logical, char array or cell array of character vectors. Y must have same number of rows as X.

Mdl = fitcdiscr (…, name, value) returns a Linear Discriminant Analysis model with additional options specified by Name-Value pair arguments listed below.

Model Parameters

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.
'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.
'Cost'A 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. cost matrix can be altered use Mdl.cost = somecost. default value cost = ones(rows(X),numel(unique(Y))).
'DiscrimType'A character vector naming the type of discriminant analysis to perform, one of 'linear' (default), 'quadratic', 'diagLinear', 'diagQuadratic', 'pseudoLinear' or 'pseudoQuadratic'. A linear type pools one covariance across the classes and a quadratic type estimates one per class; a 'diag' type keeps only the variances, and a 'pseudo' type inverts a singular covariance rather than refusing it. The property may be reassigned after fitting, but only within its own family, since the family decides which covariances the fit estimates.
'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.

Source Code: fitcdiscr

See also: ClassificationDiscriminant

Source Code: fitcdiscr

Train a linear discriminant classifier for Gamma = 0.5 and plot the decision boundaries.

 load fisheriris
 idx = ! strcmp (species, 'setosa');
 X = meas(idx,3:4);
 Y = cast (strcmpi (species(idx), 'virginica'), 'double');
 obj = fitcdiscr (X, Y, 'Gamma', 0.5)
obj =

  ClassificationDiscriminant

             ResponseName: 'Y'
               ClassNames: [0 1]
           ScoreTransform: 'none'
          NumObservations: 100
            NumPredictors: 2
              DiscrimType: 'linear'
                       Mu: [2x2 double]
                   Coeffs: [2x2 struct]
 x1 = [min(X(:,1)):0.03:max(X(:,1))];
 x2 = [min(X(:,2)):0.02:max(X(:,2))];
 [x1G, x2G] = meshgrid (x1, x2);
 XGrid = [x1G(:), x2G(:)];
 pred = predict (obj, XGrid);
 gidx = logical (pred);
 figure
 scatter (XGrid(gidx,1), XGrid(gidx,2), 'markerfacecolor', 'magenta');
 hold on
 scatter (XGrid(! gidx,1), XGrid(! gidx,2), 'markerfacecolor', 'red');
 plot (X(Y == 0, 1), X(Y == 0, 2), 'ko', X(Y == 1, 1), X(Y == 1, 2), 'kx');
 xlabel ('Petal length (cm)');
 ylabel ('Petal width (cm)');
 title ('Linear Discriminant Analysis Decision Boundary');
 legend ({'Versicolor Region', 'Virginica Region', ...
         'Sampled Versicolor', 'Sampled Virginica'}, ...
         'location', 'northwest')
 axis tight
 hold off
plotted figure