📄 mltest1.m
字号:
function [class,Distan]=MLtest1(Pt,Tt,Cen,cinv,ppri,mode)
% Usage: [class,conf]=MLtest1(Pt,Tt,Cen,cinv,ppri,mode)
% MLtest - Maximum Likelihood Classifier testing routing
% Pt: testing set feature vector Q by N
% Tt: testing set target vector Q by S
% S: number of classes
% N: feature vector dimension
% K: # of feature vectors in training set
% Q: # of feature vectors in testing/validate set
% Cen: centroid of each cluster
% cinv: an array of inverse of covariance matrix of each class
% ppri: a priori probability = fraction of samples in each class
% class: classification result Q by S
% conf: 1 x Q vector.
% confidence = probability x is in class i given the observation vector Pt
% copyright (c) 2001 Yu Hen Hu
% Last revision 8/10/2001
[Q,N]=size(Pt);
ptnorm=ones(Q,1)./sqrt(sum(Pt'.*Pt')'); % Q X 1 vector
Pt = (ptnorm*ones(1,N)).*Pt; % make each row unity norm
[Q,S]=size(Tt);
if nargin<=4,
ppri=ones(1,S)/S;
end
if S==1, % if only one output with 0 in one class
% and 1 the other, change it to 2 outputs
Tt=[Tt ones(Q,1)-Tt];
S=2;
end
% Next, calculating the distance between test vector and i-th class Gaussian
% using (X-Cen(cn,:))*C{i}^-1*(X-Cen(cn,:)) this is the negative log likelihood
for cn=1:S,
if mode==0,
tmpd=(Pt-ones(Q,1)*Cen(cn,:)); % Q x N
elseif mode==1 | mode ==2,
tmpd=Pt;
end
tmpe=tmpd*cinv{cn}; % Q x N
Distan(cn,:) = sum(tmpe'.*tmpd'); % 1 X Q
end
% Distan is S x Q matrix.
% Now incorporating the a priori probability to give a MAP classifier
if mode==1|mode==0,
Distan = Distan-diag(diag(ppri))*ones(1,Q);
end
[tmp,ind]=min(Distan);
% now calculate the confidence of the classification result.
% the confidence is calculated as the a posterior probability of the decision
%
% edist=exp(-Distan); % S x Q matrix containing est. of posterior prob of each class% edsum=ones(1,Q)./sum(edist); % 1 x Q sum of the posterior prob.
% ppmat=ones(S,1)*edsum.*edist; % matrix of posterior probability
%Now try to find the maximum posterior probability
%
%[conf,ind]=max(ppmat); % given max element and index
% Now test with Pt, Tt
Idmat=eye(S);
class=Idmat(ind,:); % Q x S
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -