⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gaussian_classify_mfold.m

📁 两个模式识别算法实现
💻 M
字号:
function [confmat,acc,accstd]=gaussian_classify_Mfold(data,labels,M,covflag)
% function [confmat,acc,accstd]=gaussian_classify_Mfold(data,labels,M,covflag)
% Matlab function to do Gaussian model classification over entire set
% M-fold crossvalidation for training/test, balanced by class
% Inputs:  data matrix must be n (instances) x d (features)
%          labels must be n (instances) x 1, with the class
%                 labels as unique integers 1,2,...,c
%          M is integer number of folds (make sure you have enough data)
%          covflag is 1 for full covariance, 0 for diag. covariance
% Outputs: confmat is confusion matrix
%          acc is overall accuracy
%          accstd is standard deviation of accuracy across folds
% Author:  Mike Johnson, Marquette University, March 26, 2009

if (nargin<3)
    M=2;           % default is 2-fold cross validation
end

if (nargin<4)
    covflag=1;     % default is full covariance model
end

ntot=size(data,1); % Total number of examples
c=max(labels);     % Labels must be integers 1..c

confmat = cell(1,M);
acc = cell(1,M);
accstd = cell(1,M);

% Split dataset into M class-balanced folds
% After this, to pull out a specific label i and fold j, just use
% "labelids=find(labels==i); foldids=labelids(folds(labelids)==j); "

folds=zeros(ntot,1);
for i=1:c
    labelids=find(labels==i);              % indices of class i examples
    n=length(labelids);                    % n=number of class i examples
    if (n<M)
        error('Not enough examples in class %d to support %d-fold crossvalidation.',i,M);
    end
    if (n<3*M)
        warning('Warning: Fewer than 3 examples per fold in class %d.',i);
    end
    permutedids=labelids(randperm(n));     % permuted indices
    startinds=1+floor(n*(0:(M-1))/M);      % starting points of folds
    endinds=floor(n*(1:M)/M);              % ending points of folds
    for m=1:M
        folds(permutedids(startinds(m):endinds(m)))=m;    % assign folds
    end
end

% Run classification with each fold used once as test data

for m=1:M
    confmat{1,m}=zeros(c,c);
    temptot=0;
    for i=1:c                               % select train/test data
        labelids=find(labels==i);
        testids=labelids(folds(labelids)==m);
        trainids=labelids(folds(labelids)~=m);
        testdata{i}=data(testids,:);
        traindata{i}=data(trainids,:);
        
        c_mean{i}=mean(traindata{i});      % TRAIN
        c_cov{i}=cov(traindata{i});
        ntrn(i)=length(trainids);
        temptot=temptot+ntrn(i);
    end
    priors = ntrn/temptot;

    foldconfmat=zeros(c,c);
    for i=1:c                              % TEST
        ntst=size(testdata{i},1);
        for x=1:ntst
            fvec=testdata{i}(x,:);
            for j=1:c
                if covflag
                    dis(j)=discr(fvec,c_mean{j},c_cov{j},priors(j)); % MV Gauss w/ priors
                else
                    dis(j)=discr2(fvec,c_mean{j},c_cov{j},priors(j)); % MV diag Gauss w/ priors
                end
            end
            [tmp,maxind]=max(dis);
            foldconfmat(i,maxind)=foldconfmat(i,maxind)+1;
        end
    end                                    
    confmat{1,m}=confmat{1,m}+foldconfmat;
    foldacc(m)=sum(diag(foldconfmat))/sum(sum(foldconfmat));
    acc{1,m}=sum(diag(confmat{1,m}))/sum(sum(confmat{1,m}));
    accstd{1,m}=std(foldacc);
end




function value=discr(x,mu,sigma,prior)
% Gaussian discriminant function
x=x(:);  % make into a column vector
mu=mu(:);
i=0; dimen=size(sigma,1);
while (rcond(sigma)<10^-12)
    sigma=sigma+eye(dimen)*eps*10^i; i=i+1;
end
invsig=inv(sigma);
value=-0.5*(x-mu)'*invsig*(x-mu)-0.5*log(det(sigma))+log(prior);


function value=discr2(x,mu,sigma,prior)  % diagonal covariance
% Gaussian discriminant function
x=x(:);  % make into a column vector
mu=mu(:);
sigma=diag(max(diag(sigma),10^-12));  % Only use diagonal
invsig=inv(sigma);
value=-0.5*(x-mu)'*invsig*(x-mu)-0.5*log(det(sigma))+log(prior);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -