用mvnpdf MATLAB分类

时间:2011-03-23 06:44:34

标签: matlab classification bayesian

我正在将数据xtrain matrix2 features2000 rows分类为训练,因此维度为2,μ为2元素向量,Σ为covariancxe矩阵2x2 :

xtrain =
    0.3630    1.6632
   -0.0098    1.8526
   -0.0424    1.6840
   -0.1565    2.1187
    0.5720   -2.7282
   -0.7808    1.1357
    0.5212   -0.6858
    0.1038    1.4735
    ...

mu = 0.3486 0.8327

sigma =
    1.1163    0.0452
    0.0452    1.5669

我正在做类似的事情:

mu           = mean(xtrain)
sigma        = cov(xtrain)
% 1/y^2 = (2 pi)^p |\Sigma| exp { (x-\mu)' inv(\Sigma) (x-\mu) }    
p = mvnpdf (xtrain, mu, sigma);

然后计算:

pdfgauss =...

问题是如何使用xtest matrix测试分类器的结果?

I was reading this and it says:

To classify data using Bayesian classifier we already know `Prior(w)` and need to compute `p(x/w)`. When `p` is multidimensioanl Gaussian, we can use Matlab internal function "`mvnpdf`".

示例)mvnpdf(X,Mean,Cov)

我们想要分类的

X <=数据 Mean&lt; =已创建时已知 Cov&lt; =已创建时已知

为每个类分类数据计算pdfgauss and multiply by Prior(w),并选择显示最大值的类

要使用这些函数,pdfgauss会使用某些东西来计算距离 dist = mahalan(X,Mean(:,i),Cov(:,:,i));

  • 如何完成此分类?

pdfgauss.m

function y = pdfgauss(X, arg1, arg2 )
% PDFGAUSS Evaluates multivariate Gaussian distribution.
%
% Synopsis:
%  y = pdfgauss(X, Mean, Cov)
%  y = pdfgauss(X, model )
%
% Description:
%  y = pdfgauss(X, Mean, Cov) evaluates a multi-variate Gaussian 
%  probability density function(s) for given input column vectors in X.
%  Mean [dim x ncomp] and Cov [dim x dim x ncomp] describe a set of 
%  ncomp Gaussian distributions to be evaluted such that
%
%  y(i,j) = exp(-0.5(mahalan(X(:,j),Mean(:,i),Cov(:,:,i) )))/norm_const
%
%  where i=1:ncomp and j=1:size(X,2). If the Gaussians are
%  uni-variate then the covariaves can be given as a vector
%  Cov = [Cov_1, Cov_2, ..., Cov_comp].
%
%  y = pdfgauss( X, model ) takes Gaussian parameters from structure
%  fields model.Mean and model.Cov.
%
% Input:
%  X [dim x num_data] Input matrix of column vectors.
%  Mean [dim x ncomp] Means of Gaussians.
%  Cov [dim x dim x ncomp] Covarince matrices.
%
% Output:
%  y [ncomp x num_data] Values of probability density function.
%
% Example:
% 
% Univariate case
%  x = linspace(-5,5,100);
%  y = pdfgauss(x,0,1);
%  figure; plot(x,y)
%
% Multivariate case
%  [Ax,Ay] = meshgrid(linspace(-5,5,100), linspace(-5,5,100));
%  y = pdfgauss([Ax(:)';Ay(:)'],[0;0],[1 0.5; 0.5 1]);
%  figure; surf( Ax, Ay, reshape(y,100,100)); shading interp;
%
% See also 
%  GSAMP, PDFGMM.
%

% About: Statistical Pattern Recognition Toolbox
% (C) 1999-2003, Written by Vojtech Franc and Vaclav Hlavac
% <a href="http://www.cvut.cz">Czech Technical University Prague</a>
% <a href="http://www.feld.cvut.cz">Faculty of Electrical Engineering</a>
% <a href="http://cmp.felk.cvut.cz">Center for Machine Perception</a>

% Modifications:
% 28-apr-2004, VF

% process input arguments
if nargin < 3,
  arg1 = c2s(arg1);
  Mean = arg1.Mean;
  Cov =  arg1.Cov;
else
  Mean = arg1;
  Cov =  arg2;
end

% get dimensions
[dim,num_data] = size(X);
ncomp = size(Mean,2);

% univariate variances can be given as a vector
if size(Cov,1) ~= size(Cov,2), Cov = reshape(Cov,1,1,ncomp); end

% alloc memory
y = zeros(ncomp,num_data);

% evaluate pdf for each component
for i=1:ncomp,
  dist = mahalan(X,Mean(:,i),Cov(:,:,i));
  y(i,:) = exp(-0.5*dist)/sqrt((2*pi)^dim*det(Cov(:,:,i)));
end

return;

2 个答案:

答案 0 :(得分:1)

我不太明白你想要分类的是什么 - 你有一个分布,一个均值,一个协方差。如果要进行分类,则需要一种函数作为分类器;

如果你有某种功能

[Mean1, Cov1, Mean2, Cov2] = ClassifyInto2Groups

然后你可以计算testX向量成为两组中任何一组的概率:

p_group1 = mvnpdf(testX, Mean1, Cov1)
p_group2 = mvnpdf(testX, Mean2, Cov2)

BelongToGroup = repmat(1, size(testX, 1));
BelongToGroup(p_group2>p_group1) = 2;

我写这个假设你想分成两组。如果你只需要计算testX属于trainX模型的概率,那么它就不是分类,你可以通过

来实现
p = mvnpdf (testX, mu, sigma);

我希望它有所帮助。

答案 1 :(得分:0)

上述答案不正确(p = mvnpdf(textX,mu,sigma);)。该语句返回testX中每个点的概率密度,它不返回实际概率。