# makecdiscr

Construct discriminant analysis classifier from parameters

## Description

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.`cobj`

= makecdiscr(`Mu`

,`Sigma`

,`Name,Value`

)

## Examples

### Construct a Compact Linear Discriminant Analysis Classifier

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

`Mu`

— Class means

matrix of scalar values

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`

`Sigma`

— Within-class covariance

matrix of scalar values

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.

`BetweenSigma`

— Between-class covariance

`[]`

(default) | symmetric, positive semidefinite matrix

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`

`ClassNames`

— Class names

numeric vector | categorical vector | logical vector | character array | string array | cell array of character vectors

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`

— Cost of misclassification

square matrix | structure

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`

`FillCoeffs`

— `Coeffs`

property flag

`"on"`

(default) | `"off"`

`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`

`PredictorNames`

— Predictor variable names

`{'X1','X2',...}`

(default) | string array | cell array of character vectors

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`

— Prior probabilities

`'uniform'`

(default) | vector of scalar values | structure

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 equalA 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`

`ResponseName`

— Response variable name

`'Y'`

(default) | character vector | string scalar

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

`cobj`

— Compact discriminant analysis classifier

`CompactClassificationDiscriminant`

object

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

is one of`discrimType`

`'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**

## See Also

`fitcdiscr`

| `compact`

| `predict`

| `CompactClassificationDiscriminant`

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)