ClassificationDiscriminant
Create a ClassificationDiscriminant
class object containing a
discriminant analysis model.
obj = ClassificationDiscriminant (X, Y)
returns a
ClassificationDiscriminant object, with X as the predictor data
and Y containing the class labels of observations in X.
X
must be a numeric matrix of input data where rows
correspond to observations and columns correspond to features or variables.
X will be used to train the discriminant model.
Y
is matrix or cell matrix containing the class labels of
corresponding predictor data in X. Y can contain any type of
categorical data. Y must have the same number of rows as X.
obj = ClassificationDiscriminant (…, name,
value)
returns a ClassificationDiscriminant object with parameters
specified by Name-Value
pair arguments.
Type help ClassificationDiscriminant
for more info.
A ClassificationDiscriminant
object, obj, stores the labeled
training data and various parameters for the discriminant analysis model,
which can be accessed in the following fields:
Field | Description | |
---|---|---|
X | Unstandardized predictor data, specified as a numeric matrix. Each column of X represents one predictor (variable), and each row represents one observation. | |
Y | Class labels, specified as a logical, numeric vector, or cell array of character vectors. Each value in Y is the observed class label for the corresponding row in X. | |
NumObservations | Number of observations used in training the ClassificationDiscriminant model, specified as a positive integer scalar. | |
RowsUsed | Rows of the original training data used in fitting the ClassificationDiscriminant model, specified as a numerical vector. | |
PredictorNames | Predictor variable names, specified as a cell array of character vectors. The variable names are in the same order in which they appear in the training data X. | |
ResponseName | Response variable name, specified as a character vector. | |
ClassNames | Names of the classes in the training data Y with duplicates removed, specified as a cell array of character vectors. | |
Prior | Prior probabilities for each class,
specified as a numeric vector. The order of the elements in Prior
corresponds to the order of the classes in ClassNames . | |
Cost | Cost of the misclassification of 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 (that is, the rows
correspond to the true class and the columns correspond to the predicted
class). The order of the rows and columns in 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. By default,
Cost(i,j) = 1 if i != j , and Cost(i,j) = 0 if
i = j . In other words, the cost is 0 for correct classification and
1 for incorrect classification. | |
Sigma | Within-class covariance matrix, specified as a numeric matrix. For ’linear’ discriminant type matrix is of size , where p is the number of predictors. | |
Mu | Class means, specified as a real matrix. K is the number of classes, and p is the number of predictors. | |
Coeffs | Coefficient matrices, specified as a struct array. | |
Delta | Threshold for linear discriminant model, specified as a numeric scalar. | |
DiscrimType | Discriminant type, specified as a character vector. | |
Gamma | Gamma regularization parameter, specified as a numeric scalar. | |
MinGamma | Minimum value of Gamma so that the correlation matrix is invertible, specified as nonnegative scalar. | |
LogDetSigma | Logarithm of the determinant of the within-class covariance matrix. For linear discriminant analysis it is specified as a numeric scalar. | |
XCentered | X data with class means subtracted, returned as a real matrix. |
See also: fitcdiscr
Source Code: ClassificationDiscriminant
compact
Create a CompactClassificationDiscriminant object.
CVMdl = compact (obj)
creates a compact version of the
ClassificationDiscriminant object, obj.
See also: fitcdiscr, ClassificationDiscriminant, CompactClassificationDiscriminant
crossval
Cross Validate a Discriminant classification object.
CVMdl = crossval (obj)
returns a cross-validated model
object, CVMdl, from a trained model, obj, using 10-fold
cross-validation by default.
CVMdl = crossval (obj, name, value)
specifies additional name-value pair arguments to customize the
cross-validation process.
Name | Value | |
---|---|---|
"KFold" | Specify the number of folds to use in
k-fold cross-validation. "KFold", k , where k is an
integer greater than 1. | |
"Holdout" | Specify the fraction of the data to
hold out for testing. "Holdout", p , where p is a
scalar in the range . | |
"Leaveout" | Specify whether to perform
leave-one-out cross-validation. "Leaveout", Value , where
Value is ’on’ or ’off’. | |
"CVPartition" | Specify a cvpartition
object used for cross-validation. "CVPartition", cv , where
isa (cv, "cvpartition") = 1. |
See also: fitcdiscr, ClassificationDiscriminant, cvpartition, ClassificationPartitionedModel
loss
Compute loss for a trained ClassificationDiscriminant object.
L = loss (obj, X, Y)
computes the loss,
L, using the default loss function 'mincost'
.
obj
is a ClassificationDiscriminant object trained on
X
and Y
.
X
must be a numeric matrix of input data where rows
correspond to observations and columns correspond to features or
variables.
Y
is matrix or cell matrix containing the class labels
of corresponding predictor data in X. Y must have same
numbers of Rows as X.
L = loss (…, name, value)
allows
additional options specified by name-value pairs:
Name | Value | |
---|---|---|
"LossFun" | Specifies the loss function to use.
Can be a function handle with four input arguments (C, S, W, Cost)
which returns a scalar value or one of:
’binodeviance’, ’classifcost’, ’classiferror’, ’exponential’,
’hinge’, ’logit’,’mincost’, ’quadratic’.
| |
"Weights" | Specifies observation weights, must be
a numeric vector of length equal to the number of rows in X.
Default is ones (size (X, 1)) . loss normalizes the weights so that
observation weights in each class sum to the prior probability of that
class. When you supply Weights, loss computes the weighted
classification loss. |
See also: ClassificationDiscriminant
margin
m = margin (obj, X, Y)
returns
the classification margins for obj with data X and
classification Y. m is a numeric vector of length size (X,1).
obj
is a ClassificationDiscriminant object trained on X
and Y
.
X
must be a numeric matrix of input data where rows
correspond to observations and columns correspond to features or
variables.
Y
is matrix or cell matrix containing the class labels
of corresponding predictor data in X. Y must have same
numbers of Rows as X.
The classification margin for each observation is the difference between the classification score for the true class and the maximal classification score for the false classes.
See also: fitcdiscr, ClassificationDiscriminant
predict
Classify new data points into categories using the discriminant analysis model from a ClassificationDiscriminant object.
label = predict (obj, XC)
returns the vector of
labels predicted for the corresponding instances in XC, using the
predictor data in obj.X
and corresponding labels, obj.Y
,
stored in the ClassificationDiscriminant model, obj.
ClassificationDiscriminant
class object.
[label, score, cost] = predict (obj,
XC)
also returns score, which contains the predicted class
scores or posterior probabilities for each instance of the corresponding
unique classes, and cost, which is a matrix containing the expected
cost of the classifications.
The score matrix contains the posterior probabilities for each class, calculated using the multivariate normal probability density function and the prior probabilities of each class. These scores are normalized to ensure they sum to 1 for each observation.
The cost matrix contains the expected classification cost for each class, computed based on the posterior probabilities and the specified misclassification costs.
See also: ClassificationDiscriminant, fitcdiscr
savemodel
Save a ClassificationDiscriminant object.
savemodel (obj, filename)
saves a
ClassificationDiscriminant object into a file defined by filename.
See also: loadmodel, fitcdiscr, ClassificationDiscriminant
## Create discriminant classifier ## Evaluate some model predictions on new data. load fisheriris x = meas; y = species; xc = [min(x); mean(x); max(x)]; obj = fitcdiscr (x, y); [label, score, cost] = predict (obj, xc); |
load fisheriris model = fitcdiscr (meas, species); X = mean (meas); Y = {'versicolor'}; ## Compute loss for discriminant model L = loss (model, X, Y) L = 0 |
load fisheriris mdl = fitcdiscr (meas, species); X = mean (meas); Y = {'versicolor'}; ## Margin for discriminant model m = margin (mdl, X, Y) m = 1.0000 |
load fisheriris x = meas; y = species; obj = fitcdiscr (x, y, "gamma", 0.4); ## Cross-validation for discriminant model CVMdl = crossval (obj) CVMdl = ClassificationPartitionedModel object with properties: BinEdges: [0x0 double] CategoricalPredictors: [0x0 double] ClassNames: [3x1 cell] Cost: [3x3 double] CrossValidatedModel: ClassificationDiscriminant KFold: [1x1 double] ModelParameters: [1x1 struct] NumObservations: [1x1 double] Partition: [1x1 cvpartition] PredictorNames: [1x4 cell] Prior: [3x1 double] ResponseName: Y ScoreTransform: none Standardize: [0x0 double] Trained: [10x1 cell] X: [150x4 double] Y: [150x1 cell] |