predict
uses three quantities to classify observations:posterior probability,prior probability, andcost。
predict
classifies so as to minimize the expected classification cost:
where
is the predicted classification.
Kis the number of classes.
is the posterior probability of classkfor observationx。
is the cost of classifying an observation asywhen its true class isk。
The space ofX
values divides into regions where a classificationY
is a particular value. The regions are separated by straight lines for linear discriminant analysis, and by conic sections (ellipses, hyperbolas, or parabolas) for quadratic discriminant analysis. For a visualization of these regions, seeCreate and Visualize Discriminant Analysis Classifier。
The posterior probability that a pointxbelongs to classkis the product of theprior probabilityand the multivariate normal density. The density function of the multivariate normal with 1-by-dmeanμkandd-by-dcovariance Σkat a 1-by-dpointxis
where is the determinant of Σk, and is the inverse matrix.
LetP(k) represent the prior probability of classk。Then the posterior probability that an observationxis of classkis
whereP(x) is a normalization constant, namely, the sum overkofP(x|k)P(k).
The prior probability is one of three choices:
'uniform'
— The prior probability of classk
is 1 over the total number of classes.
'empirical'
— The prior probability of classk
is the number of training samples of classk
divided by the total number of training samples.
A numeric vector — The prior probability of classk
is thej
th element of thePrior
vector. Seefitcdiscr
。
After creating a classifierobj
, you can set the prior using dot notation:
obj.Prior = v;
wherev
is a vector of positive elements representing the frequency with which each element occurs. You do not need to retrain the classifier when you set a new prior.
There are two costs associated with discriminant analysis classification: the true misclassification cost per class, and the expected misclassification cost per observation.
Cost(i,j)
is the cost of classifying an observation into classj
if its true class isi
。By default,Cost(i,j)=1
ifi~=j
, andCost(i,j)=0
ifi=j
。换句话说,是成本0
for correct classification, and1
for incorrect classification.
You can set any cost matrix you like when creating a classifier. Pass the cost matrix in theCost
name-value pair infitcdiscr
。
After you create a classifierobj
, you can set a custom cost using dot notation:
obj.Cost = B;
B
is a square matrix of sizeK
-by-K
when there areK
classes. You do not need to retrain the classifier when you set a new cost.
Suppose you haveNobs
observations that you want to classify with a trained discriminant analysis classifierobj
。Suppose you haveK
classes. You place the observations into a matrixXnew
with one observation per row. The command
[label,score,cost] = predict(obj,Xnew)
回报,等欧tputs, a cost matrix of sizeNobs
-by-K
。Each row of the cost matrix contains the expected (average) cost of classifying the observation into each of theK
classes.cost(n,k)
is
where
Kis the number of classes.
is theposterior probabilityof classifor observationXnew(n).
is thecostof classifying an observation askwhen its true class isi。