In EM and GMM(Theory), I have introduced the theory of em algorithm for gmm. Now lets practice it in matlab!

1. Generate 1000 pieces of  random 2-dimention data which obey 5 gaussian distribution.

function X = GenerateData
Sigma = [1, 0; 0, 1];
mu1 = [1, -1];
x1 = mvnrnd(mu1, Sigma, 200);
mu2 = [5.5, -4.5];
x2 = mvnrnd(mu2, Sigma, 200);
mu3 = [1, 4];
x3 = mvnrnd(mu3, Sigma, 200);
mu4 = [6, 4.5];
x4 = mvnrnd(mu4, Sigma, 200);
mu5 = [9, 0.0];
x5 = mvnrnd(mu5, Sigma, 200);
% obtain the 1000 data points to be clustered
X = [x1; x2; x3; x4; x5];
end

2. Complete em algorithm.

function [Mu, Sigma, Pi, r_nk] = EmForGmm(Data, classNum, initVal)
% Data : Matrix(n * d), n is the quantity of the data and d is the data
% dimention
% classNum : Scale
% initVal : Cell(3 * 1), initial value for Mu, Sigma and Pi
% cell 1: Mu
% cell 2: Sigma
% cell 3: Pi
[sampleNum, sampleDim] = size(Data);
indexPoint = zeros(sampleNum, 1);
while(1)
for n = 1 : sampleNum
x = Data(n, :);
px_nk_sumk = 0;
for k = 1 : classNum
Sigma_k = initVal{2}(:,:,k);
Mu_k = initVal{1}(k,:);
Pi_k = initVal{3}(k);
px(n,k) = (1/(2*pi^(sampleDim/2)*det(Sigma_k)^(0.5))) ...
* exp(-0.5 * (x - Mu_k)*inv(Sigma_k)*(x - Mu_k)');
px_nk_sumk = px_nk_sumk + Pi_k * px(n, k);
end
for k = 1 : classNum
Sigma_k = initVal{2}(:,:,k);
Mu_k = initVal{1}(k,:);
Pi_k = initVal{3}(k);
r(n, k) = Pi_k * px(n, k) / px_nk_sumk;
end
end
Nk = sum(r)';
newMuK = r' * Data;
Nkk = repmat(Nk,1,2);
newMuK = newMuK ./ Nkk;
for i = 1 : classNum
nk = Nk(i);
MuT = repmat(newMuK(i,:),sampleNum,1);
xT = Data - MuT;
rT = r(:,i);
rT = repmat(rT,1,2);
newSigma(:,:,i) = xT' * (xT .* rT) / nk;
end
newPiK = Nk / sampleNum;
indexPointT = indexPoint;
[aa,indexPoint] = max(r,[],2);
j1 = sum(sum(abs(newMuK - initVal{1}))) < 1e-6;
j2 = sum(sum(sum(abs(newSigma - initVal{2})))) < 1e-6;
j3 = sum(abs(newPiK - initVal{3})) < 1e-6;
clf;
if (j1 && j2 && j3)
for i = 1:sampleNum
if (indexPoint(i)==1)
plot(Data(i,1), Data(i,2), 'r.')
end
if (indexPoint(i)==2)
plot(Data(i,1), Data(i,2), 'b.')
end
if (indexPoint(i)==3)
plot(Data(i,1), Data(i,2), 'k.')
end
if (indexPoint(i)==4)
plot(Data(i,1), Data(i,2), 'g.')
end
if (indexPoint(i)==5)
plot(Data(i,1), Data(i,2), 'm.')
end
hold on;
end
break;
else
initVal{1} = newMuK;
initVal{2} = newSigma;
initVal{3} = newPiK;
end
end
Mu = newMuK;
Sigma = newSigma;
Pi = newPiK;
r_nk = r;
end

 3. Complete main function.

clear,clc,clf
Data = GenerateData;
classNum = 5;
[sampleNum, sampleDia] = size(Data); %% Initial value
% indexNum = floor(1 + (sampleNum - 1) * rand(1,classNum));
indexNum = [50,300,500,700,900];
initMu = Data(indexNum,:); initSigmaT = [1 0.2;0.2 1];
initSigma = zeros(2,2,classNum);
for i = 1 : classNum
initSigma(:,:,i) = initSigmaT;
initPi(i,1) = 1 / classNum;
end
initVal = cell(3,1);
initVal{1} = initMu;
initVal{2} = initSigma;
initVal{3} = initPi; %% EM algorithm
[Mu, Sigma, Pi, r_nk] = EmForGmm(Data, classNum, initVal);

4. Result.

The cluster result can be show as figure 3.

Figure 3

  The probality distribution function can be writen as:

\[  p(\mathbf{x}) = \sum_{k=1}^{K}\pi_kp(\mathbf{x}|\mu_k\Sigma_k)  \]

where,

  $\mu_1  =  (1.028, -1.158) $, $\mu_2  =  (5.423, -4.538) $, $\mu_3  =  (1.036, 3.975) $,  $\mu_4  =  (5.835, 4.474) $,  $\mu_5  =  (9.074, -0.063) $

Notice that, when generate the data:

$\mu_1  =  (1, -1) $, $\mu_2  =  (5.5, -4.5) $, $\mu_3  =  (1, 4) $, $\mu_4  =  (6, 4.5) $, $\mu_5  =  (9, 0) $)


\[
\Sigma_1 = \left(
\begin{array}{cc}
1.0873& 0.0376\\
0.0376& 0.8850
\end{array}
\right),
\Sigma_2 = \left(
\begin{array}{cc}
1.1426& 0.0509\\
0.0509& 0.9192
\end{array}
\right),
\Sigma_3 = \left(
\begin{array}{cc}
0.9752& -0.0712\\
-0.0712& 0.9871
\end{array}
\right),
\Sigma_4 = \left(
\begin{array}{cc}
1.0111& -0.0782\\
-0.0782& 1.2034
\end{array}
\right),
\Sigma_5 = \left(
\begin{array}{cc}
0.8665& -0.1527\\
-0.1527& 0.9352
\end{array}
\right)
\]

Notice that, when generate the data:

\[\Sigma = \left(
\begin{array}{cc}
1& 0\\
0& 1
\end{array}
\right)
\]


$\pi_1  =  0.1986$, $\pi_2  =  0.2004 $, $\pi_3  =  0.1992$,  $\pi_4  =  0.2015 $,  $\pi_5  =  0.2002$

Notice that, when generate the data:  each guassian components occupy 20% of all data. (1000 data point, 200 for each guassian components)

EM and GMM(Code)的更多相关文章

  1. EM and GMM(Theory)

    Part 1: Theory 目录: What's GMM? How to solve GMM? What's EM? Explanation of the result What's GMM? GM ...

  2. 微信小程序API登录凭证(code),获得的用户登录态拥有一定的时效性

    调用接口获取登录凭证(code)进而换取用户登录态信息,包括用户的唯一标识(openid) 及本次登录的 会话密钥(session_key).用户数据的加解密通讯需要依赖会话密钥完成. OBJECT参 ...

  3. 三维卷积:全景图像Spherical CNNs(Code)

    卷积神经网络(CNN)可以很好的处理二维平面图像的问题.然而,对球面图像进行处理需求日益增加.例如,对无人机.机器人.自动驾驶汽车.分子回归问题.全球天气和气候模型的全方位视觉处理问题. 将球形信号的 ...

  4. css里px em rem特点(转)

    1.px特点: 1.IE无法调整px作为单位的字体大小: 2.Firefox能够调整px.em和rem. px是像素,是相对长度单位,是相对于显示器屏幕分辨率而言的. 2.em特点: 1.em的值并不 ...

  5. PCL:描述三维离散点的ROPS特征(Code)

    前言: 三维点云为三维欧式空间点的集合.对点云的形状描述若使用局部特征,则可分为两种:固定世界坐标系的局部描述和寻找局部主方向的局部描述,ROPS特征为寻找局部主方向的特征描述. 1.寻找主方向(对X ...

  6. Python:Matplotlib 画曲线和柱状图(Code)

    原文链接:http://blog.csdn.net/ikerpeng/article/details/20523679 参考资料:http://matplotlib.org/gallery.html ...

  7. Qt:&OpenCV—Q图像处理基本操作(Code)

    原文链接:http://www.cnblogs.com/emouse/archive/2013/03/31/2991333.html 作者写作一系列:http://www.cnblogs.com/em ...

  8. 编码(Code)

    很遗憾,直接搜索Code或者编码是很难得找到这本书的,我也是无意中才接触到本书. 第一次读本书,对各方面的知识都不算很懂,觉得很多地方都写的太多浅显,好像本该就是这样子,一个编码系统说的那么麻烦干嘛, ...

  9. px和em的区别(转)

    在国内网站中,包括三大门户,以及“引领”中国网站设计潮流的蓝色理想,ChinaUI等都是使用了px作为字体单位.只有百度好歹做了个可调的表率.而 在大洋彼岸,几乎所有的主流站点都使用em作为字体单位, ...

随机推荐

  1. FZU 1397 保送

    网络流入门题. 源点到每一个学生连一条边,容量为1 每个学校到汇点连一条边,容量为L 符合要求的学生和学校之间连边,容量为1. 从源点到汇点的最大流就是答案. #include<cstdio&g ...

  2. 《算法导论》插入排序----InsertSort

    算法导论,插入排序 public class InsertSort { public static double [] sort(double [] num) { for(int i =1; i< ...

  3. 一起学JUCE之HashMap

    基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同.) ...

  4. 【bug】java.lang.NoSuchMethodError: android.widget.TextView.setBackground

    安卓的背景色设置需要根据SDK的版本来分情况考虑: if (Build.VERSION.SDK_INT >= 16) { textView.setBackground(null); } else ...

  5. FlexGrid简单demo

    1.首先加入以下代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <hea ...

  6. iOS 界面开发

    iOS 自动布局 iOS 界面 之 EALayout 无需反复编译,可视化实时界面,告别Storyboard AutoLayout Xib等等烦人的工具 iOS应用国际化教程(2014版) iOS开发 ...

  7. Javascript 浮点运算问题分析与解决

    分析 JavaScript 只有一种数字类型 Number ,而且在Javascript中所有的数字都是以IEEE-754标准格式表示的. 浮点数的精度问题不是JavaScript特有的,因为有些小数 ...

  8. iOS 开源库 之 AFNetWorking 2.x

    1. 网络请求的基本知识 2. Get/Post 请求的使用 3. 文件(图片)上传 4. 断点下载 5. 其它使用细节 6. 设计优良的地方

  9. 连接linux 服务器

    File > Quick Connect ,Hostname 是ip , Username是用户名

  10. ASP.NET Zero--12.一个例子(5)商品分类管理-编辑分类

    1.添加编辑按钮 打开文件Index.js [..\MyCompanyName.AbpZeroTemplate.Web\Areas\Mpa\Views\Category\Index.js] 在acti ...