Sie sind auf Seite 1von 4

3/22/13

Discriminant analysis - MATLAB classify - MathWorks India

classify
Discriminant analysis

Syntax
class = classify(sample,training,group)
class = classify(sample,training,group,'type')
class = classify(sample,training,group,'type',prior)
[class,err] = classify(...)
[class,err,POSTERIOR] = classify(...)
[class,err,POSTERIOR,logp] = classify(...)
[class,err,POSTERIOR,logp,coeff] = classify(...)

Description
class = classify(sample,training,group)classifies each row of the data in
sampleinto one of the groups in training. sampleand trainingmust be matrices w ith
the same number of columns. groupis a grouping variable for training. Its unique values
define groups; each element defines the group to w hich the corresponding row of training
belongs. groupcan be a categorical variable, a numeric vector, a string array, or a cell array of
strings. trainingand groupmust have the same number of row s. classifytreats NaNs
or empty strings in groupas missing values, and ignores the corresponding row s of
training. The output classindicates the group to w hich each row of samplehas been
assigned, and is of the same type as group.
class = classify(sample,training,group,'type')allow s you to specify the
type of discriminant function. Specify typeinside single quotes. typeis one of:
linear Fits a multivariate normal density to each group, w ith a pooled estimate of
covariance. This is the default.
diaglinear Similar to linear, but w ith a diagonal covariance matrix estimate (naive
Bayes classifiers).
quadratic Fits multivariate normal densities w ith covariance estimates stratified by
group.
diagquadratic Similar to quadratic, but w ith a diagonal covariance matrix estimate
(naive Bayes classifiers).
mahalanobis Uses Mahalanobis distances w ith stratified covariance estimates.
class = classify(sample,training,group,'type',prior)allow s you to
specify prior probabilities for the groups. prioris one of:
A numeric vector the same length as the number of unique values in group(or the number
of levels defined for group, if groupis categorical). If groupis numeric or categorical, the
order of priormust correspond to the ordered values in group, or, if groupcontains
strings, to the order of first occurrence of the values in group.
A 1-by-1 structure w ith fields:
prob A numeric vector.
group Of the same type as group, containing unique values indicating the groups
to w hich the elements of probcorrespond.
As a structure, priorcan contain groups that do not appear in group. This can be useful
if trainingis a subset a larger training set. classifyignores any groups that appear in
the structure but not in the grouparray.
The string 'empirical', indicating that group prior probabilities should be estimated from
www.mathworks.in/help/stats/classify.html

1/4

3/22/13

Discriminant analysis - MATLAB classify - MathWorks India

the group relative frequencies in training.


priordefaults to a numeric vector of equal probabilities, i.e., a uniform distribution. prioris
not used for discrimination by Mahalanobis distance, except for error rate calculation.
[class,err] = classify(...)also returns an estimate errof the misclassification
error rate based on the trainingdata. classifyreturns the apparent error rate, i.e., the
percentage of observations in trainingthat are misclassified, w eighted by the prior
probabilities for the groups.
[class,err,POSTERIOR] = classify(...)also returns a matrix POSTERIORof
estimates of the posterior probabilities that the jth training group w as the source of the ith sample
observation, i.e., Pr(group j|obs i). POSTERIORis not computed for Mahalanobis discrimination.
[class,err,POSTERIOR,logp] = classify(...)also returns a vector logp
containing estimates of the logarithms of the unconditional predictive probability density of the
sample observations, p(obs i) = p(obs i|group j)Pr(group j) over all groups. logpis not
computed for Mahalanobis discrimination.
[class,err,POSTERIOR,logp,coeff] = classify(...)also returns a structure
array coeffcontaining coefficients of the boundary curves betw een pairs of groups. Each
element coeff(I,J)contains information for comparing group Ito group Jin the follow ing
fields:
type Type of discriminant function, from the typeinput.
name1 Name of the first group.
name2 Name of the second group.
const Constant term of the boundary equation (K)
linear Linear coefficients of the boundary equation (L)
quadratic Quadratic coefficient matrix of the boundary equation (Q)
For the linearand diaglineartypes, the quadraticfield is absent, and a row xfrom the
samplearray is classified into group Irather than group Jif 0 < K+x*L. For the other types,
xis classified into group Iif 0 < K+x*L+x*Q*x'.

Examples
For training data, use Fisher's sepal measurements for iris versicolor and virginica:
load fisheriris
SL = meas(51:end,1);
SW = meas(51:end,2);
group = species(51:end);
h1 = gscatter(SL,SW,group,'rb','v^',[],'off');
set(h1,'LineWidth',2)
legend('Fisher versicolor','Fisher virginica',...
'Location','NW')

www.mathworks.in/help/stats/classify.html

2/4

3/22/13

Discriminant analysis - MATLAB classify - MathWorks India

Classify a grid of measurements on the same scale:


[X,Y] = meshgrid(linspace(4.5,8),linspace(2,4));
X = X(:); Y = Y(:);
[C,err,P,logp,coeff] = classify([X Y],[SL SW],...
group,'quadratic');
Visualize the classification:
hold on;
gscatter(X,Y,C,'rb','.',1,'off');
K = coeff(1,2).const;
L = coeff(1,2).linear;
Q = coeff(1,2).quadratic;
% Function to compute K + L*v + v'*Q*v for multiple vectors
% v=[x;y]. Accepts x and y as scalars or column vectors.
f = @(x,y) K + [x y]*L + sum(([x y]*Q) .* [x y], 2);
h2 = ezplot(f,[4.5 8 2 4]);
set(h2,'Color','m','LineWidth',2)
axis([4.5 8 2 4])
xlabel('Sepal Length')
ylabel('Sepal Width')
title('{\bf Classification with Fisher Training Data}')

www.mathworks.in/help/stats/classify.html

3/4

3/22/13

Discriminant analysis - MATLAB classify - MathWorks India

More About
Grouping Variables

References
[1] Krzanow ski, W. J. Principles of Multivariate Analysis: A User's Perspective. New York:
Oxford University Press, 1988.
[2] Seber, G. A. F. Multivariate Observations. Hoboken, NJ: John Wiley & Sons, Inc., 1984.

See Also
classregtree| mahal| NaiveBayes

Was this topic helpful?

Yes

No

Try MATLAB, Simulink, and Other Products


Get trial now

www.mathworks.in/help/stats/classify.html

4/4

Das könnte Ihnen auch gefallen