# CompactClassificationDiscriminant

Compact discriminant analysis classification

## Description

A `CompactClassificationDiscriminant`

object
is a compact version of a discriminant analysis classifier. The compact version does not
include the data for training the classifier. Therefore, you cannot perform some tasks
with a compact classifier, such as cross validation. Use a compact classifier for making
predictions (classifications) of new data.

## Creation

You can create a `CompactClassificationDiscriminant`

object in two ways:

Create a compact model from a full

`ClassificationDiscriminant`

model object by using the`compact`

object function.Create a compact model by using the

`makecdiscr`

function and specifying the class means`Mu`

and covariance matrix`Sigma`

.

## Properties

`BetweenSigma`

— Between-class covariance

square matrix

This property is read-only.

Between-class covariance, specified as a `p`

-by-`p`

matrix, where `p`

is the number of predictors.

**Data Types: **`double`

`CategoricalPredictors`

— Categorical predictor indices

`[]`

This property is read-only.

Categorical predictor indices, which is always empty (`[]`

).

`ClassNames`

— Class names in training data `Y`

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

This property is read-only.

Class names in the training data `Y`

with duplicates removed.
`ClassNames`

has the same data type as the data in the argument
`Y`

in the training data. `ClassNames`

can have
the following data types:

Categorical array

Cell array of character vectors

Character array

Logical vector

Numeric vector

(The software treats string arrays as cell arrays of character vectors.)

**Data Types: **`single`

| `double`

| `logical`

| `char`

| `string`

| `cell`

| `categorical`

`Coeffs`

— Coefficient matrices

`k`

-by-`k`

structure | `[]`

This property is read-only.

Coefficient matrices, specified as a `k`

-by-`k`

structure, where `k`

is the number of classes. If `fitcdiscr`

had the `FillCoeffs`

name-value pair set to
`'off'`

when constructing the classifier, `Coeffs`

is empty (`[]`

).

`Coeffs(i,j)`

contains coefficients of the linear or quadratic
boundaries between classes `i`

and `j`

. Fields in
`Coeffs(i,j)`

:

`DiscrimType`

`Class1`

—`ClassNames`

`(i)`

`Class2`

—`ClassNames`

`(j)`

`Const`

— A scalar`Linear`

— A vector with`p`

components, where`p`

is the number of columns in`X`

`Quadratic`

—`p`

-by-`p`

matrix, exists for quadratic`DiscrimType`

The equation of the boundary between class `i`

and class
`j`

is

`Const`

+ `Linear`

* `x`

+ `x'`

* `Quadratic`

* `x`

=
`0`

,

where `x`

is a column vector of length `p`

.

**Data Types: **`struct`

`Cost`

— Cost of classifying a point

square matrix

Cost of classifying a point, specified as a square matrix.
`Cost(i,j)`

is the cost of classifying a point into class
`j`

if its true class is `i`

(the rows correspond
to the true class and the columns correspond to the predicted class). The order of the
rows and columns of `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.

Change a `Cost`

matrix using dot notation: ```
obj.Cost =
costMatrix
```

.

**Data Types: **`double`

`Delta`

— Delta threshold for a linear discriminant model

nonnegative scalar

Value of the Delta threshold for a linear discriminant model, specified as a
nonnegative scalar. If a coefficient of `obj`

has magnitude smaller
than `Delta`

, `obj`

sets this coefficient to
`0`

, and so you can eliminate the corresponding predictor from the
model. Set `Delta`

to a higher value to eliminate more
predictors.

`Delta`

must be `0`

for quadratic discriminant
models.

Change `Delta`

using dot notation: ```
obj.Delta =
newDelta
```

.

**Data Types: **`double`

`DeltaPredictor`

— Minimum value of Delta coefficient for predictor to be in model

row vector of length `p`

This property is read-only.

Minimum value of Delta coefficient for predictor to be in model, specified as a row
vector of length `p`

, where `p`

is the number of
predictors in `obj`

. If
`DeltaPredictor(i) < Delta`

then coefficient
`i`

of the model is `0`

.

If `obj`

is a quadratic discriminant model, all elements of
`DeltaPredictor`

are `0`

.

**Data Types: **`double`

`DiscrimType`

— Discriminant type

character vector

Discriminant type, specified as a character vector or string. Available values:

`'linear'`

`'quadratic'`

`'diagLinear'`

`'diagQuadratic'`

`'pseudoLinear'`

`'pseudoQuadratic'`

Change `DiscrimType`

using dot notation: ```
obj.DiscrimType =
newDiscrimType
```

. You can change between linear types, or between quadratic
types, but cannot change between linear and quadratic types.

**Data Types: **`char`

| `string`

`Gamma`

— Gamma regularization parameter

scalar from `0`

through `1`

Value of the Gamma regularization parameter, specified as a scalar from
`0`

through `1`

. Change `Gamma`

using dot notation: `obj.Gamma = newGamma`

.

If you set

`1`

for linear discriminant, the discriminant sets its type to`'diagLinear'`

.If you set a value between

`MinGamma`

and`1`

for linear discriminant, the discriminant sets its type to`'linear'`

.You cannot set values below the value of the

`MinGamma`

property.For quadratic discriminant, you can set either

`0`

(for`DiscrimType`

`'quadratic'`

) or`1`

(for`DiscrimType`

`'diagQuadratic'`

).

**Data Types: **`double`

`LogDetSigma`

— Logarithm of determinant of within-class covariance matrix

scalar | vector

This property is read-only.

Logarithm of the determinant of the within-class covariance matrix, returned as a
scalar or vector. The type of `LogDetSigma`

depends on the discriminant
type:

Scalar for linear discriminant analysis

Vector of length

`K`

for quadratic discriminant analysis, where`K`

is the number of classes

**Data Types: **`double`

`MinGamma`

— Minimal value of Gamma parameter so that correlation matrix is invertible

nonnegative scalar

This property is read-only.

Minimal value of the Gamma parameter so that the correlation matrix is invertible,
returned as a nonnegative scalar. If the correlation matrix is not singular,
`MinGamma`

is `0`

.

`Mu`

— Class means

real `K`

-by-`p`

matrix

This property is read-only.

Class means, specified as a `K`

-by-`p`

matrix of
real values. `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: **`double`

`PredictorNames `

— Names of predictor variables

cell array

This property is read-only.

Names of predictor variables, returned as a cell array. The names are in the order in
which they appear in the training data `X`

.

**Data Types: **`cell`

`Prior`

— Prior probabilities for each class

numeric vector

Prior probabilities for each class, returned as a numeric vector. The order of the
elements of `Prior`

corresponds to the order of the classes in
`ClassNames`

.

Add or change a `Prior`

vector using dot notation: ```
obj.Prior
= priorVector
```

.

**Data Types: **`double`

`ResponseName`

— Name of the response variable `Y`

character vector

This property is read-only.

Name of the response variable `Y`

, returned as a character
vector.

**Data Types: **`char`

| `string`

`ScoreTransform`

— Score transformation function

name of a built-in function | function handle | `'none'`

Score transformation function, specified as a character vector or string representing
a built-in transformation function, or as a function handle for transforming scores.
`'none'`

means no transformation; equivalently,
`'none'`

means `@(x)x`

. For a list of built-in
transformation functions and the syntax of custom transformation functions, see
`fitcdiscr`

.

Implement dot notation to add or change a `ScoreTransform`

function
using one of the following:

`cobj.ScoreTransform = '`

'`function`

`cobj.ScoreTransform = @`

`function`

**Data Types: **`char`

| `string`

| `function_handle`

`Sigma`

— Within-class covariance

numeric array

This property is read-only.

Within-class covariance, returned as a numeric array. The dimensions depend on
`DiscrimType`

:

`'linear'`

(default) — Matrix of size`p`

-by-`p`

, where`p`

is the number of predictors`'quadratic'`

— Array of size`p`

-by-`p`

-by-`K`

, where`K`

is the number of classes`'diagLinear'`

— Row vector of length`p`

`'diagQuadratic'`

— Array of size`1`

-by-`p`

-by-`K`

`'pseudoLinear'`

— Matrix of size`p`

-by-`p`

`'pseudoQuadratic'`

— Array of size`p`

-by-`p`

-by-`K`

**Data Types: **`double`

## Object Functions

`compareHoldout` | Compare accuracies of two classification models using new data |

`edge` | Classification edge for discriminant analysis classifier |

`lime` | Local interpretable model-agnostic explanations (LIME) |

`logp` | Log unconditional probability density for discriminant analysis classifier |

`loss` | Classification loss for discriminant analysis classifier |

`mahal` | Mahalanobis distance to class means of discriminant analysis classifier |

`margin` | Classification margins for discriminant analysis classifier |

`nLinearCoeffs` | Number of nonzero linear coefficients in discriminant analysis classifier |

`partialDependence` | Compute partial dependence |

`plotPartialDependence` | Create partial dependence plot (PDP) and individual conditional expectation (ICE) plots |

`predict` | Predict labels using discriminant analysis classifier |

`shapley` | Shapley values |

## Examples

### Construct a Compact Discriminant Analysis Classifier

Load the sample data.

`load fisheriris`

Construct a discriminant analysis classifier for the sample data.

fullobj = fitcdiscr(meas,species);

Construct a compact discriminant analysis classifier, and compare its size to that of the full classifier.

cobj = compact(fullobj); b = whos('fullobj'); % b.bytes = size of fullobj c = whos('cobj'); % c.bytes = size of cobj [b.bytes c.bytes] % shows cobj uses 60% of the memory

`ans = `*1×2*
18610 11847

The compact classifier is smaller than the full classifier.

### Construct Classifier Using Means and Covariances

Construct a compact 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; cpct = makecdiscr(mu,sigstar,... 'ClassNames',{'setosa','versicolor','virginica'});

## More About

### Discriminant Classification

The model for discriminant analysis is:

Each class (

`Y`

) generates data (`X`

) using a multivariate normal distribution. That is, the model assumes`X`

has a Gaussian mixture distribution (`gmdistribution`

).For linear discriminant analysis, the model has the same covariance matrix for each class, only the means vary.

For quadratic discriminant analysis, both means and covariances of each class vary.

`predict`

classifies so as to minimize the expected
classification cost:

$$\widehat{y}=\underset{y=1,\mathrm{...},K}{\mathrm{arg}\mathrm{min}}{\displaystyle \sum _{k=1}^{K}\widehat{P}\left(k|x\right)C\left(y|k\right)},$$

where

$$\widehat{y}$$ is the predicted classification.

*K*is the number of classes.$$\widehat{P}\left(k|x\right)$$ is the posterior probability of class

*k*for observation*x*.$$C\left(y|k\right)$$ is the cost of classifying an observation as

*y*when its true class is*k*.

For details, see Prediction Using Discriminant Analysis Models.

### Regularization

Regularization is the process of finding a small set of predictors
that yield an effective predictive model. For linear discriminant
analysis, there are two parameters, *γ* and *δ*,
that control regularization as follows. `cvshrink`

helps
you select appropriate values of the parameters.

Let Σ represent the covariance matrix of the data *X*,
and let $$\widehat{X}$$ be the centered data (the data *X* minus
the mean by class). Define

$$D=\text{diag}\left({\widehat{X}}^{T}*\widehat{X}\right).$$

The regularized covariance matrix $$\tilde{\Sigma}$$ is

$$\tilde{\Sigma}=\left(1-\gamma \right)\Sigma +\gamma D.$$

Whenever *γ* ≥ `MinGamma`

, $$\tilde{\Sigma}$$ is nonsingular.

Let *μ _{k}* be the
mean vector for those elements of

*X*in class

*k*, and let

*μ*

_{0}be the global mean vector (the mean of the rows of

*X*). Let

*C*be the correlation matrix of the data

*X*, and let $$\tilde{C}$$ be the regularized correlation matrix:

$$\tilde{C}=\left(1-\gamma \right)C+\gamma I,$$

where *I* is the identity matrix.

The linear term in the regularized discriminant analysis classifier
for a data point *x* is

$${\left(x-{\mu}_{0}\right)}^{T}{\tilde{\Sigma}}^{-1}\left({\mu}_{k}-{\mu}_{0}\right)=\left[{\left(x-{\mu}_{0}\right)}^{T}{D}^{-1/2}\right]\left[{\tilde{C}}^{-1}{D}^{-1/2}\left({\mu}_{k}-{\mu}_{0}\right)\right].$$

The parameter *δ* enters into this equation
as a threshold on the final term in square brackets. Each component
of the vector $$\left[{\tilde{C}}^{-1}{D}^{-1/2}\left({\mu}_{k}-{\mu}_{0}\right)\right]$$ is set to zero
if it is smaller in magnitude than the threshold *δ*.
Therefore, for class *k*, if component *j* is
thresholded to zero, component *j* of *x* does
not enter into the evaluation of the posterior probability.

The `DeltaPredictor`

property is a vector related
to this threshold. When *δ* ≥ `DeltaPredictor(i)`

, all classes *k* have

$$\left|{\tilde{C}}^{-1}{D}^{-1/2}\left({\mu}_{k}-{\mu}_{0}\right)\right|\le \delta .$$

Therefore, when *δ* ≥ `DeltaPredictor(i)`

, the regularized
classifier does not use predictor `i`

.

## Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

The

`predict`

function supports code generation.When you train a discriminant analysis model by using

`fitcdiscr`

or create a compact discriminant analysis model by using`makecdiscr`

, the value of the`'ScoreTransform'`

name-value pair argument cannot be an anonymous function.

For more information, see Introduction to Code Generation.

## Version History

**Introduced in R2011b**

## See Also

`ClassificationDiscriminant`

| `compact`

| `makecdiscr`

| `fitcdiscr`

| `predict`

| `compareHoldout`

## 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)