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 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: ClassificationDiscriminant

Source Code: fitcdiscr

Example: 1

 

 ## 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)
 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 (str2num (cell2mat (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

obj =

  ClassificationDiscriminant object with properties:

           ClassNames: [2x1 cell]
               Coeffs: [2x2 struct]
                 Cost: [2x2 double]
                Delta: [1x1 double]
          DiscrimType: linear
                Gamma: [1x1 double]
          LogDetSigma: [1x1 double]
             MinGamma: [1x1 double]
                   Mu: [2x2 double]
      NumObservations: [1x1 double]
        NumPredictors: [1x1 double]
       PredictorNames: [1x2 cell]
                Prior: [2x1 double]
         ResponseName: Y
             RowsUsed: [100x1 double]
       ScoreTransform: none
                Sigma: [2x2 double]
                    X: [100x2 double]
            XCentered: [100x2 double]
                    Y: [100x1 double]

                    
plotted figure