# nLinearCoeffs

Number of nonzero linear coefficients

## Syntax

`ncoeffs = nLinearCoeffs(obj)ncoeffs = nLinearCoeffs(obj,delta)`

## Description

`ncoeffs = nLinearCoeffs(obj)` returns the number of nonzero linear coefficients in the linear discriminant model `obj`.

`ncoeffs = nLinearCoeffs(obj,delta)` returns the number of nonzero linear coefficients for threshold parameter `delta`.

## Input Arguments

 `obj` Discriminant analysis classifier, produced using `fitcdiscr`. `delta` Scalar or vector value of the `Delta` parameter. See Gamma and Delta.

## Output Arguments

 `ncoeffs` Nonnegative integer, the number of nonzero coefficients in the discriminant analysis model `obj`. If you call `nLinearCoeffs` with a `delta` argument, `ncoeffs` is the number of nonzero linear coefficients for threshold parameter `delta`. If `delta` is a vector, `ncoeffs` is a vector with the same number of elements. If `obj` is a quadratic discriminant model, `ncoeffs` is the number of predictors in `obj`.

## Definitions

### Gamma and Delta

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 $\stackrel{^}{X}$ be the centered data (the data X minus the mean by class). Define

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

The regularized covariance matrix $\stackrel{˜}{\Sigma }$ is

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

Whenever γ ≥ `MinGamma`, $\stackrel{˜}{\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 $\stackrel{˜}{C}$ be the regularized correlation matrix:

`$\stackrel{˜}{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}{\stackrel{˜}{\Sigma }}^{-1}\left({\mu }_{k}-{\mu }_{0}\right)=\left[{\left(x-{\mu }_{0}\right)}^{T}{D}^{-1/2}\right]\left[{\stackrel{˜}{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[{\stackrel{˜}{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

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

Therefore, when δ ≥ `DeltaPredictor(i)`, the regularized classifier does not use predictor `i`.

## Examples

expand all

### Find the Number of Nonzero Coefficients in a Discriminant Analysis Classifier

Find the number of nonzero coefficients in a discriminant analysis classifier for various `Delta` values.

Create a discriminant analysis classifier from the `fishseriris` data.

```load fisheriris obj = fitcdiscr(meas,species);```

Find the number of nonzero coefficients in `obj`.

`ncoeffs = nLinearCoeffs(obj)`
```ncoeffs = 4```

Find the number of nonzero coefficients for `delta` = 1, 2, 4, and 8.

```delta = [1 2 4 8]; ncoeffs = nLinearCoeffs(obj,delta)```
```ncoeffs = 4 4 3 0```

The `DeltaPredictor` property gives the values of `delta` where the number of nonzero coefficients changes.

`ncoeffs2 = nLinearCoeffs(obj,obj.DeltaPredictor)`
```ncoeffs2 = 4 3 1 2```