📄 mixtureem.m
字号:
function [c,z,pi,w,Q] = mixtureEM(data,K,sigma,z0,feedback)% mixtureEM : cluster by estimating a mixture of Gaussians% [c,z,pi,w,Q] = mixtureEM(data,K,sigma,z0,feedback)% data - d*n samples% K - number of clusters wanted% sigma - standard deviation of components% z0 - d*K initial guess for component means% feedback - function feedback(i,data,stats,theta,Q)% returns:% c - 1*n calculated membership vector where c(j) \in 1..K% z - d*K cluster centroids% pi - K*1 mixture coefficients% w - K*n soft assignment matrix% Q - maximized expected log likelihood% Copyright (c) 2001 Frank Dellaert% All rights Reserved% Check argumentsif ndims(data)~=2, error('data must be a matrix'); end[d,n] = size(data);if ~isequal(size(K),[1 1]), error('K must be a scalar');endK=round(K);if (K<1 | K>n), error('K must be at least one and at most n'); endif nargin<4,z0=[];endif nargin<5,feedback=@dummy;end% initialize cluster centers and mixture coeffpermutation=randperm(n);if isempty(z0) theta0.z = data(:,permutation(1:K));else theta0.z = z0;endtheta0.pi = ones(K,1)/K;% call EMparameters.Eparameters.sigma=sigma;parameters.Mparameters.sigma=sigma;[theta,w,Q] = EM(theta0,data,@Estep,@Mstep,feedback,parameters);% w are the soft assignments, turn into crisp assignment[maxw,c]=max(w);z=theta.z;pi=theta.pi;%------------------------------------------------------------% Estepfunction w = Estep(theta,data,parameters)% calculate log-likelihood for all K*n possible assignmentsE = sqrDist(data,theta.z);% turn into unnormalized posterior probability q% taking some care to avoid very small numbersloglikelihood=-0.5*E/parameters.sigma^2;maxll=max(loglikelihood);K=size(E,1);likelihood=exp(loglikelihood-ones(K,1)*maxll);q=full(spdiags(theta.pi,0,K,K)*likelihood);% normalizew = q./(ones(K,1)*sum(q));%------------------------------------------------------------% Mstepfunction [theta,Q] = Mstep(theta,data,w,parameters)% re-estimate component means by weighted averageK=size(theta.z,2);for i=1:K wi=transpose(w(i,:)); theta.z(:,i) = data * wi / sum(wi); % d*1 = d*n * n*1 / 1*1end% re-estimate mixture coefficientstheta.pi=sum(w,2);theta.pi=theta.pi/sum(theta.pi);% calculate Q% should not have to do this twice (next time in E-step, I mean)E = sqrDist(data,theta.z); % K*nQ = -0.5*sum(sum(E.*w))/parameters.sigma^2 + sum(log(theta.pi).*sum(w,2));%------------------------------------------------------------% dummy feedbackfunction dummy(i,data,w,theta,Q)%------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -