2011-03-23 25 views
1

我正在與2 features2000 rows作爲訓練分類數據xtrain matrix,所以尺寸爲2,μ是一個2元素矢量和Σ是covariancxe矩陣的2×2:分類與mvnpdf MATLAB

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 < =創建
Cov <時創建

當要分類數據已經知道=計算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; 

回答

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); 

做到這一點,我希望它幫助。

0

上述答案不正確(p = mvnpdf(textX,mu,sigma);)。該語句返回testX中每個點處的概率密度,但不返回實際概率。

+0

這應該是對該答案的評論,而不是答案本身。 – Andrew 2014-06-17 21:32:04

+0

嘿,我知道你還沒有足夠的代表評論,但**請**儘量避免寫評論作爲答案。這是**不是**的答案 – 2014-06-17 21:32:36