Main Content

makecdiscr

Construct discriminant analysis classifier from parameters

Description

cobj = makecdiscr(Mu,Sigma) constructs a compact discriminant analysis classifier from the class means Mu and covariance matrix Sigma.

example

cobj = makecdiscr(Mu,Sigma,Name,Value) constructs a compact classifier with additional options specified by one or more name-value pair arguments. For example, you can specify the cost of misclassification or the prior probabilities for each class.

example

Examples

collapse all

Construct a compact linear discriminant analysis classifier from the means and covariances of the Fisher iris data.

load fisheriris
mu(1,:) = mean(meas(1:50,:));
mu(2,:) = mean(meas(51:100,:));
mu(3,:) = mean(meas(101:150,:));

mm1 = repmat(mu(1,:),50,1);
mm2 = repmat(mu(2,:),50,1);
mm3 = repmat(mu(3,:),50,1);
cc = meas;
cc(1:50,:) = cc(1:50,:) - mm1;
cc(51:100,:) = cc(51:100,:) - mm2;
cc(101:150,:) = cc(101:150,:) - mm3;
sigstar = cc' * cc / 147; % unbiased estimator of sigma
cpct = makecdiscr(mu,sigstar,...
   'ClassNames',{'setosa','versicolor','virginica'})
cpct = 
  CompactClassificationDiscriminant
           PredictorNames: {'x1'  'x2'  'x3'  'x4'}
             ResponseName: 'Y'
    CategoricalPredictors: []
               ClassNames: {'setosa'  'versicolor'  'virginica'}
           ScoreTransform: 'none'
              DiscrimType: 'linear'
                       Mu: [3x4 double]
                   Coeffs: [3x3 struct]


Input Arguments

collapse all

Class means, specified as a K-by-p matrix of scalar values class means of size. K is the number of classes, and p is the number of predictors. Each row of Mu represents the mean of the multivariate normal distribution of the corresponding class. The class indices are in the ClassNames attribute.

Data Types: single | double

Within-class covariance, specified as a matrix of scalar values.

  • For a linear discriminant, Sigma is a symmetric, positive semidefinite matrix of size p-by-p, where p is the number of predictors.

  • For a quadratic discriminant, Sigma is an array of size p-by-p-by-K, where K is the number of classes. For each i, Sigma(:,:,i) is a symmetric, positive semidefinite matrix.

Data Types: single | double

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'ClassNames',{'setosa' 'versicolor' 'virginica'} specifies a discriminant analysis classifier that uses 'setosa', 'versicolor', and 'virginica' as the grouping variables.

Between-class covariance, specified as a symmetric, positive semidefinite matrix of size p-by-p, where p is the number of predictors. If you specify BetweenSigma, then its value is stored in the BetweenSigma property of the classifier object. If you do not specify BetweenSigma, then makecdiscr computes the BetweenSigma property value from the class means Mu by assuming equal weights for the classes.

Data Types: single | double

Class names as ordered in Mu, specified as the comma-separated pair consisting of 'ClassNames' and an array containing grouping variables. Use any data type for a grouping variable, including numeric vector, categorical vector, logical vector, character array, string array, or cell array of character vectors.

The default is 1:K, where K is the number of classes (the number of rows of Mu).

Example: 'ClassNames',{'setosa' 'versicolor' 'virginica'}

Data Types: single | double | logical | char | string | cell

Cost of misclassification, specified as the comma-separated pair consisting of 'Cost' and a square matrix, where Cost(i,j) is the cost of classifying a point into class j if its true class is i. Alternatively, Cost can be a structure S having two fields: S.ClassNames containing the group names as a variable of the same type as y, and S.ClassificationCosts containing the cost matrix.

The default is Cost(i,j)=1 if i~=j, and Cost(i,j)=0 if i=j.

Data Types: single | double | struct

Coeffs property flag, specified as "on" or "off". Setting the flag to "on" populates the Coeffs property of the classifier object. This process can be computationally intensive.

Example: "FillCoeffs","off"

Data Types: char | string

Predictor variable names, specified as the comma-separated pair consisting of 'PredictorNames' and a string array or cell array of character vectors containing the names for the predictor variables, in the order in which they appear in X.

Data Types: string | cell

Prior probabilities for each class, specified as the comma-separated pair consisting of 'Prior' and one of the following:

  • 'uniform', meaning all class prior probabilities are equal

  • A vector containing one scalar value for each class

  • A structure S with two fields:

    • S.ClassNames containing the class names as a variable of the same type as ClassNames

    • S.ClassProbs containing a vector of corresponding probabilities

Data Types: char | string | single | double | struct

Response variable name, specified as the comma-separated pair consisting of 'ResponseName' and a character vector or string scalar containing the name of the response variable y.

Example: 'ResponseName','Response'

Data Types: char | string

Output Arguments

collapse all

Compact discriminant analysis classifier, returned as a CompactClassificationDiscriminant object. You can use the predict object function to predict classification labels for new data.

Tips

  • You can change the discriminant type using dot notation after constructing cobj:

    cobj.DiscrimType = 'discrimType'

    where discrimType is one of 'linear', 'quadratic', 'diagLinear', 'diagQuadratic', 'pseudoLinear', or 'pseudoQuadratic'. You can change between linear types or between quadratic types, but cannot change between a linear and a quadratic type.

  • cobj is a linear classifier when Sigma is a matrix. cobj is a quadratic classifier when Sigma is a three-dimensional array.

Version History

Introduced in R2014a